-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep.py
61 lines (47 loc) · 1.91 KB
/
sleep.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
# -*- coding: utf-8 -*-
"""Sleep.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wxPN8wUtZPntTknM97web7Ep2xUnqQWH
"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
# Set a random seed for reproducibility
np.random.seed(42)
# Create a synthetic sleep dataset
num_samples = 1000
# Features related to sleep (you can modify and add more features)
hours_of_sleep = np.random.uniform(4, 10, num_samples)
caffeine_intake = np.random.uniform(0, 300, num_samples)
exercise_hours = np.random.uniform(0, 2, num_samples)
# Assume a simple linear relationship with sleep quality
# You can adjust coefficients based on the impact you want each feature to have
sleep_quality = 70 + 5 * hours_of_sleep - 2 * caffeine_intake + 3 * exercise_hours + np.random.normal(0, 5, num_samples)
# Create a DataFrame
data = pd.DataFrame({'Hours_of_Sleep': hours_of_sleep,
'Caffeine_Intake': caffeine_intake,
'Exercise_Hours': exercise_hours,
'Sleep_Quality': sleep_quality})
# Split the data into training and testing sets
X = data.drop('Sleep_Quality', axis=1)
y = data['Sleep_Quality']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a linear regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
predictions = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, predictions)
print(f'Mean Squared Error: {mse}')
# Plot actual vs predicted values
plt.scatter(y_test, predictions)
plt.xlabel('Actual Sleep Quality')
plt.ylabel('Predicted Sleep Quality')
plt.title('Actual vs Predicted Sleep Quality')
plt.show()