機器學習---披薩價格預測

已知披薩的部分直徑和價格,預測當直徑爲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()

圖形展示:
在這裏插入圖片描述

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