-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
91 lines (69 loc) · 2.53 KB
/
Copy pathdemo.py
File metadata and controls
91 lines (69 loc) · 2.53 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('task1/healthcare-dataset-stroke-data.csv')
# Display the first few rows of the dataset
print(df.head())
# Display basic information about the dataset
print(df.info())
# Display basic statistics of the dataset
print(df.describe())
# Check for missing values
print("Missing values in each column:")
print(df.isnull().sum())
#check for skewness in 'bmi' column
print("Mean:", df['bmi'].mean())
print("Median:", df['bmi'].median())
print("Skewness:", df['bmi'].skew())
sns.histplot(df['bmi'], kde=True)
plt.title('Distribution of BMI')
plt.show()
# Drop the 'id' column as it is not useful for analysis
df = df.drop('id', axis=1)
# Fill missing values in 'bmi' with the median (because the data is skewed)
df['bmi'] = df['bmi'].fillna(df['bmi'].median())
# Check for missing values again
print("Missing values after filling 'bmi':")
print(df.isnull().sum()) # done
print(df.select_dtypes(include='object').columns)
# Convert categorical variables to numerical using one-hot encoding
df = pd.get_dummies(df, columns=['gender', 'ever_married', 'work_type', 'Residence_type', 'smoking_status'], drop_first=True, dtype=int)
# Display the first few rows after encoding
print(df.head())
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# # Fit and transform selected columns
df[['age', 'avg_glucose_level', 'bmi']] = scaler.fit_transform(df[['age', 'avg_glucose_level', 'bmi']])
# Display the first few rows after scaling
print(df.head())
import matplotlib.pyplot as plt
import seaborn as sns
numerical_cols = ['age', 'avg_glucose_level', 'bmi']
plt.figure(figsize=(15, 4))
for i, col in enumerate(numerical_cols, 1):
plt.subplot(1, 3, i)
sns.boxplot(data=df, y=col)
plt.title(f'Boxplot of {col}')
plt.tight_layout()
plt.show()
def remove_outliers(df, column):
Q1 = df[column].quantile(0.25)
Q3 = df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]
# Remove outliers from each column
for col in numerical_cols:
df = remove_outliers(df, col)
numerical_cols = ['age', 'avg_glucose_level', 'bmi']
# Plot boxplots after removing outliers
plt.figure(figsize=(15, 4))
for i, col in enumerate(numerical_cols, 1):
plt.subplot(1, 3, i)
sns.boxplot(data=df, y=col)
plt.title(f'Boxplot of {col}')
plt.tight_layout()
plt.show()
df.to_csv('task1/cleaned_stroke_data.csv', index=False)