This repository was archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocess_utils.py
110 lines (98 loc) · 3.77 KB
/
preprocess_utils.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
import numpy as np
from sklearn import preprocessing
def data_preprocess_musk_dd(bags):
for bag in bags:
# preprocess for musk data on dd method according to paper
bag['instances'] /= 100
return bags
def data_preprocess_musk_svm(bags):
bag_labels = list()
instances = list()
for bag in bags:
[n_instances, _] = bag['instances'].shape
if 0 == bag['label']:
bag['label'] = 0
bag['inst_labels'] = np.zeros([n_instances, ])
bag_labels.append(0)
else:
bag['label'] = 1
bag['inst_labels'] = np.ones([n_instances, ])
bag_labels.append(1)
instances.extend(bag['instances'])
# bag['instances'] /= 100
# bag['instances'] = preprocessing.minmax_scale(bag['instances'], axis=1, feature_range=(-1, 1))
# bag['instances'] = preprocessing.normalize(bag['instances'], axis=1)
instances = np.asarray(instances)
instances = preprocessing.minmax_scale(instances, axis=0, feature_range=(-1, 1))
# instances = preprocessing.minmax_scale(instances, axis=0, feature_range=(0, 1))
# instances = preprocessing.normalize(instances, norm='l2', axis=0)
inst_idx = 0
for bag in bags:
[n_instances, _] = bag['instances'].shape
bag['instances'] = instances[inst_idx: inst_idx + n_instances, :]
inst_idx += n_instances
return bags, bag_labels
def normalized_bag(bags):
bag_labels = list()
for bag in bags:
[n_instances, _] = bag['instances'].shape
if 0 == bag['label']:
bag['inst_labels'] = np.zeros([n_instances, ])
bag_labels.append(0)
else:
bag['inst_labels'] = np.ones([n_instances, ])
bag_labels.append(1)
bag['instances'] = preprocessing.normalize(bag['instances'], axis=1)
return bags, bag_labels
def scale_bag(bags):
bag_labels = list()
for bag in bags:
[n_instances, _] = bag['instances'].shape
if 0 == bag['label']:
bag['inst_labels'] = np.zeros([n_instances, ])
bag_labels.append(0)
else:
bag['inst_labels'] = np.ones([n_instances, ])
bag_labels.append(1)
bag['instances'] = preprocessing.minmax_scale(bag['instances'], axis=1, feature_range=(0, 1))
return bags, bag_labels
def normalized_inst(bags):
bag_labels = list()
instances = list()
for bag in bags:
[n_instances, _] = bag['instances'].shape
if 0 == bag['label']:
bag['inst_labels'] = np.zeros([n_instances, ])
bag_labels.append(0)
else:
bag['inst_labels'] = np.ones([n_instances, ])
bag_labels.append(1)
instances.extend(bag['instances'])
instances = np.asarray(instances)
instances = preprocessing.normalize(instances, norm='l2', axis=1)
inst_idx = 0
for bag in bags:
[n_instances, _] = bag['instances'].shape
bag['instances'] = instances[inst_idx: inst_idx + n_instances, :]
inst_idx += n_instances
return bags, bag_labels
def scale_inst(bags):
bag_labels = list()
instances = list()
for bag in bags:
[n_instances, _] = bag['instances'].shape
if 0 == bag['label']:
bag['inst_labels'] = np.zeros([n_instances, ])
bag_labels.append(0)
else:
bag['inst_labels'] = np.ones([n_instances, ])
bag_labels.append(1)
instances.extend(bag['instances'])
instances = np.asarray(instances)
instances = preprocessing.minmax_scale(instances, axis=1, feature_range=(0, 1))
inst_idx = 0
for bag in bags:
[n_instances, _] = bag['instances'].shape
bag['instances'] = instances[inst_idx: inst_idx + n_instances, :]
inst_idx += n_instances
return bags, bag_labels