-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_data.py
More file actions
52 lines (45 loc) · 1.93 KB
/
Copy pathsplit_data.py
File metadata and controls
52 lines (45 loc) · 1.93 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
import numpy as np
from sklearn.decomposition import PCA
def split_data(gt_reshape, class_num, train_ratio, val_ratio, train_num, val_num, samples_type):
train_index = []
test_index = []
val_index = []
if samples_type == 'ratio':
# class_num = 16 类
for i in range(class_num):
idx = np.where(gt_reshape == i + 1)[-1]
samplesCount = len(idx)
# print("Class ",i,":", samplesCount)
train_num = np.ceil(samplesCount * train_ratio).astype('int32')
val_num = np.ceil(samplesCount * val_ratio).astype('int32')
np.random.shuffle(idx)
train_index.append(idx[:train_num])
val_index.append(idx[train_num:train_num+val_num])
test_index.append(idx[train_num+val_num:])
else:
sample_num = train_num
# class_num = 16 类
for i in range(class_num):
idx = np.where(gt_reshape == i + 1)[-1]
samplesCount = len(idx)
# print("Class ",i,":", samplesCount) # 每一类的个数
max_index = np.max(samplesCount) + 1
np.random.shuffle(idx)
if sample_num > max_index:
sample_num = 10
else:
sample_num = train_num
# 取出每个类别选择出的训练集
train_index.append(idx[: sample_num])
val_index.append(idx[sample_num : sample_num+val_num])
test_index.append(idx[sample_num+val_num : ])
train_index = np.concatenate(train_index, axis=0)
val_index = np.concatenate(val_index, axis=0)
test_index = np.concatenate(test_index, axis=0)
return train_index, val_index, test_index
def apply_PCA(X, num_components=75):
newX = np.reshape(X, (-1, X.shape[2]))
pca = PCA(n_components=num_components, whiten=True)
newX = pca.fit_transform(newX)
newX = np.reshape(newX, (X.shape[0],X.shape[1], num_components))
return newX, pca