首页 > 解决方案 > 使用线性回归传递输入以获取预测的车辆价格

问题描述

我正在编写 Dhaval Patel 的教程,以创建一个线性回归预测模型,以根据年龄和里程获得汽车销售价格。该模型效果很好,但我不确定如何通过输入来获得预测的销售价格,因为我对这一切都很陌生,但真的很想学习!

以下是生成销售价格输出预测的基本 Python 脚本 -

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

df = pd.read_csv("carprices.csv")
print(df)

X = df[['Mileage','Age(yrs)']]
y = df['Sell Price($)']

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

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

clf.predict(X_test)
print(y_test)

print(clf.score(X_test, y_test))


# carprices.csv data structure
    Mileage  Age(yrs)  Sell Price($)
    0     69000         6          18000
    1     35000         3          34000
    2     57000         5          26100
    etc..

======= Output ======
# array with predicted sale prices
array([25246.47327651, 16427.86889147, 27671.99607064, 25939.47978912])

#Output of test data
5     26750
14    19400
19    28200
2     26100
Name: Sell Price($), dtype: int64
0.7512386644573188

所以基本上它把 csv 数据分成两部分,用于训练和测试数据,其中测试数据占数据集的 20%。我想做的是传递和输入某辆车的年龄和里程,并让模型根据该单一输入预测销售价格。我将在哪里添加此输入?

链接到 github 示例 - https://github.com/codebasics/py/blob/master/ML/6_train_test_split/train_test_split.ipynb

标签: pythonlinear-regressionprediction

解决方案


这很容易从任何教程中获得......只要有一点理解。

clf.predict(vect)

是一个函数,它返回对输入向量 的预测vect。当您在测试集上执行此操作时,您将获得用于评估测试准确性的数据。要获得一个输入的预测,请将该单个输入作为参数。

要利用这一点,您必须捕获返回值:

vect_pred = clf.predict(vect).  Is that what you needed?

推荐阅读