----- use sklearn library, import data files analog linear regression analysis based on python programming jupyter notebook
- First, run jupyter notebook, built environment python
- 1. Open the Windows Terminal command line, type == jupyter notebook ==, open our jupyter tool as follows:
- 2. Create a python file jupyter of web pages, as follows:
- 3, you can now enter our code in jupyter line of code inside it!
- Second, following the csv file, for example, to write python code that break down the steps of our least-squares method
- 1, we need to import basic library
- 2, we import the data file == mytest.csv ==
- 3, for our x, y assignment sampling
- 4, the following is the use of data processing sklearn library, find y = ax + b and the == a == == b ==
- 5, the output of our print linear regression equation obtained
- 6, by == scatter == fitting curve shown in FIG.
- Three, python library use sklearn all-source analysis regression equation
- Four, == shift + enter == run our code
- 1, the results shown below:
- 2, it can be seen, our arguments upper fitting python code by X (weight) value of the linear regression equation 20, then we excel wps by the same data, the same value of 20, and we sklearn database comparison results, as shown below:
- 3, to the value from the variable X 200 and 2000 of the linear regression equation, we can make changes to x, y assigned to do!
The last blog, we use linear regression analysis of the least squares method, this method is the basis of our understanding of linear regression of high school we studied, it is possible to solve the regression coefficient and intercept According to this method, but as college students for us, there is clearly a more simple way, without having to write the code ourselves, it is ok just need to import third-party libraries, this blog, Lin Jun seniors will take us to know, how to usesklearn libraryTo analyze linear regression of
First, run jupyter notebook, built environment python
1. Open the Windows Terminal command line, enterjupyter notebook, Open our jupyter tool as follows:
2. Create a python file jupyter of web pages, as follows:
3, you can now enter our code in jupyter line of code inside it!
Second, following the csv file, for example, to write python code that break down the steps of our least-squares method
Document reads as follows:
file for weight X, Y linear regression of height
1, we need to import basic library
from sklearn import linear_model #表示,可以调用sklearn中的linear_model模块进行线性回归。
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
2, we import the data filemytest.csv
data = np.loadtxt(open("D:mytest.csv","rb"),delimiter=",",skiprows=0)
data1=data[0:20]
Above == data1 = data [0:20] == means that we import file data before the row 20, by analogy, is introduced 200,2000 line data here can be modified as follows
1), introduced into the data line 200
data1=data[0:200]
2) introducing rows 2000
data1=data[0:2000]
3, for our x, y assignment sampling
x=[example[1] for example in data1]
y=[example[2] for example in data1]
X = np.asarray(x).reshape(-1, 1)
Y = np.asarray(y).reshape(-1, 1)
1), above, X, Y 1 represents the first column of the imported data, second row are assigned to the X, Y, as shown below:
2), abovex, yIt is installed for the data listarray arrayConvenient sklearn library data analysis
4, the following is the use of data processing sklearn library, find y = ax + b inawithb
model = linear_model.LinearRegression()
model.fit(X,Y)
b=model.intercept_[0] #截距
a=model.coef_[0]#线性模型的系数
a1=a[0]
The above data processing does not require us to manage, third-party libraries sklearn is written, we are directly used on ok
5, the output of our print linear regression equation obtained
print("y=",a1,"x+",b)
6, byscatterFitting curve shown in FIG.
y1 = a1*X + b
plt.scatter(X,Y)
plt.plot(x,y1,c='r')
These are the use sklearn libraries for simple linear regression equation, the process is relatively simple, basic is the use of third-party libraries, the only difficulty is to solve the file to import, and then assign!
Three, python library use sklearn all-source analysis regression equation
from sklearn import linear_model #表示,可以调用sklearn中的linear_model模块进行线性回归。
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = np.loadtxt(open("D:mytest.csv","rb"),delimiter=",",skiprows=0)
data1=data[0:20]
x=[example[1] for example in data1]
y=[example[2] for example in data1]
X = np.asarray(x).reshape(-1, 1)
Y = np.asarray(y).reshape(-1, 1)
model = linear_model.LinearRegression()
model.fit(X,Y)
b=model.intercept_[0] #截距
a=model.coef_[0]#线性模型的系数
a1=a[0]
print("y=",a1,"x+",b)
y1 = a1*X + b
plt.scatter(X,Y)
plt.plot(x,y1,c='r')
four,shift+enterRun our code
1, the results shown below:
2, it can be seen, our arguments upper fitting python code by X (weight) value of the linear regression equation 20, then we excel wps by the same data, the same value of 20, and we sklearn database comparison results, as shown below:
As can be seen, python out of the linear regression fit and we excel fit linear regression equation, roughly the same, the problem is the significant figures, you can see, our code is quite perfect!
3, to the value from the variable X 200 and 2000 of the linear regression equation, we can make changes to x, y assigned to do!
1) The value x 200
Contrast excel:
2), the value of 2000 x
contrast excel:
That's all we have in this blog, I hope that through this blog, we can better understand how to use third-party librariessklearnSeeking linear regression equation Oh!
Problems encountered little friends Comments Guest Book Oh, seniors give you patience to answer!
Chen programming years and one day in January ^ _ ^