我最近开始学习Python和机器学习。我正在做一个关于房价的基本决策树回归器示例。我已经训练了算法,并找到了最佳分支数量,但如何在新数据上使用这个模型呢?
我有以下列,我的目标值是’SalePrice’
['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
显然,对于原始数据,我已经有了SalePrice,所以我可以比较这些值。如果我只有上面的列,我该如何找到价格呢?
完整代码如下
import pandas as pdfrom sklearn.metrics import mean_absolute_errorfrom sklearn.model_selection import train_test_splitfrom sklearn.tree import DecisionTreeRegressor# Path of the file to readiowa_file_path = 'train.csv'home_data = pd.read_csv(iowa_file_path)#Simplify data to remove useless infoSimpleTable=home_data[['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd','SalePrice']]# Create target object and call it y # input target valuey = home_data.SalePrice # Create X input columns names to be analysedfeatures = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']X = home_data[features]# Split into validation and training datatrain_X, val_X, train_y, val_y = train_test_split(X, y, random_state=0, test_size=0.8, train_size=0.2)# Specify Modeliowa_model = DecisionTreeRegressor(random_state=0)# Fit Modeliowa_model.fit(train_X, train_y)# Make validation predictions and calculate mean absolute errorval_predictions = iowa_model.predict(val_X)val_mae = mean_absolute_error(val_predictions, val_y)print("Validation MAE: {:,.0f}".format(val_mae))def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y): model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0) model.fit(train_X, train_y) preds_val = model.predict(val_X) mae = mean_absolute_error(val_y, preds_val) return(mae)# to find best number of leavescandidate_max_leaf_nodes = [10, 20, 50, 100, 200, 400] # start with big numbers are work your way downfor max_leaf_nodes in candidate_max_leaf_nodes: my_mae=get_mae(max_leaf_nodes,train_X,val_X,train_y,val_y) print("MAX leaf nodes: %d \t\t Mean Absolute Error:%d" %(max_leaf_nodes,my_mae))scores = {leaf_size: get_mae(leaf_size, train_X, val_X, train_y, val_y) for leaf_size in candidate_max_leaf_nodes}best_tree_size = min(scores, key=scores.get)print(best_tree_size)#run on all data and put back into data fram final_model=DecisionTreeRegressor(max_leaf_nodes=best_tree_size,random_state=0)final_model.fit(X,y)final_model.predict(X)final_predictions = final_model.predict(X)finaltableinput = {'Predicted_Price':final_predictions}finaltable = pd.DataFrame(finaltableinput)SimpleTable.head()jointable = SimpleTable.join(finaltable)#export data with predicted values to csvjointable.to_csv('newdata4.csv')
提前感谢
回答:
如果你想根据独立变量(X)使用已训练的模型来预测价格(Y),你需要使用predict()
方法。这意味着基于你的算法在训练过程中开发的模型,它将使用这些变量来预测SalePrice
。我看到你已经在代码中使用了.predict()
。
你应该首先定义变量,例如:
X_new = df_new[['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']] #假设这是一个Pandas数据框new_sale_price = final_model.predict(X_new) #这将返回一个数组df_new['SalePrice'] = new_sale_price #长度将是相等的,所以你应该不会遇到问题。
你也可以在一行中完成这个操作:
df_new['SalePrice'] = final_model.predict(X_new)
当然,由于你不知道这些X
值的真实SalePrice
,你无法进行性能检查。这就是在现实世界中,当你想根据一组变量预测或预测价格时发生的情况,你需要训练你的模型以达到其最佳性能,然后使用它进行预测!如果你有疑问,请随时在评论中留言。