Upload _1402.py
Browse files
_1402.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
""".1402
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1-MyRdtV24jTgS9BfGaWSD_PPnzPW5R3E
|
8 |
+
"""
|
9 |
+
|
10 |
+
import os
|
11 |
+
import pandas as pd
|
12 |
+
import numpy as np
|
13 |
+
import seaborn as sns
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
import warnings
|
16 |
+
warnings.filterwarnings('ignore')
|
17 |
+
|
18 |
+
data = pd.read_csv('/content/psychological_state_dataset.csv')
|
19 |
+
|
20 |
+
data.sample(20)
|
21 |
+
|
22 |
+
data.info()
|
23 |
+
|
24 |
+
numerical_columns = data.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
25 |
+
categorical_columns = data.select_dtypes(include=['object']).columns.tolist()
|
26 |
+
|
27 |
+
print("Numerical Columns:", numerical_columns)
|
28 |
+
print("Categorical Columns:", categorical_columns)
|
29 |
+
|
30 |
+
for col in categorical_columns:
|
31 |
+
print(f"Value counts for {col}:\n{data[col].value_counts()}\n")
|
32 |
+
|
33 |
+
for col in numerical_columns:
|
34 |
+
sns.histplot(data[col], kde=True, bins=30)
|
35 |
+
plt.title(f'Distribution of {col}')
|
36 |
+
plt.show()
|
37 |
+
|
38 |
+
for col in numerical_columns:
|
39 |
+
sns.boxplot(x=data[col])
|
40 |
+
plt.title(f'Boxplot of {col}')
|
41 |
+
plt.show()
|
42 |
+
|
43 |
+
for col in categorical_columns:
|
44 |
+
sns.countplot(x=data[col], order=data[col].value_counts().index)
|
45 |
+
plt.title(f'Distribution of {col}')
|
46 |
+
plt.xticks(rotation=45)
|
47 |
+
plt.show()
|
48 |
+
|
49 |
+
corr_matrix = data[numerical_columns].corr()
|
50 |
+
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f')
|
51 |
+
plt.title('Correlation Heatmap')
|
52 |
+
plt.show
|
53 |
+
|
54 |
+
sns.pairplot(data[numerical_columns])
|
55 |
+
plt.show()
|
56 |
+
|
57 |
+
for cat_col in categorical_columns:
|
58 |
+
if data[cat_col].nunique() < 5:
|
59 |
+
g = sns.FacetGrid(data, col=cat_col, sharey=False)
|
60 |
+
g.map(sns.histplot, "Skin Temp (°C)")
|
61 |
+
plt.show()
|
62 |
+
|
63 |
+
if 'Time' in categorical_columns:
|
64 |
+
data['Time'] = pd.to_datetime(data['Time'])
|
65 |
+
|
66 |
+
for col in numerical_columns:
|
67 |
+
sns.lineplot(x=data['Time'], y=data[col])
|
68 |
+
plt.title(f'{col} Over Time')
|
69 |
+
plt.show()
|
70 |
+
|
71 |
+
for col in ['Mood State', 'Psychological State']:
|
72 |
+
if col in categorical_columns:
|
73 |
+
sns.countplot(x=col, data=data, order=data[col].value_counts().index)
|
74 |
+
plt.title(f'{col} Distribution')
|
75 |
+
plt.show()
|
76 |
+
|
77 |
+
from math import pi
|
78 |
+
|
79 |
+
radar_data = data[numerical_columns].mean()
|
80 |
+
categories = radar_data.index
|
81 |
+
values = radar_data.values.tolist()
|
82 |
+
values += values[:1]
|
83 |
+
|
84 |
+
angles = [n / float(len(categories)) * 2 * pi for n in range(len(categories))]
|
85 |
+
angles += angles[:1]
|
86 |
+
|
87 |
+
plt.figure(figsize=(8, 8))
|
88 |
+
ax = plt.subplot(111, polar=True)
|
89 |
+
plt.xticks(angles[:-1], categories)
|
90 |
+
|
91 |
+
ax.plot(angles, values, linewidth=2, linestyle='solid')
|
92 |
+
ax.fill(angles, values, alpha=0.4)
|
93 |
+
plt.title('Radar Chart of Numerical Data')
|
94 |
+
plt.show()
|
95 |
+
|
96 |
+
if 'Cognitive Load' in numerical_columns:
|
97 |
+
sns.scatterplot(x=data['Age'], y=data['Focus Duration (s)'], size=data['Cognitive Load'], sizes=(20, 200))
|
98 |
+
|
99 |
+
for cat_col in categorical_columns:
|
100 |
+
grouped_stats = data.groupby(cat_col)[numerical_columns].mean()
|
101 |
+
print(f"Grouped statistics for {cat_col}:\n", grouped_stats)
|
102 |
+
|
103 |
+
from sklearn.cluster import KMeans
|
104 |
+
from sklearn.preprocessing import StandardScaler
|
105 |
+
from sklearn.decomposition import PCA
|
106 |
+
|
107 |
+
scaler = StandardScaler()
|
108 |
+
scaled_data = scaler.fit_transform(data[numerical_columns])
|
109 |
+
|
110 |
+
kmeans = KMeans(n_clusters=3, random_state=42)
|
111 |
+
data['Cluster'] = kmeans.fit_predict(scaled_data)
|
112 |
+
|
113 |
+
pca = PCA(n_components=2)
|
114 |
+
pca_data = pca.fit_transform(scaled_data)
|
115 |
+
|
116 |
+
plt.figure(figsize=(10, 6))
|
117 |
+
sns.scatterplot(x=pca_data[:, 0], y=pca_data[:, 1], hue=data['Cluster'], palette='viridis', s=100)
|
118 |
+
plt.title('K-Means Clusters Visualized in 2D')
|
119 |
+
plt.xlabel('PCA Component 1')
|
120 |
+
plt.ylabel('PCA Component 2')
|
121 |
+
plt.show()
|
122 |
+
|
123 |
+
from scipy.cluster.hierarchy import linkage, dendrogram
|
124 |
+
|
125 |
+
linked = linkage(scaled_data, method='ward')
|
126 |
+
|
127 |
+
plt.figure(figsize=(12, 6))
|
128 |
+
dendrogram(linked, orientation='top', distance_sort='descending', show_leaf_counts=False)
|
129 |
+
plt.title('Hierarchiacal Clustering Dendrogram')
|
130 |
+
plt.xlabel('Samples')
|
131 |
+
plt.ylabel('Distance')
|
132 |
+
plt.show()
|
133 |
+
|
134 |
+
from sklearn.manifold import TSNE
|
135 |
+
|
136 |
+
tsne = TSNE(n_components=2, random_state=42, perplexity=30, n_iter=300)
|
137 |
+
tsne_results = tsne.fit_transform(scaled_data)
|
138 |
+
|
139 |
+
plt.figure(figsize=(10, 6))
|
140 |
+
sns.scatterplot(x=tsne_results[:, 0], y=tsne_results[:,1], hue=data['Cluster'], palette='coolwarm', s=100)
|
141 |
+
plt.title('T-SNE Clustering Visualization')
|
142 |
+
plt.xlabel('TSNE Component 1')
|
143 |
+
plt.ylabel('TSNE Component 2')
|
144 |
+
plt.show()
|
145 |
+
|
146 |
+
for cat_col in categorical_columns:
|
147 |
+
grouped_data = data.groupby(cat_col)[numerical_columns].mean()
|
148 |
+
plt.figure(figsize=(12, 6))
|
149 |
+
sns.heatmap(grouped_data, annot=True, fmt=".2f", cmap='coolwarm')
|
150 |
+
plt.title(f'Mean Values of Numerical Features by {cat_col}')
|
151 |
+
plt.ylabel(cat_col)
|
152 |
+
plt.xlabel('Numerical Features')
|
153 |
+
plt.show()
|
154 |
+
|
155 |
+
from pandas.plotting import parallel_coordinates
|
156 |
+
|
157 |
+
parallel_data = data[numerical_columns].copy()
|
158 |
+
parallel_data['Cluster'] = data['Cluster']
|
159 |
+
|
160 |
+
plt.figure(figsize=(12, 6))
|
161 |
+
parallel_coordinates(parallel_data, class_column='Cluster', colormap='viridis')
|
162 |
+
plt.title('Parallel Coordinates Plot by Clusters')
|
163 |
+
plt.xticks(rotation=45)
|
164 |
+
plt.show()
|
165 |
+
|
166 |
+
from sklearn.model_selection import train_test_split
|
167 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
168 |
+
from sklearn.ensemble import RandomForestClassifier
|
169 |
+
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, accuracy_score
|
170 |
+
|
171 |
+
data_encoded = data.copy()
|
172 |
+
for col in categorical_columns:
|
173 |
+
le = LabelEncoder()
|
174 |
+
data_encoded[col] = le.fit_transform(data[col])
|
175 |
+
|
176 |
+
X = data_encoded.drop(['Cognitive Load'], axis=1)
|
177 |
+
y = data_encoded['Cognitive Load']
|
178 |
+
|
179 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
180 |
+
|
181 |
+
scaler = StandardScaler()
|
182 |
+
X_train[numerical_columns] = scaler.fit_transform(X_train[numerical_columns])
|
183 |
+
X_test[numerical_columns] = scaler.transform(X_test[numerical_columns])
|
184 |
+
|
185 |
+
clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
186 |
+
clf.fit(X_train, y_train)
|
187 |
+
|
188 |
+
y_pred = clf.predict(X_test)
|
189 |
+
|
190 |
+
print("Classification Report:")
|
191 |
+
print(classification_report(y_test, y_pred))
|
192 |
+
|
193 |
+
sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d')
|
194 |
+
|
195 |
+
accuracy = accuracy_score(y_test, y_pred)
|
196 |
+
print(f"accuracy_score: {accuracy * 100:.2f}%")
|
197 |
+
|
198 |
+
feature_importances = pd.DataFrame({'Feature': X.columns, 'Importance': clf.feature_importances_})
|
199 |
+
feature_importances = feature_importances.sort_values(by='Importance', ascending=False)
|
200 |
+
|
201 |
+
plt.figure(figsize=(10, 6))
|
202 |
+
sns.barplot(x='Importance', y='Feature', data=feature_importances)
|
203 |
+
plt.title('Feature Importance')
|
204 |
+
plt.show()
|