Upload application20.py
Browse files- application20.py +73 -0
application20.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
df = pd.read_csv('C:/Users/Donte Patton/Downloads/dataset_2191_sleep.csv')
|
3 |
+
df.head()
|
4 |
+
import warnings
|
5 |
+
warnings.filterwarnings('ignore')
|
6 |
+
|
7 |
+
print(df.shape)
|
8 |
+
|
9 |
+
df.isnull().sum().sum()
|
10 |
+
|
11 |
+
df.isnull().sum()
|
12 |
+
|
13 |
+
df.dtypes
|
14 |
+
|
15 |
+
import pandas as pd
|
16 |
+
import numpy as np
|
17 |
+
|
18 |
+
df.replace('?', np.nan, inplace=True)
|
19 |
+
|
20 |
+
df['max_life_span'] = pd.to_numeric(df['max_life_span'], errors='coerce')
|
21 |
+
df['gestation_time'] = pd.to_numeric(df['gestation_time'], errors='coerce')
|
22 |
+
df['total_sleep'] = pd.to_numeric(df['total_sleep'], errors='coerce')
|
23 |
+
|
24 |
+
print(df.info())
|
25 |
+
|
26 |
+
df.describe()
|
27 |
+
|
28 |
+
import seaborn as sns
|
29 |
+
import matplotlib.pyplot as plt
|
30 |
+
|
31 |
+
sns.pairplot(df)
|
32 |
+
plt.show()
|
33 |
+
|
34 |
+
print(df["body_weight"].describe())
|
35 |
+
|
36 |
+
sns.scatterplot(data=df, x="body_weight", y="total_sleep")
|
37 |
+
plt.show()
|
38 |
+
|
39 |
+
import pandas as pd
|
40 |
+
import seaborn as sns
|
41 |
+
import matplotlib.pyplot as plt
|
42 |
+
|
43 |
+
Q1 = df["body_weight"].quantile(0.25)
|
44 |
+
Q3 = df["body_weight"].quantile(0.75)
|
45 |
+
IQR = Q3 - Q1
|
46 |
+
|
47 |
+
lower_bound = Q1 - 1.5 * IQR
|
48 |
+
upper_bound = Q3 + 1.5 * IQR
|
49 |
+
|
50 |
+
print(f"Lower bound: {lower_bound}")
|
51 |
+
print(f"Upper bound: {upper_bound}")
|
52 |
+
|
53 |
+
outliers = df[(df["body_weight"] < lower_bound) | (df["body_weight"] > upper_bound)]
|
54 |
+
print("\n Outliers:")
|
55 |
+
print(outliers)
|
56 |
+
|
57 |
+
filtered_df = df[(df["body_weight"] >= lower_bound) & (df["body_weight"] <=upperbound)]
|
58 |
+
|
59 |
+
sns.scatterplot(data=filtered_df, x="body_weight", y="total_sleep")
|
60 |
+
plt.title("Scatterplot without Outliers")
|
61 |
+
plt.xlabel("Body Weight")
|
62 |
+
plt.ylabel("Total Sleep")
|
63 |
+
plt.grid(True)
|
64 |
+
plt.show()
|
65 |
+
|
66 |
+
print(f"\nOriginal row count: {len(df)}")
|
67 |
+
print(f"Filtered row count: {len(filtered_df)}")
|
68 |
+
|
69 |
+
from sklearn.model_selection import train_test_split
|
70 |
+
|
71 |
+
X = df.drop(columns='total_sleep')
|
72 |
+
y = df['total_sleep']
|
73 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_sixe=0.2, random_state=42)
|