kaggle官网course Machine—Learning课程Exercise全部答案
【摘要】
# Code you have previously used to load dataimport pandas as pdfrom sklearn.metrics import mean_absolute_errorfrom sklearn.model_selection import train_test_splitfrom sk...
-
# Code you have previously used to load data
-
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 = '../input/home-data-for-ml-course/train.csv'
-
-
home_data = pd.read_csv(iowa_file_path)
-
# Create target object and call it y
-
y = home_data.SalePrice
-
# Create X
-
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=1)
-
-
# Specify Model
-
iowa_model = DecisionTreeRegressor(random_state=1)
-
# 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 when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
-
-
# Using best value for max_leaf_nodes
-
iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
-
iowa_model.fit(train_X, train_y)
-
val_predictions = iowa_model.predict(val_X)
-
val_mae = mean_absolute_error(val_predictions, val_y)
-
print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
-
# Define the model. Set random_state to 1
-
rf_model = RandomForestRegressor(random_state=1)
-
rf_model.fit(train_X, train_y)
-
rf_val_predictions = rf_model.predict(val_X)
-
rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)
-
-
print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
-
-
# Set up code checking
-
from learntools.core import binder
-
binder.bind(globals())
-
from learntools.machine_learning.ex6 import *
-
print("\nSetup complete")
文章来源: blog.csdn.net,作者:小小谢先生,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/xiewenrui1996/article/details/103478207
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)