forked from microsoft/muzic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreshold.py
211 lines (197 loc) · 10.1 KB
/
threshold.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
import random
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os
import warnings
from sklearn.manifold import TSNE
from scipy.stats import pearsonr
from sklearn.feature_selection import SelectKBest, f_classif, VarianceThreshold
# Import libraries
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, roc_auc_score
import joblib
import lightgbm as lgb
# 从数据集中选取feature的阈值
def EMOPIA_threshold():
feature_table = np.load(r"../../StyleCtrlData/jSymbolic_lib\datasets\EMOPIA\data\feature_table.npy")
X, Y = feature_table[:, :-1], feature_table[:, -1]
# for i in range(X.shape[1]):
# medium_value = np.percentile(X[:, i], 50)
# thresholds.append(medium_value)
# X_discrete.append(np.searchsorted([medium_value], X[:, i])[:, np.newaxis])
for bucket_num in [3, 5, 8, 12, 16]:
thresholds = []
for i in range(X.shape[1]):
thres = []
for j in range(1, bucket_num):
thres.append(np.percentile(X[:, i], 100.0/bucket_num*j))
thresholds.append(thres)
# X_discrete.append(np.searchsorted(thres, X[:, i])[:, np.newaxis])
thresholds = np.array(thresholds)
# X_discrete = np.concatenate(X_discrete, axis = 1)
# cv_folds = 5
# skf = StratifiedKFold(n_splits=cv_folds, random_state=2022, shuffle=True)
# y_preds = np.zeros(Y.shape) - 1
# for train_index, test_index in skf.split(X_discrete, Y):
# X_train, y_train = X_discrete[train_index], Y[train_index]
# X_test, y_test = X_discrete[test_index], Y[test_index]
# clf = RandomForestClassifier(n_estimators=100, random_state=2022)
# clf.fit(X_train, y_train)
# y_pred_score = clf.predict_proba(X_test)
# y_pred = np.argmax(y_pred_score, axis=1)
# y_pred += 1 # 预测第几个象限
# y_preds[test_index] = y_pred
# print(f"离散化后的{X_discrete.shape[1]}维特征:", f1_score(Y, y_preds, average="micro"))
np.save(f"../../StyleCtrlData/jSymbolic_lib/datasets/EMOPIA/data/threshold_{bucket_num}.npy", thresholds)
# 二值化之后的分类精度为0.6762523191094619
# feature_selected = np.load("./data/selected_feature/emotion_And_select_17.npy")
# feature_name = np.load("./data/feature_name.npy")
# name2index = dict(zip(feature_name, range(len(feature_name))))
# feature_selected_index = [name2index[i] for i in feature_selected]
# X_discrete = X_discrete[:, feature_selected_index]
# y_preds = np.zeros(Y.shape) - 1
# for train_index, test_index in skf.split(X_discrete, Y):
# X_train, y_train = X_discrete[train_index], Y[train_index]
# X_test, y_test = X_discrete[test_index], Y[test_index]
# clf = RandomForestClassifier(n_estimators=100, random_state=2022)
# clf.fit(X_train, y_train)
# y_pred_score = clf.predict_proba(X_test)
# y_pred = np.argmax(y_pred_score, axis=1)
# y_pred += 1 # 预测第几个象限
# y_preds[test_index] = y_pred
# print(f"离散化后的{X_discrete.shape[1]}维特征:", f1_score(Y, y_preds, average="micro"))
# 分为两个桶:
# 离散化后的1495维特征: 0.6762523191094619
# 离散化后的39维特征: 0.62430426716141
# 离散化后的17维特征: 0.5092764378478665
# 分为三个桶
# 离散化后的1495维特征: 0.6725417439703154
# 离散化后的39维特征: 0.6447124304267161
# 离散化后的17维特征: 0.5445269016697588
def VGMIDI_feedback():
threshold = np.load("./datasets/EMOPIA/data/threshold_2.npy")
feature_table = np.load("./datasets/VGMIDI/data/feature_table.npy")
X = feature_table[:, :-1]
Y = feature_table[:, -1]
X_discrete = []
for i in range(X.shape[1]):
X_discrete.append(np.searchsorted([threshold[i]], X[:,i])[:, np.newaxis])
X_discrete = np.concatenate(X_discrete, axis = 1)
cv_folds = 5
skf = StratifiedKFold(n_splits=cv_folds, random_state=2022, shuffle=True)
y_preds_continuous = np.zeros(Y.shape) - 1
for train_index, test_index in skf.split(X, Y):
X_train, y_train = X[train_index], Y[train_index]
X_test, y_test = X[test_index], Y[test_index]
clf = RandomForestClassifier(n_estimators=100, random_state=2022)
clf.fit(X_train, y_train)
y_pred_score = clf.predict_proba(X_test)
y_pred = np.argmax(y_pred_score, axis=1)
y_pred += 1 # 预测第几个象限
y_preds_continuous[test_index] = y_pred
print(f1_score(Y, y_preds_continuous, average="micro"))
def TopMAGD_threshold():
feature_table = []
labels = []
path_root = r"../..\StyleCtrlData\data\1004_TopMAGD\truncated_2560\split_data"
for split in ["train","valid", "test"]:
feature_table.append(np.load(path_root + f"/1495/{split}_raw_command_1495.npy"))
labels.append(np.load(path_root + f"/{split}_style_labels.npy"))
feature_table = np.vstack(feature_table)
labels = np.vstack(labels)
X = feature_table
Y = labels
feature_selected = np.load(r"E:\Music\Project\StyleCtrl\StyleCtrlData\jSymbolic_lib\data\style_select_feature\style_or_10_select_103.npy")
feature_names = np.load(r"E:\Music\Project\StyleCtrl\StyleCtrlData\jSymbolic_lib\data\feature_name.npy")
feature2id = dict(zip(feature_names, range(len(feature_names))))
select_features = [feature2id[i] for i in feature_selected]
id2genre = np.load(r"E:\Music\Project\StyleCtrl\StyleCtrlData\data\1004_TopMAGD\truncated_2560\split_data\id2genre.npy")
X = X[:, select_features]
# 不离散化的feature
cv_folds = 5
skf = StratifiedKFold(n_splits=cv_folds, random_state=2022, shuffle=True)
y_preds = np.zeros(Y.shape) - 1
for index, style_name in enumerate(id2genre):
Y_style_label = Y[:, index]
for train_index, test_index in skf.split(X, Y_style_label):
X_train, y_train = X[train_index], Y_style_label[train_index]
X_test, y_test = X[test_index], Y_style_label[test_index]
clf = RandomForestClassifier(n_estimators=100, random_state=2022)
clf.fit(X_train, y_train)
y_pred_score = clf.predict_proba(X_test)
y_pred = np.argmax(y_pred_score, axis=1)
y_pred += 1 # 预测第几个象限
y_preds[test_index] = y_pred
print(f"连续的{X.shape[1]}维特征:", style_name, f1_score(Y_style_label, y_preds, average="micro"), roc_auc_score(Y_style_label, y_preds))
X_discrete = []
for bucket_num in [2, 3, 5, 8, 12, 16]:
thresholds = []
for i in range(X.shape[1]):
thres = []
for j in range(1, bucket_num):
thres.append(np.percentile(X[:, i], 100.0 / bucket_num * j))
thresholds.append(thres)
X_discrete.append(np.searchsorted(thres, X[:, i])[:, np.newaxis])
# thresholds = np.array(thresholds)
X_discrete = np.concatenate(X_discrete, axis = 1)
cv_folds = 5
skf = StratifiedKFold(n_splits=cv_folds, random_state=2022, shuffle=True)
y_preds = np.zeros(Y.shape) - 1
for train_index, test_index in skf.split(X_discrete, Y):
X_train, y_train = X_discrete[train_index], Y[train_index]
X_test, y_test = X_discrete[test_index], Y[test_index]
clf = RandomForestClassifier(n_estimators=100, random_state=2022)
clf.fit(X_train, y_train)
y_pred_score = clf.predict_proba(X_test)
y_pred = np.argmax(y_pred_score, axis=1)
y_pred += 1 # 预测第几个象限
y_preds[test_index] = y_pred
print(f"离散化后的{X_discrete.shape[1]}维特征:", f1_score(Y, y_preds, average="micro"), roc_auc_score(Y, y_preds))
# np.save(path_root + f"/threshold_{bucket_num}.npy", thresholds)
# 二值化之后的分类精度为0.6762523191094619
# feature_selected = np.load("./data/selected_feature/emotion_And_select_17.npy")
# feature_name = np.load("./data/feature_name.npy")
# name2index = dict(zip(feature_name, range(len(feature_name))))
# feature_selected_index = [name2index[i] for i in feature_selected]
# X_discrete = X_discrete[:, feature_selected_index]
# y_preds = np.zeros(Y.shape) - 1
# for train_index, test_index in skf.split(X_discrete, Y):
# X_train, y_train = X_discrete[train_index], Y[train_index]
# X_test, y_test = X_discrete[test_index], Y[test_index]
# clf = RandomForestClassifier(n_estimators=100, random_state=2022)
# clf.fit(X_train, y_train)
# y_pred_score = clf.predict_proba(X_test)
# y_pred = np.argmax(y_pred_score, axis=1)
# y_pred += 1 # 预测第几个象限
# y_preds[test_index] = y_pred
# print(f"离散化后的{X_discrete.shape[1]}维特征:", f1_score(Y, y_preds, average="micro"))
def YMDB_threshold():
feature_table = np.load(r"../../StyleCtrlData/jSymbolic_lib/datasets/1224_YMDB/data/fea_table.npy")
X = feature_table
# X, Y = feature_table[:, :-1], feature_table[:, -1]
# for i in range(X.shape[1]):
# medium_value = np.percentile(X[:, i], 50)
# thresholds.append(medium_value)
# X_discrete.append(np.searchsorted([medium_value], X[:, i])[:, np.newaxis])
for bucket_num in [2, 3, 5, 8, 12, 16]:
thresholds = []
for i in range(X.shape[1]):
thres = []
for j in range(1, bucket_num):
thres.append(np.percentile(X[:, i], 100.0 / bucket_num * j))
thresholds.append(thres)
# X_discrete.append(np.searchsorted(thres, X[:, i])[:, np.newaxis])
thresholds = np.array(thresholds)
np.save(f"../../StyleCtrlData/jSymbolic_lib/datasets/1224_YMDB/data/threshold_{bucket_num}.npy", thresholds)
if __name__ == "__main__":
# EMOPIA_threshold()
# TopMAGD_threshold()
YMDB_threshold()