Python 如何在新数据上使用决策树回归器?(蟒蛇、熊猫、猫科动物)

Python 如何在新数据上使用决策树回归器?(蟒蛇、熊猫、猫科动物),python,pandas,machine-learning,sklearn-pandas,Python,Pandas,Machine Learning,Sklearn Pandas,我最近开始学习python和机器学习。我一直在做一个涉及房价的基本决策树回归例子。所以我已经训练了算法,找到了最佳的分支数,但是我如何在新数据上使用它呢 我有以下几列,我的目标值是“SalePrice” ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd'] 显然,对于原始数据,我已经有了SalePrice,所以我可以比较这些值。如果我只有上面的栏目,我怎么才能找到

我最近开始学习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')





提前感谢

如果您想知道价格(Y)给定的自变量(X)和已经训练过的模型,您需要使用
predict()
方法。这意味着,基于您的算法在培训中开发的模型,它将使用变量预测
销售价格。我看到您已经在代码中使用了
.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
,因此无法进行性能检查。这就是现实世界中发生的事情,每当你想根据一组变量进行预测或预测价格时,你需要训练你的模型以达到其峰值性能,然后用它进行预测!如果您有疑问,请随时在评论中留下任何问题。

决策树算法是一种受监督的学习模型,这意味着为了对其进行培训,您必须向模型提供功能和目标的数据(“销售价格”)

如果您想在没有目标数据的情况下应用机器学习,则必须使用无监督的模型


可以找到这些不同类型学习模型的基本介绍。

谢谢您的帮助,但我的意思是使用训练数据分析来预测新数据。这个问题现在已经得到了回答。很高兴能帮上忙!请随意接受我的回答,这将标志着问题已被回答,并有助于提高我的声誉:)