导入模块
import numpy as np
from sklearn.linear_model import LinearRegression
准备x的数据
X = np.array([
[1,3],
[2,4],
[3,4],
[4,6],
[5,5]
])
准备y的数据
y = np.array([1, 3, 5, 7, 9])
创建线性回归模块
df = LinearRegression()
填充数据并训练
df.fit(X, y)
输出参数
print(“w=”, df.coef_, “b=”, df.intercept_)
定义一个函数y=wx+b
def main(X):
y = np.dot(df.coef_, X)+df.intercept_
return y
测试数据
print(main([3,4]))
全部代码
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([
[1,3],
[2,4],
[3,4],
[4,6],
[5,5]
])
y = np.array([1, 3, 5, 7, 9])
df = LinearRegression()
df.fit(X, y)
print("w=", df.coef_, "b=", df.intercept_)
def main(X):
y = np.dot(df.coef_, X)+df.intercept_
return y
print(main([3,4]))