-
Notifications
You must be signed in to change notification settings - Fork 0
/
desion_tree_regressor_amangel.py
66 lines (40 loc) · 1.62 KB
/
desion_tree_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
# -*- coding: utf-8 -*-
"""desion_tree_regressor_amangel.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_Fv12se_fqrjHNu4aSP6xqzmQfVVdk81
## 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 .#######"""
dataset1.round()
"""# split dataset"""
x = dataset1.iloc[:, -5:-1].values
y = dataset1.iloc[:, -1].values
print(x)
print(y)
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 Decision Tree Regression model on the whole dataset"""
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(X_train, y_train)
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)