[英]Predict a discret value with sklearn
任何人都可以幫助教如何預測 x=12 的響應嗎? 輸入的命令或指令是什么?
from sklearn.linear_model import LinearRegression
import numpy as np
#Data
x = np.array([6, 8, 10, 14, 18]).reshape((-1, 1))
y = np.array([7, 9, 13, 17, 18])
#Instantion of the model
model_linearReg=LinearRegression()
model_linearReg.fit(x,y)
#Precision of the model
precision=model_linearReg.score(x,y)
print(precision*100)
#prediction of the model
prediction=model_linearReg.predict(x)
print(prediction)
LinearRegression或任何 sklearn 分類器的輸入幾乎總是 2D numpy
arrays 形狀N_targets x N_features
; 由於您的回歸任務只有一個變量( N_targets = 1
)和一個特征( N_features = 1
),您只需將12
包裝到一個列表中(技術上類似於數組)兩次:
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([6, 8, 10, 14, 18]).reshape((-1, 1))
y = np.array([7, 9, 13, 17, 18])
lr = LinearRegression().fit(x, y)
print(lr.predict([[12]])) # [13.56896552]
# probably want to unwrap as well
print(lr.predict([[12]])[0]) # 13.56896551724138
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.