Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用clf.predict获得线性回归模型进行预测?_Python_Scikit Learn - Fatal编程技术网

Python 如何使用clf.predict获得线性回归模型进行预测?

Python 如何使用clf.predict获得线性回归模型进行预测?,python,scikit-learn,Python,Scikit Learn,因此,我用线性回归建立了一个汽车价格预测模型。 现在我需要用它来预测未来5年的价格。 如何使用clf进行预测 X = df[['Year','Engine','FuelType','Age','Transmission','Mileage']] y=df['Price'] 这是我的X和y值 X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2) clf = LinearRegression() clf.fit(X

因此,我用线性回归建立了一个汽车价格预测模型。 现在我需要用它来预测未来5年的价格。 如何使用clf进行预测

X = df[['Year','Engine','FuelType','Age','Transmission','Mileage']]
y=df['Price']
这是我的X和y值

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)

clf = LinearRegression()
clf.fit(X_train, y_train)

clf.predict(X_test)   

print(clf.score(X_test, y_test))
在这之后我用

clf.predict([[2022,1300,0,5,0,10000]])

我得到了
数组([-5722871.63724422])

为了只得到正值,你应该对y值进行对数运算:

y_log = np.log(ytrain.values, where=(ytrain.values>0))
并对模型预测进行反变换:

y_pred = exp(clf.predict(X_test))

你确定你的模特不合身吗?列车组的预测值是多少?您是否尝试绘制仅更改年份时的预测值,以查看模型的行为?是的,我绘制了年份和价格之间的图表