-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDA.py
356 lines (263 loc) · 11.4 KB
/
EDA.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import pandas as pd
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from scipy.stats import chi2_contingency
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix, classification_report
#******************************************load csv file**********************************
path_activity_context_tracking = "dataset/activity_context_tracking_data.csv"
path_activity_context_tracking_clean = "dataset/activity_context_tracking_data_clean.csv"
def get_main_data():
try:
return pd.read_csv(path_activity_context_tracking)
except FileNotFoundError:
print(f"Error: CSV file not found at {path_activity_context_tracking}")
return None
def get_clean_data():
try:
return pd.read_csv(path_activity_context_tracking_clean)
except FileNotFoundError:
print(f"Error: CSV file not found at {path_activity_context_tracking_clean}")
return None
#******************************************load csv file**********************************
#*******************************************Start EDA*******************************************
def pi_chart():
try:
data = get_main_data()
except:
print("Error: Failed to retrieve data")
return
try:
plt.figure(figsize=(8, 6))
activity_counts = data['activity'].value_counts()
wedges, texts, autotexts = plt.pie(activity_counts, labels=None, autopct='%1.1f%%')
plt.title("Pie Chart Activity")
plt.yticks(rotation=90)
plt.yticks(fontsize=12)
legend_labels = [f'{label}: {size.get_text()}' for label, size in zip(activity_counts.index, autotexts)]
plt.legend(legend_labels, loc='upper left', bbox_to_anchor=(1, 0.5), fontsize='small', title="Activity Legend")
plt.savefig('pi.png', dpi=300, bbox_inches='tight')
plt.show()
except:
print("Error: Failed to create pie chart")
def Activity_Frequency():
try:
data = get_main_data()
activity_counts = data["activity"].value_counts()
activity_counts.plot(kind="bar")
plt.title("Activity Frequency")
plt.xlabel("Activity")
plt.ylabel("Frequency")
#plt.savefig('eda22.png', dpi=300, bbox_inches='tight')
plt.show()
print(activity_counts)
except FileNotFoundError:
print("Error: File not found.")
except Exception as e:
print(f"Error: {e}")
def Histograms_of_Each_Column_with_Activity():
data = get_clean_data()
columns = ['orX', 'orY', 'orZ', 'rX', 'rY', 'rZ', 'accX', 'accY', 'accZ', 'gX', 'gY', 'gZ', 'mX', 'mY', 'mZ','lux','soundLevel']
grouped_data = data.groupby('activity')
num_rows = int(np.ceil(len(columns) / 3))
num_cols = min(3, len(columns))
fig, axs = plt.subplots(num_rows, num_cols, figsize=(15, 10))
axs = axs.ravel()
for i, column in enumerate(columns):
for activity, group in grouped_data:
axs[i].hist(group[column], bins=10, alpha=0.5, label=activity)
axs[i].set_title(column)
fig.text(0.5, 0.04, 'Values', ha='center')
fig.text(0.04, 0.5, 'Frequency', va='center', rotation='vertical')
fig.suptitle('Histograms of Each Column with Activity ')
plt.tight_layout()
plt.savefig('Histograms.png', dpi=300, bbox_inches='tight')
plt.show()
def Correlation_Matrix_Heatmap():
try:
df = get_main_data()
columns_for_correlation = ['orX', 'orY', 'orZ', 'rX', 'rY', 'rZ', 'accX', 'accY', 'accZ', 'gX', 'gY', 'gZ', 'mX', 'mY', 'mZ', 'lux', 'soundLevel', 'activity']
correlation_matrix = df[columns_for_correlation].corr()
print(correlation_matrix)
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=0.5)
plt.title('Correlation Matrix Heatmap')
#plt.savefig('Correlation Matrix Heatmap.png', dpi=300, bbox_inches='tight')
plt.show()
except FileNotFoundError:
print("Error: File not found.")
except Exception as e:
print(f"Error: {e}")
def eda_descriptive_statistical_analysis1():
try:
data = get_main_data()
column = ["orX","orY","orZ","rX","rY","rZ","accX","accY","accZ","gX","gY","gZ","mX","mY","mZ","lux", "soundLevel"]
i= 0 ;
for x in column :
col = column[i]
i=i+1
data[col] = pd.to_numeric(data[col], errors='coerce')
mean = np.mean(data[col])
median = np.median(data[col])
std_dev = np.std(data[col])
variance = np.var(data[col])
min_val = np.min(data[col])
max_val = np.max(data[col])
skewness = pd.Series(data[col]).skew()
kurtosis = pd.Series(data[col]).kurtosis()
#print results
print("********************",col,"*********************")
print("column: ",col," Mean:", mean)
print("column: ",col," Median:", median)
print("column: ",col," Standard deviation:", std_dev)
print("column: ",col," Variance:", variance)
print("column: ",col," Minimum:", min_val)
print("column: ",col," Maximum:", max_val)
print("column: ",col," Skewness:", skewness)
print("column: ",col," Kurtosis:", kurtosis)
print("*************************************************")
except Exception as e:
print("An error occurred: ", e)
def create_Average_Magnetic_by_Activity():
try:
data = get_main_data()
magnetic_means = data.groupby("activity")[["mX", "mY", "mZ"]].mean()
magnetic_means.plot(kind="bar")
plt.title("Average Magnetic by Activity")
plt.xlabel("Activity")
plt.ylabel("Magnetic")
#print(magnetic_means)
#plt.savefig('create_Average_Magnetic_by_Activity.png', dpi=300, bbox_inches='tight')
plt.show()
except Exception as e:
print("Error occurred while creating the Average Magnetic by Activity plot:", str(e))
def create_Average_Gyroscope_by_Activity():
try:
data = get_main_data()
gyroscope_means = data.groupby("activity")[["gX", "gY", "gZ"]].mean()
gyroscope_means.plot(kind="bar")
plt.title("Average Gyroscope by Activity")
plt.xlabel("Activity")
plt.ylabel("Gyroscope")
#print(gyroscope_means)
#plt.savefig('create_Average_Gyroscope_by_Activity.png', dpi=300, bbox_inches='tight')
plt.show()
except Exception as e:
print(f"Error occurred: {e}")
def create_Average_Accelerometer_by_Activity():
try:
data = get_main_data()
accelerometer_means = data.groupby("activity")[["accX", "accY", "accZ"]].mean()
accelerometer_means.plot(kind="bar")
plt.title("Average Accelerometer by Activity")
plt.xlabel("Activity")
plt.ylabel("Accelerometer")
#print(accelerometer_means)
#plt.savefig('create_Average_Accelerometer_by_Activity.png', dpi=300, bbox_inches='tight')
plt.show()
except Exception as e:
print("Error:", e)
def create_Average_Rotation_by_Activity():
try:
data = get_main_data()
rotation_means = data.groupby("activity")[["rX", "rY", "rZ"]].mean()
rotation_means.plot(kind="bar")
plt.title("Average Rotation by Activity")
plt.xlabel("Activity")
plt.ylabel("Rotation")
#print(rotation_means)
#plt.savefig('create_Average_Rotation_by_Activity.png', dpi=300, bbox_inches='tight')
plt.show()
except Exception as e:
print("An error occurred:", e)
def create_Average_Orientation_by_Activity():
try:
data = get_main_data()
orientation_means = data.groupby("activity")[["orX", "orY", "orZ"]].mean()
orientation_means.plot(kind="bar")
plt.title("Average Orientation by Activity")
plt.xlabel("Activity")
plt.ylabel("Orientation")
#print(orientation_means)
#plt.savefig('create_Average_Orientation_by_Activity.png', dpi=300, bbox_inches='tight')
plt.show()
except Exception as e:
print(f"An error occurred: {e}")
def check_balance_data():
try:
df = get_main_data()
df['activity'].hist()
plt.title('check balance data')
plt.xlabel('Activity')
plt.ylabel('Frequency')
plt.xticks(rotation=90)
plt.show()
class_freq = df['activity'].value_counts()
print(class_freq)
imbalance_ratio = class_freq.min() / class_freq.max()
print('Imbalance ratio:', imbalance_ratio)
obs = pd.crosstab(df['activity'], columns='count')
chi2, p, dof, expected = chi2_contingency(obs)
print('p-value:', p)
X = df.drop('activity', axis=1)
y = df['activity']
skf = StratifiedKFold(n_splits=5)
scores = []
for train_idx, test_idx in skf.split(X, y):
X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]
clf = LogisticRegression()
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
scores.append(score)
print('Cross-validation scores:', scores)
except Exception as e:
print('Error:', e)
def balance_data_with_smote():
try:
df = get_clean_data()
X = df.drop('activity', axis=1)
y = df['activity']
print("Before balance : ")
print(df['activity'].value_counts())
print("")
smote = SMOTE()
X_balanced, y_balanced = smote.fit_resample(X, y)
print("After balance : ")
print(y_balanced.value_counts())
except Exception as e:
print('Error:', e)
#*******************************************End EDA*******************************************
#*********************************Start Machine Learning***************************************
def clean_data():
try:
print("Start clean data")
data = get_main_data()
#drop the Additional columns
data = data.drop(['_id'], axis=1)
#Remove duplicate rows
data = data.drop_duplicates()
# Remove missing values
data = data.dropna()
#Save file after clean
data.to_csv(path_activity_context_tracking_clean, index=False)
print("finish clean data")
except FileNotFoundError:
print(f"Error: File {path_activity_context_tracking} not found.")
except Exception as e:
print(f"Error: {e}")
#*********************************End Machine Learning***************************************