-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
68 lines (56 loc) · 2.23 KB
/
visualization.py
File metadata and controls
68 lines (56 loc) · 2.23 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
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def plot_pairplot(data_path='data/X_train.csv', target_path='data/y_train.csv'):
"""
Plots a pairplot of the training data.
"""
X_train = pd.read_csv(data_path)
y_train = pd.read_csv(target_path)
# Reset index to ensure proper concatenation
X_train = X_train.reset_index(drop=True)
y_train = y_train.reset_index(drop=True)
df = pd.concat([X_train, y_train], axis=1)
# Add a temporary column for species names for better legend
iris_species = {0: 'setosa', 1: 'versicolor', 2: 'virginica'}
df['species'] = df['target'].map(iris_species)
sns.pairplot(df, hue='species', diag_kind='kde')
plt.suptitle('Pair Plot of Iris Dataset', y=1.02)
plt.show()
def plot_histograms(data_path='data/X_train.csv'):
"""
Plots histograms of each feature in the training data.
"""
X_train = pd.read_csv(data_path)
X_train.hist(figsize=(12, 10), bins=15, edgecolor='black')
plt.suptitle('Histograms of Iris Features')
plt.tight_layout(rect=[0, 0, 1, 0.96])
plt.show()
def plot_boxplots(data_path='data/X_train.csv', target_path='data/y_train.csv'):
"""
Plots box plots of each feature for each target class.
"""
X_train = pd.read_csv(data_path)
y_train = pd.read_csv(target_path)
# Reset index to ensure proper concatenation
X_train = X_train.reset_index(drop=True)
y_train = y_train.reset_index(drop=True)
df = pd.concat([X_train, y_train], axis=1)
# Add a temporary column for species names for better legend
iris_species = {0: 'setosa', 1: 'versicolor', 2: 'virginica'}
df['species'] = df['target'].map(iris_species)
plt.figure(figsize=(15, 10))
for i, feature in enumerate(X_train.columns):
plt.subplot(2, 3, i + 1)
sns.boxplot(x='species', y=feature, data=df)
plt.suptitle('Box Plots of Iris Features by Species')
plt.tight_layout(rect=[0, 0, 1, 0.96])
plt.show()
if __name__ == '__main__':
# Generate plots if the script is run directly
print("Generating pair plot...")
plot_pairplot()
print("Generating histograms...")
plot_histograms()
print("Generating box plots...")
plot_boxplots()