-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAG.py
More file actions
83 lines (71 loc) · 2.75 KB
/
Copy pathAG.py
File metadata and controls
83 lines (71 loc) · 2.75 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#AG.py
import math
from multiprocessing import Process, Array
import pickle
from sklearn import tree
import warnings
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from skimage.io import imread
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
import numpy as np
import pandas as pd
class AGClassifier(object):
Dec_T_1 = tree.DecisionTreeClassifier(max_depth=5)
Dec_T_2 = tree.DecisionTreeClassifier(max_depth=5)
Ran_F_1 = RandomForestClassifier(max_depth=5)
Ran_F_2 = RandomForestClassifier(max_depth=5)
Near_N = KNeighborsClassifier(n_neighbors=3)
TrainingSet=[]
SubsetA=[]
SubsetB=[]
TrainingTarget= []
SubTargetA=[]
SubTargetB=[]
def __init__(self):
garbage=""
def classify(self,classifier,dataset,predict_result):
for i in range(0,dataset.shape[0]):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
predict_result[i]=(classifier.predict(dataset.iloc[i])[0])
def train(self,X,y):
self.TrainingSet = X
self.TrainingTarget = y
size = X.shape[0]
midpoint = int(math.floor((size/2)))
self.SubsetA=X[:midpoint]
self.SubsetB = X[midpoint:size]
self.SubTargetA = self.TrainingTarget[0:midpoint]
self.SubTargetB = self.TrainingTarget[midpoint:]
self.Dec_T_1.fit(self.SubsetA,self.SubTargetA)
self.Dec_T_2.fit(self.SubsetB,self.SubTargetB)
self.Ran_F_1.fit(self.SubsetA,self.SubTargetA)
self.Ran_F_2.fit(self.SubsetB,self.SubTargetB)
DT_pre = []
DT_pre_A = Array('i', range(midpoint))
DT_pre_B = Array('i', range((size-midpoint)))
RT_pre = []
RT_pre_A = Array('i', range(midpoint))
RT_pre_B = Array('i', range((size-midpoint)))
jobs=[Process(target=self.classify, args=(self.Dec_T_1,self.SubsetB,DT_pre_B)), Process(target=self.classify, args=(self.Dec_T_2,self.SubsetA,DT_pre_A)), Process(target=self.classify, args=(self.Ran_F_1,self.SubsetB,RT_pre_B)), Process(target=self.classify, args=(self.Ran_F_2,self.SubsetA,RT_pre_A))]
for j in jobs:
j.start()
print("set trained")
for j in jobs:
j.join()
for pred in DT_pre_A:
DT_pre.append(pred)
for pred in DT_pre_B:
DT_pre.append(pred)
for pred in RT_pre_A:
RT_pre.append(pred)
for pred in RT_pre_B:
RT_pre.append(pred)
DT_pre = np.asarray((DT_pre))
RT_pre = np.asarray((RT_pre))
self.TrainingSet['dt'] = pd.Series(DT_pre, index=self.TrainingSet.index)
self.TrainingSet['rt'] = pd.Series(RT_pre, index=self.TrainingSet.index)
self.Near_N.fit(self.TrainingSet,self.TrainingTarget)