回归模型代码实现

回归模型代码实现

回归模型代码实现_第1张图片

1.多元函数线性回归w的求法

import  numpy as np

# 定义一组自变量,每条数据最后一列为b
x = np.array([[1,1,1,1],[1,2,3,1],[2,2,4,1],[2,3,5,1]])

# 定义w,其中b的值为0.5
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 = 1*x_0+2*x_1+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

你可能感兴趣的:(机器学习,数据挖掘,人工智能,线性回归,python,机器学习,pandas,numpy)