-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled10.py
58 lines (39 loc) · 1.53 KB
/
Untitled10.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
#!/usr/bin/env python
# coding: utf-8
# In[7]:
pip install xgboost
# In[11]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the data from a CSV file
df = pd.read_csv('sleep_newww - Sheet1.csv')
# Separate the features (X) and labels (y)
X = df[['Sleep duration', 'Exercise frequency']]
y = df['Sleep effIicency 1']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Random Forest model
rf_model = RandomForestClassifier()
rf_model.fit(X_train, y_train)
rf_y_pred = rf_model.predict(X_test)
rf_accuracy = accuracy_score(y_test, rf_y_pred)
print("Random Forest Accuracy:", rf_accuracy)
x1 = np.linspace(X['Sleep duration'].min() - 2, X['Sleep duration'].max() + 2, 100)
x2 = np.linspace(X['Exercise frequency'].min() - 2, X['Exercise frequency'].max() + 2, 100)
xx1, xx2 = np.meshgrid(x1, x2)
Z = rf_model.predict(np.c_[xx1.ravel(), xx2.ravel()])
Z = Z.reshape(xx1.shape)
plt.figure()
plt.contourf(xx1, xx2, Z, alpha=0.3)
plt.scatter(X['Sleep duration'], X['Exercise frequency'], c=y, edgecolors='k')
plt.xlabel('Sleep duration')
plt.ylabel('Exercise frequency')
plt.title('Decision Boundary (Random Forest)')
plt.xlim(X['Sleep duration'].min() - 2, X['Sleep duration'].max() + 2)
plt.ylim(X['Exercise frequency'].min() - 2, X['Exercise frequency'].max() + 2)
plt.show()
# In[ ]: