-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifire.py
63 lines (55 loc) · 1.94 KB
/
classifire.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
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.feature_selection import mutual_info_classif
import numpy as np
import pandas as pd
import os
def getParameters(line):
parameters = line.split(';')
for i in range(len(parameters)):
parameters[i] = int(parameters[i])
return parameters
def readDataset(filename):
#flag=0
data = []
with open(filename) as file:
for line in file:
#if flag==0:
# flag=1
# continue
parameters = getParameters(line)
data.append(parameters)
return data
def readLabels(filename):
labels = []
with open(filename) as file:
for line in file:
labels.append(int(line[0]))
return labels
def method_chi2(data, labels, test):
selecter = SelectKBest(score_func=mutual_info_classif, k=30)
selecter.fit(data, labels)
string = selecter.get_support()
return selecter.transform(data),selecter.transform(test)
def classifire(dataset,labels,test_data):
dataset,test_data=method_chi2(dataset,labels,test_data)
myClassifier = RandomForestClassifier(n_estimators=1000,n_jobs = -1, max_features = 'auto',min_samples_split=10)
myClassifier.fit(dataset,labels)
proba=myClassifier.predict(test_data)
#print(proba)
return proba
def Main():
directory = os.path.dirname(os.path.abspath(__file__))
'''
dataset = readDataset(directory + "\\data.csv")
test_data=readDataset(directory + "\\data2.csv")
flag = readLabels(directory + "\\flag.csv")
'''
dataset = readDataset(directory + "\\datape1.csv")
test_data=readDataset(directory + "\\datape.csv")
flag = readLabels(directory + "\\flagpe1.csv")
return classifire(dataset,flag,test_data)
#
if __name__ == '__main__':
Main()