[text] task 2

Viewer

  1. import pandas as pd
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.ensemble import RandomForestRegressor
  4. from sklearn.metrics import r2_score
  5.  
  6. # read the data on the companies' accounts and activity
  7. fb = pd.read_csv('/datasets/dataset_facebook_cosmetics_us.csv', sep = ';')
  8.  
  9. # divide the data into features (the X matrix) and a target variable (y)
  10. X = fb.drop('Total Interactions', axis = 1)
  11. y = fb['Total Interactions']
  12.  
  13. # divide the data into train and test
  14. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
  15.  
  16. # define the model's algorithm
  17. model = RandomForestRegressor(random_state=0) # define the model as an element of the RandomForestRegressor class (random_state=0)
  18.  
  19. # train the model
  20. model.fit(X_train,y_train) # train your model using train data
  21.  
  22. # use the trained model to make forecasts
  23. predictions = model.predict(X_train)# make a forecast for test data using the model
  24.  
  25. # estimate R-squared using test data and print the result
  26. r2 = r2_score(y_train,predictions) # write your code here
  27. print('R-squared value: ', r2)

Editor

You can edit this paste and save as new:


File Description
  • task 2
  • Paste Code
  • 01 Mar-2021
  • 1.09 Kb
You can Share it: