回归模型代码实现
1.多元函数线性回归w的求法
import numpy as np
x = np.array([[1,1,1,1],[1,2,3,1],[2,2,4,1],[2,3,5,1]])
y = np.dot(x,np.array([2,4,6,0.5]))
x_x_1 = np.linalg.inv(np.dot(x.T,x))
x_1_y = np.dot(x.T,y)
w = np.dot(x_x_1,x_1_y)
print(w)
[2,4,6,0.5]
2.线性回归代码实现
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([[1,1],[1,2],[2,2],[2,3]])
y = np.dot(x,np.array([1,2])) + 3
reg = LinearRegression().fit(x,y)
reg.score(x,y)
1.0
reg.coef_
array([1., 2.])
reg.intercept_
3.000000000000001
reg.predict(np.array([[3,5]]))
array([16.])
3.对数几率回归代码实现
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X,y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X,y)
clf.predict(X[:2,:])
array([0, 0])
clf.predict_proba(X[:2,:])
array([[9.81788430e-01, 1.82115558e-02, 1.43322263e-08],[9.71702577e-01, 2.82973926e-02, 2.99842724e-08]])
clf.score(X,y)
0.9733333333333334