gpt4 book ai didi

python - 如何对新数据使用决策树回归器? (Python、Pandas、Sklearn)

转载 作者:行者123 更新时间:2023-11-30 08:31:34 29 4
gpt4 key购买 nike

我最近开始学习Python和机器学习。我一直在做一个涉及房价的基本决策树回归器示例。所以我已经训练了算法并找到了最佳分支数量,但是如何在新数据上使用它?

我有以下几列,我的目标值为“SalePrice”

['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']

显然,对于原始数据,我已经有了 SalePrice,因此我可以比较这些值。如果我只有上面的列,我该如何查找价格?

完整代码如下

import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor


# Path of the file to read
iowa_file_path = 'train.csv'

home_data = pd.read_csv(iowa_file_path)
#Simplify data to remove useless info
SimpleTable=home_data[['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd','SalePrice']]
# Create target object and call it y # input target value
y = home_data.SalePrice
# Create X input columns names to be analysed
features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
X = home_data[features]

# Split into validation and training data
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=0, test_size=0.8, train_size=0.2)


# Specify Model
iowa_model = DecisionTreeRegressor(random_state=0)
# Fit Model
iowa_model.fit(train_X, train_y)

# Make validation predictions and calculate mean absolute error
val_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 leaves
candidate_max_leaf_nodes = [10, 20, 50, 100, 200, 400] # start with big numbers are work your way down
for 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 csv
jointable.to_csv('newdata4.csv')




提前致谢

最佳答案

如果您想通过已训练的模型了解给定自变量 (X) 的价格 (Y),则需要使用 predict() 方法。这意味着,根据您的算法通过训练开发的模型,它将使用变量来预测 SalePrice。我发现您已经在代码中使用了 .predict()

您应该首先定义变量,例如:

X_new = df_new[['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']] #Let's say this is a pandas dataframe
new_sale_price = final_model.predict(X_new) #This will return an array
df_new['SalePrice'] = new_sale_price #The length will be of equal length so you should have no trouble.

您也可以通过一行完成此操作:

df_new['SalePrice'] = final_model.predict(X_new) 

当然,由于您不知道 X 这些值的真实 SalePrice,因此您无法进行性能检查。这就是现实世界中发生的情况,每当您想要基于一组变量进行预测或预测价格时,您需要训练您的模型以实现其最佳性能,然后用它进行预测!如果您有疑问,请随时在评论中留下任何问题。

关于python - 如何对新数据使用决策树回归器? (Python、Pandas、Sklearn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59936599/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com