kaggle官网course Machine—Learning课程Exercise全部答案

举报
小小谢先生 发表于 2022/04/16 00:23:02 2022/04/16
【摘要】 # 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...

  
  1. # Code you have previously used to load data
  2. import pandas as pd
  3. from sklearn.metrics import mean_absolute_error
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.tree import DecisionTreeRegressor
  6. # Path of the file to read
  7. iowa_file_path = '../input/home-data-for-ml-course/train.csv'
  8. home_data = pd.read_csv(iowa_file_path)
  9. # Create target object and call it y
  10. y = home_data.SalePrice
  11. # Create X
  12. features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
  13. X = home_data[features]
  14. # Split into validation and training data
  15. train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
  16. # Specify Model
  17. iowa_model = DecisionTreeRegressor(random_state=1)
  18. # Fit Model
  19. iowa_model.fit(train_X, train_y)
  20. # Make validation predictions and calculate mean absolute error
  21. val_predictions = iowa_model.predict(val_X)
  22. val_mae = mean_absolute_error(val_predictions, val_y)
  23. print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
  24. # Using best value for max_leaf_nodes
  25. iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
  26. iowa_model.fit(train_X, train_y)
  27. val_predictions = iowa_model.predict(val_X)
  28. val_mae = mean_absolute_error(val_predictions, val_y)
  29. print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
  30. # Define the model. Set random_state to 1
  31. rf_model = RandomForestRegressor(random_state=1)
  32. rf_model.fit(train_X, train_y)
  33. rf_val_predictions = rf_model.predict(val_X)
  34. rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)
  35. print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
  36. # Set up code checking
  37. from learntools.core import binder
  38. binder.bind(globals())
  39. from learntools.machine_learning.ex6 import *
  40. print("\nSetup complete")

 

文章来源: blog.csdn.net,作者:小小谢先生,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/xiewenrui1996/article/details/103478207

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。