-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.cpp
125 lines (106 loc) · 2.76 KB
/
Sample.cpp
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
#include "Sample.h"
Sample::Sample(float** datas, int* labels, int sampleNum, int featureNum) {
_dataset = datas;
_labels = labels;
_sampleIndex = NULL;
_featureIndex = NULL;
_sampleNum = sampleNum;
_featureNum = featureNum;
_selectedSampleNum = 0;
_selectedFeatureNum = 0;
}
Sample::Sample(Sample* sample, int start, int end) {
_sampleNum = sample->_sampleNum;
_featureNum = sample->_featureNum;
_selectedSampleNum = end - start + 1;
_selectedFeatureNum = sample->_selectedFeatureNum;
_dataset = sample->_dataset;
_labels = sample->_labels;
if (sample->_sampleIndex != NULL) {
_sampleIndex = new int[_selectedSampleNum];
for (int i = 0; i < _selectedSampleNum; i++) {
_sampleIndex[i] = sample->_sampleIndex[start+i];
}
} else {
_sampleIndex = NULL;
}
if (sample->_featureIndex != NULL) {
_featureIndex = new int[_selectedFeatureNum];
for (int i = 0; i < _selectedFeatureNum; i++) {
_featureIndex[i] = sample->_featureIndex[i];
}
} else {
_featureIndex = NULL;
}
}
Sample::Sample(Sample *sample) {
_sampleNum = sample->_sampleNum;
_featureNum = sample->_featureNum;
_selectedSampleNum = sample->_selectedSampleNum;
_selectedFeatureNum = sample->_selectedFeatureNum;
_dataset = sample->_dataset;
_labels = sample->_labels;
if (sample->_sampleIndex != NULL) {
_sampleIndex = new int[_selectedSampleNum];
for (int i = 0; i < _selectedSampleNum; i++) {
_sampleIndex[i] = sample->_sampleIndex[i];
}
} else {
_sampleIndex = NULL;
}
if (sample->_featureIndex != NULL) {
_featureIndex = new int[_selectedFeatureNum];
for (int i = 0 ; i < _selectedFeatureNum; i++) {
_featureIndex[i] = sample->_featureIndex[i];
}
} else {
_featureIndex = NULL;
}
}
Sample::~Sample() {
_dataset = NULL;
_labels = NULL;
if (_sampleIndex != NULL) {
delete [] _sampleIndex;
_sampleIndex = NULL;
}
if (_featureIndex != NULL) {
delete [] _featureIndex;
_featureIndex = NULL;
}
}
void Sample::randomSelectSample(int selectedSampleNum) {
_selectedSampleNum = selectedSampleNum;
if (_sampleIndex != NULL) {
delete [] _sampleIndex;
}
_sampleIndex = new int[selectedSampleNum];
srand(time(NULL));
for (int i = 0; i < selectedSampleNum; i++) {
_sampleIndex[i] = rand()%_sampleNum;
}
}
void Sample::randomSelectFeature(int selectedFeatureNum) {
_selectedFeatureNum = selectedFeatureNum;
if (_featureIndex != NULL) {
delete [] _featureIndex;
}
_featureIndex = new int[selectedFeatureNum];
srand(time(NULL));
int i, j;
for (i = 0, j = _featureNum-selectedFeatureNum; j < _featureNum; i++, j++) {
int index = rand()%j;
int flag = 0;
for (int k = 0 ; k <= i; k++) {
if (_featureIndex[k] == index) {
flag = 1;
break;
}
}
if (flag == 0) {
_featureIndex[i] = index;
} else {
_featureIndex[i] = j;
}
}
}