-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
185 lines (151 loc) · 5.69 KB
/
analysis.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
import pandas as pd
from constants import output_file, y_name, path_name, seed
from matplotlib import pyplot as plt
import seaborn as sns, numpy as np
from sklearn.base import TransformerMixin
from xgboost import XGBClassifier
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
# df = pd.read_csv(output_file)
# features = list(df.columns)
# for c in [y_name, path_name]:
# features.remove(c)
# X = df[features]
# y = df[y_name]
# class_0 = df[df[y_name] == 0]
# class_1 = df[df[y_name] == 1]
# print(f"Class 0: {class_0.shape}, Class 1: {class_1.shape}")
# df.drop(columns=[y_name, path_name], inplace=True)
class ColumnWiseOutlierClipper(TransformerMixin):
def __init__(self, lower_percentile=1, upper_percentile=99):
self.lower_percentile = lower_percentile
self.upper_percentile = upper_percentile
def fit(self, X, y=None):
self.lower_bounds_ = X.apply(lambda col: np.percentile(col, self.lower_percentile), axis=0)
self.upper_bounds_ = X.apply(lambda col: np.percentile(col, self.upper_percentile), axis=0)
return self
def transform(self, X):
X_clipped = X.copy()
for col in X.columns:
X_clipped[col] = np.clip(X_clipped[col], self.lower_bounds_[col], self.upper_bounds_[col])
return X_clipped
def print_correlation():
'''
Dalla matrice di correlazione possono essere rimosse queste colonne:
mfcc_mean_4, mfcc_mean_6, gfcc_mean_2, mfcc_mean_8, mfcc_mean_7, mfcc_mean_10
:return:
'''
# Spearman per non essere soggetti agli outlier
corr_matrix = df.corr(method='spearman').round(2)
plt.figure(figsize=(25, 25))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', linewidths=0.5)
plt.title('Matrice di Correlazione')
plt.savefig("Correlation.png")
def print_outliers(df, fn=None):
'''
Dal grafico si vede che ci sono molti outliers. Potrebbe valer la pena trasformarli
'''
plt.figure(figsize=(30, 8))
sns.boxplot(data=df)
plt.title('Boxplot delle variabili')
if fn is None:
plt.savefig("BoxPlot.png")
else:
plt.savefig(fn)
def print_kde():
plt.figure(figsize=(10, 6))
sns.kdeplot(df['mfcc_mean_0'], shade=True)
plt.title('KDE Plot mfcc_mean_0')
plt.xlabel('Valori')
plt.ylabel('Densità')
plt.show()
def remove_outliers(q1=0.25, q3=0.75):
'''
Dallo studio degli outlier la colonna mfcc_mean_1 può essere levata in quanto molto distorta
'''
df = pd.read_csv(output_file)
df.drop(columns=[y_name, path_name], inplace=True)
for col in df.columns:
Q1 = df[col].quantile(q1)
Q3 = df[col].quantile(q3)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
df = df[(df[col] >= lower_bound) & (df[col] <= upper_bound)]
return df
def compute_importance():
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=seed)
model = XGBClassifier(use_label_encoder=False, verbosity=2, seed=seed)
model.fit(X_train, y_train)
print("Computing Feature Importance")
importance = model.feature_importances_
feature_names = X.columns
importance_df = pd.DataFrame({'Feature': feature_names, 'Importance': importance})
importance_df = importance_df.sort_values(by='Importance', ascending=False)
plt.figure(figsize=(12, 8))
plt.title("Feature Importance")
plt.barh(importance_df['Feature'], importance_df['Importance'], color="b", align="center")
plt.xlabel('Importance')
plt.gca().invert_yaxis()
plt.tight_layout()
plt.savefig("feature_importance.png")
print("Computing Permutation Importance")
result = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=seed, n_jobs=-1)
importances = result.importances_mean
std = result.importances_std
indices = np.argsort(importances)[::-1]
perm_df = pd.DataFrame({'Feature': X.columns[indices], 'Importance': importances[indices]})
plt.figure(figsize=(12, 8))
plt.title("Permutation Importance delle Feature")
plt.bar(range(X.shape[1]), importances[indices], color="r", yerr=std[indices], align="center")
plt.xticks(range(X.shape[1]), X.columns[indices], rotation=90)
plt.xlim([-1, X.shape[1]])
plt.tight_layout()
plt.savefig("permutation_importance.png")
return importance_df, perm_df
# importance_df, permutation_df = compute_importance()
# neg_importance = permutation_df[permutation_df['Importance'] < 0]
# print(neg_importance)
# print(df.var())
# print_correlation()
# print_outliers(df)
# print_kde()
# print(df.shape)
# new_df = remove_outliers(q1=0.1, q3=0.9)
# print(new_df.shape)
# print_outliers(df)
# clipper = ColumnWiseOutlierClipper(lower_percentile=2.5, upper_percentile=97.5)
# df_clipped = clipper.fit_transform(df)
# print_outliers(df_clipped, "box_cox_clipped.png")
import pandas as pd
from sklearn.metrics import accuracy_score, precision_score, recall_score
df = pd.read_csv('test_result.csv')
# Estrai le colonne rilevanti
y_true = df['true_label']
y_pred = df['predicted_label']
# Calcolo dell'accuracy
accuracy = accuracy_score(y_true, y_pred)
# Calcolo della precisione
precision = precision_score(y_true, y_pred)
# Calcolo del recall
recall = recall_score(y_true, y_pred)
# Stampa i risultati
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
'''
df = pd.read_csv("test_result.csv")
paths = df["path"].values
leg = []
mal = []
for path in paths:
path_list = path.split("/")
class_ = path_list[1]
file_ = path_list[2]
if class_ == "0":
leg.append(file_)
else:
mal.append(file_)
print("Leg", len(leg))
print("Mal", len(mal))
'''