-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRandomForest.py
47 lines (38 loc) · 1.28 KB
/
RandomForest.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 28 09:27:15 2019
@author: maggiedube
"""
#import numpy as np
import pandas as pd
import matplotlib.pyplot as plt #Data Visualization Libraries
#import seaborn as sns
#%matplotlib inline
#defining predictors as the pre-set feature names
dd = pd.read_csv("~/desktop/DSCData.csv")
dd.head()
dd.info()
dd.describe()
dd['Glucose Level'][0]
#sns.pairplot(dd)
dd.corr()
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_colwidth', 1000)
pd.set_option('display.width', None)
X = dd[['Glucose Level', 'Minutes passed']]
#X = dd[['Glucose Level', 'Minutes passed', 'Carbohydrate Intake', 'Insulin Injected', 'Sick?', 'Menstrual Cycle?']]
y = dd['Insulin Injected']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
#X_test.fillna(X_test.mean())
#X_test = X_test.fillna(X_train.mean())
from sklearn.ensemble import RandomForestRegressor
f = RandomForestRegressor()
f.fit(X_train, y_train)
predictions = f.predict(X_test)
plt.scatter(y_test, predictions)
plt.show()
from sklearn.metrics import mean_squared_error
print("Mean Squared Error: %.2f" % mean_squared_error(y_test, predictions))