-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_load.py
More file actions
64 lines (53 loc) · 1.78 KB
/
data_load.py
File metadata and controls
64 lines (53 loc) · 1.78 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
import numpy as np
import scipy.io as scio
import sklearn
from sklearn import preprocessing
def dataset_pro(data_name, methods):
"""
:param data_name: .m file, 'X': n,d; 'Y': n,1
:return: 'X': n,d; 'Y': n,1
"""
data_old = scio.loadmat(r'./dataset/'+ data_name + '.mat')
label = data_old["Y"].astype('int') # n 1
unique_label = np.unique(label)
classes = unique_label.shape[0]
if methods == 'minmax':
minmaxscaler = sklearn.preprocessing.MinMaxScaler()
x = minmaxscaler.fit_transform(data_old["X"])
elif methods == 'scale':
x = preprocessing.scale(data_old["X"]) # n d
else:
x = np.array(data_old["X"])
return x, label.reshape((label.shape[0],)), classes
def feature_ranking(w):
"""
This function ranks features according to the feature weights matrix W
Input:
-----
W: {numpy array}, shape (n_features, n_classes)
feature weights matrix
Output:
------
idx: {numpy array}, shape {n_features,}
feature index ranked in descending order by feature importance
"""
t = (w * w).sum(1)
idx = np.argsort(t, 0)
return idx[::-1]
def construct_label_matrix(label):
"""
This function converts a 1d numpy array to a 2d array, for each instance, the class label is 1 or 0
Input:
-----
label: {numpy array}, shape(n_samples,)
Output:
------
label_matrix: {numpy array}, shape(n_samples, n_classes)
"""
n_samples = label.shape[0]
unique_label = np.unique(label)
n_classes = unique_label.shape[0]
label_matrix = np.zeros((n_samples, n_classes))
for i in range(n_classes):
label_matrix[label == unique_label[i], i] = 1
return label_matrix.astype(int)