-
Notifications
You must be signed in to change notification settings - Fork 1
/
pr2.py
133 lines (89 loc) · 3.46 KB
/
pr2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import re
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA
from sklearn.decomposition import SparsePCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_predict
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC,NuSVC
from sklearn import cross_validation, linear_model
from sklearn.linear_model import LogisticRegression
from imblearn.over_sampling import SMOTE
from sklearn.decomposition import TruncatedSVD
from sklearn.random_projection import sparse_random_matrix
from sklearn.linear_model import Perceptron
from sklearn import svm
train_file = "train.dat"
test_file = "test.dat"
feature_size = 100001
k_folds = 10
def load(filename, ftype):
with open(filename, "r") as rfile:
lines = rfile.readlines()
if ftype == "train":
labels = [int(l[0]) for l in lines]
for index, item in enumerate(labels):
if (item == 0):
labels[index] = -1
docs = [re.sub(r'[^\w]', ' ',l[1:]).split() for l in lines]
else:
labels = []
docs = [re.sub(r'[^\w]', ' ',l).split() for l in lines]
features = []
for doc in docs:
line = [0]*feature_size
for index, val in enumerate(doc):
line[int(val)] = 1
features.append(line)
return features, labels
print 'Starting processing for drug activity prediction'
print "Loading training data"
# Loading train.dat file
features, labels = load(train_file, "train")
#Using Dimensionality Reduction on train data
print "Reducing Dimensions using Truncated SVD on train data"
svd_trunc = TruncatedSVD(algorithm='randomized', n_components=1500, n_iter=50, random_state=42)
svd_trunc_m = svd_trunc.fit(features, labels)
reduced_features = svd_trunc_m.transform(features)
#Using oversampling SMOTE
print "Oversampling data using SMOTE!"
sm = SMOTE(random_state=42,kind='svm')
reduced_features, labels = sm.fit_sample(reduced_features, labels)
#processing test data
print "Loading test data"
test_features, test_labels = load(test_file, "test")
print "Reducing Dimensions using Truncated SVD on test data"
test_reduced_features = svd_trunc_m.transform(test_features)
# Classifying
names = ["Decision Tree"]
classifiers = [DecisionTreeClassifier(random_state=53,class_weight={-1: 1, 1: 1.5})]
print 'Starting classification!!'
for name, clf in zip(names, classifiers):
print 'Report on ' + name
cv_predicted = cross_val_predict(clf, reduced_features, labels, cv=k_folds)
print metrics.classification_report(labels, cv_predicted)
scores = cross_validation.cross_val_score(clf, reduced_features, labels)
print '\nCross validation scores: '
print scores.mean()
#training classifier
clf.fit(reduced_features, labels)
# Predict test labels
test_predicted = clf.predict(test_reduced_features)
print 'Test predicted for ' + name
result_file = 'format.dat'
print 'Output stored in', result_file
output = open(result_file, 'w')
for t in test_predicted:
if int(t) == -1:
t = 0
output.write(str(t))
output.write("\n")
output.close()
print 'Finished!'