机器学习---披萨价格预测

已知披萨的部分直径和价格,预测当直径为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.LinearRegression(fit_intercept=True,normalize=False)

x = np.array([[6],[8],[10],[14],[18]])
y = [7,9,13,17.5,18]
reg.fit(x,y)

k = reg.coef_ # 获取斜w1,w2,w3....wn
b   = reg.intercept_ # 获取截距

x0  = np.arange(0,25,0.5)
y0  = k*x0+b


plt.figure()
plt.scatter(x,y)
plt.plot(x0,y0)
plt.show()

图形展示:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章