已知披萨的部分直径和价格,预测当直径为X时,价格是多少??
直径 | 价格 |
---|---|
6 | 7 |
8 | 9 |
10 | 13 |
14 | 17.5 |
18 | 18 |
代码如下:
import numpy as np
import sklearn.linear_model
xTrain = np.array([6,8,10,14,18])[:,np.newaxis]
print(xTrain)
yTrain = np.array([7,9,13,17.5,18])
model = sklearn.linear_model.LinearRegression()
hypothesis = model.fit(xTrain,yTrain)
print('theta0=',hypothesis.intercept_)
print('theta1=',hypothesis.coef_)
print(model.predict([[12]]))
运行结果:
[[ 6]
[ 8]
[10]
[14]
[18]]
theta0= 1.965517241379315
theta1= [0.9762931]
[13.68103448]
代码:
import numpy as np
import sklearn.linear_model
import matplotlib.pyplot as plt
reg =sklearn.linear_model.Li