Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.py 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  1. import pandas as pd
  2. from sklearn.metrics import mean_absolute_error
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.ensemble import RandomForestRegressor
  5. from dagshub import DAGsHubLogger
  6. if __name__ == "__main__":
  7. iowa_file_path = 'data/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. logger = DAGsHubLogger()
  17. logger.log_hyperparams(model="RandomForest")
  18. logger.log_hyperparams(criterion="mae")
  19. # Specify Model
  20. iowa_model = RandomForestRegressor(n_estimators=100, criterion='mae', random_state=0)
  21. # Fit Model
  22. iowa_model.fit(train_X, train_y)
  23. # Make validation predictions and calculate mean absolute error
  24. val_predictions = iowa_model.predict(val_X)
  25. val_mae = mean_absolute_error(val_predictions, val_y)
  26. logger.log_metrics(mae=val_mae)
  27. logger.save()
  28. logger.close()
  29. print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
  30. # Save predictions in format used for competition scoring
  31. output = pd.DataFrame({'Id': val_X.index,
  32. 'SalePrice': val_predictions})
  33. output.to_csv('submission.csv', index=False)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...