我正在研究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数据分为2个部分用于训练和测试数据,其中测试数据为数据集的20%。我想做的是通过和输入特定车辆的年龄和里程,并让模型根据该单一输入来预测销售价格。我将在哪里添加此输入?
链接到github示例-https://github.com/codebasics/py/blob/master/ML/6_train_test_split/train_test_split.ipynb
只要有一点了解,就可以从任何教程中获得……。
clf.predict(vect)
是一个返回输入矢量
vect
的预测的函数。在测试集上执行此操作时,您将获得用于评估测试准确性的数据。要获得一个输入的预测,请将该单个输入作为参数。
要使用此功能,必须捕获返回值:
vect_pred = clf.predict(vect). Is that what you needed?