- import pandas as pd
- from sklearn.model_selection import train_test_split
- from sklearn.ensemble import RandomForestRegressor
- from sklearn.metrics import r2_score
- # read the data on the companies' accounts and activity
- fb = pd.read_csv('/datasets/dataset_facebook_cosmetics_us.csv', sep = ';')
- # divide the data into features (the X matrix) and a target variable (y)
- X = fb.drop('Total Interactions', axis = 1)
- y = fb['Total Interactions']
- # divide the data into train and test
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
- # define the model's algorithm
- model = RandomForestRegressor(random_state=0) # define the model as an element of the RandomForestRegressor class (random_state=0)
- # train the model
- model.fit(X_train,y_train) # train your model using train data
- # use the trained model to make forecasts
- predictions = model.predict(X_train)# make a forecast for test data using the model
- # estimate R-squared using test data and print the result
- r2 = r2_score(y_train,predictions) # write your code here
- print('R-squared value: ', r2)
[text] task 2
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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:
Latest Code Pastes