forked from qwert-27/a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPre-processing and Visualization.txt
More file actions
58 lines (45 loc) · 1.61 KB
/
Pre-processing and Visualization.txt
File metadata and controls
58 lines (45 loc) · 1.61 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
# Pre-processing and Visualization of Data
from sklearn.impute import KNNImputer
from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load dataset
data = load_iris()
# Create DataFrame
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
print("Original Data (first 5 rows):")
print(df.head())
# Introduce missing values randomly
np.random.seed(41)
missing_rate = 0.1
n_missing = int(np.floor(missing_rate * df.size))
missing_indices = np.random.choice(df.size, n_missing, replace=False)
# Set random entries to NaN
df.iloc[np.unravel_index(missing_indices, df.shape)] = np.nan
print("\nData with Randomly Missing Values (first 5 rows):")
print(df.head())
# Impute missing values using KNNImputer
knn_imputer = KNNImputer(n_neighbors=3)
df_imputed = pd.DataFrame(knn_imputer.fit_transform(df), columns=df.columns)
print("\nImputed Data (first 5 rows):")
print(df_imputed.head())
# Summary statistics
print("\nSummary Statistics of Original Data:")
print(df.describe())
# Visualization - Histogram for each feature
plt.figure(figsize=(12, 8))
for i, feature in enumerate(df.columns[:-1]):
plt.subplot(2, 2, i + 1)
sns.histplot(df[feature], kde=True, color='blue', label='Original')
plt.legend()
plt.tight_layout()
plt.show()
# Correlation heatmap
corr_matrix = df_imputed.drop(columns=['target']).corr()
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title("Correlation Matrix Heatmap (After Imputation)")
plt.show()