Home>
I don't know what the "#solve the least squares formula to get the coefficient" part of the reference code does.
How should I write it if I want to adapt it to my own code?
# Import required libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
#Generation of observation points (generate y with noise for x at equal intervals)
np.random.seed (0)
x = (np.arange (51)/50) [:, np.newaxis]
noise = (np.random.rand (51)/3) [:, np.newaxis]
y = (x * 2) + noise
y [30:38] = 3.25
# Solve the least squares equation to get the coefficient
x_ = np.concatenate ([x, np.ones (np.shape (x))], axis = 1)
w = np.dot (np.linalg.inv (np.dot (x_.T, x_)), np.dot (x_.T, y))
y_hat_ = np.dot (x_, w)
Code at hand
#Experimental value
x = np.array ([6.26379, 8.57417, 8.66527, 8.75069, 11.6708, 12.3487, 14.5032, 15.7422, 21.7646, 23.0518, 26.5069, 26.4035, 26.321, 23.0045, 19.2654, 17.9425, 14.5669, 13.513, 10.4902, 9.95136, 9.77395])
y = np.array ([3.709910308, 3.300454417, 3.219869361, 2.879991517, 2.250120678, 2.24981186, 1.859931899, 1.839996231, 1.560029151, 1.360016958, 1.210037387, 1.527926405, 1.320005022, 1.340038138, 1.618120234, 1.410033737, 1.83006856, 1.849465)
#Function I want to find
def func (x, a):
return (((a + (b * x))) + ((a + ((b * x) ** 2))(4 * b * math.log (0.1))) ** 0.5)/(2 * b)

Answer # 1
Related articles
 python  i don't know how to deal with the error
 i don't know how to write python
 python  [beginner] i don't know how to write a bat file i want to run py
 python  i don't know how to put out the sum of the matrix
 python  i don't know how to fix the type error
 python  i don't know how to do css select in bs4
 python  i don't know how to use wait_for_motion () and wait_for_no_motion ()
 python  i want to know how to not plot data with a value of 0 in matplotlib
 [wordpress] i don't know how to move pages
 c #  i don't know how to call aync/awit
 facebook  i don't know how to set the callback url to the app!
 i don't know how to fix it due to a recursion error
 c  i don't know how many times it has been run
 [vba] i don't know how to log in automatically from the next time [keep login]
 python  i don't know the cause of the error
 i don't know how to upload php
 xcode [beginner] i don't know how to put out the code
 reactjs  [react] i don't know how to pass arguments
 html  i don't know how to specify an element
Trends
 python  you may need to restart the kernel to use updated packages error
 php  coincheck api authentication doesn't work
 php  i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
 [php] i want to get account information using coincheck api
 the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
 javascript  how to check if an element exists in puppeteer
 python 3x  typeerror: 'method' object is not subscriptable
 i want to call a child component method from a parent in vuejs
 dart  flutter: the instance member'stars' can't be accessed in an initializer error
 xcode  pod install [!] no `podfile 'found in the project directory
After all, what I want to do is to find the coefficient of the nonlinear function by the least squares method.
Then you can do it with scipy.optimize.curve_fit.
Reference: Nonlinear function modeling in Python