-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_forest_regressor_amangel.py
71 lines (44 loc) · 1.78 KB
/
random_forest_regressor_amangel.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""random_forest_regressor_amangel.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1WcyIbIh_vqP32rKWinXKxCmgGhCFzRsk
## import libraries and dataset
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv("yiyao_df3.csv")
print(dataset)
dataset.head()
"""# rearrange column"""
df_reorder = dataset[['Gender','Ethnicity', 'DevType', 'Hobbyist', 'Employment', 'Country', 'EdLevel', 'UndergradMajor', 'OrgSize', 'Year', 'Age', 'LanguageWorkedWith', 'DatabaseWorkedWith', 'YearsCodePro', 'ConvertedComp']] # rearrange column here
df_reorder.to_csv('reorder.csv', index=False)
dataset1 = pd.read_csv("reorder.csv")
dataset1.head()
"""## remove excess numbers after decimal """
dataset2 = pd.read_csv("reorder.csv")
dataset2 = dataset2.round()
dataset2.head()
"""# split dataset"""
x = dataset1.iloc[:, -5:-1].values
y = dataset1.iloc[:, -1].values
print(x)
print(y)
"""## removing 'NaN' from datset"""
x = np.nan_to_num(x)
y = np.nan_to_num(y)
"""## Splitting the dataset into the Training set and Test set"""
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
"""## Training the Random forest regressor model"""
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
regressor.fit(X_train, y_train)
"""## Predicting the Test set results"""
y_pred = regressor.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
"""## Evaluating the Model Performance"""
from sklearn.metrics import r2_score
r2_score(y_test, y_pred)