-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear.py
128 lines (110 loc) · 3.49 KB
/
linear.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
import tensorflow as tf
import sklearn
from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.utils import shuffle
from sklearn import preprocessing
import numpy
import random
import pandas as pd
d = pd.read_csv('santander/train.csv')
X_train_list=[]
Y_train_list=[]
X_validate_list=[]
Y_validate_list=[]
data_list=[]
for row in d.itertuples():
#X_train_list.append(list(row[1:371]))
#Y_train_list.append(list(row[372]))
data_list.append(list(row[1:]))
random.shuffle(data_list)
datasize=len(data_list)
training_data = data_list[:datasize*60/100]
validate_data = data_list[datasize*60/100:]
for innerlist in training_data:
#print len(innerlist)
X_train_list.append(innerlist[:369])
if innerlist[370]==0:
Y_train_list.append([1,0])
else:
Y_train_list.append([0,1])
for innerlist in validate_data:
X_validate_list.append(innerlist[:369])
if innerlist[370]==0:
Y_validate_list.append([1,0])
else:
Y_validate_list.append([0,1])
scaler = preprocessing.StandardScaler()
X_train = scaler.fit_transform(X_train_list)
X_validate = scaler.fit_transform(X_validate_list)
print ("Scaled")
X_train_list=[]
X_validate_list=[]
X_train_list_2 = X_train.tolist()
X_validate_list_2 = X_validate.tolist()
x=tf.placeholder(tf.float32, [None, 369])
X_train=[]
X_validate=[]
data_list=[]
d=[]
training_data=[]
validate_data=[]
#W1 = tf.Variable(tf.zeros([13,5]))
#W2 = tf.Variable(tf.zeros([5,1]))
#W = tf.Variable(tf.random_normal([13,1], mean = 0.0,stddev=0.35))
#b1 = tf.Variable(tf.zeros([1]))
#b2 = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.zeros([369,2]))
b = tf.Variable(tf.zeros([2]))
#y = tf.nn.softmax(tf.matmul(x,W) + b)
#h1 = tf.nn.relu(tf.matmul(x,W1) + b1)
#y = tf.matmul(h1,W2) + b2
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder(tf.float32, [None, 2])
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#sse = tf.reduce_sum( tf.pow((y_ - y),2))
sse = tf.reduce_sum( (y_ - y)*(y_ - y))
mse = tf.reduce_mean( tf.pow((y_ - y),2))
train_step = tf.train.GradientDescentOptimizer(0.000000000000000001).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
correct_predition = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_predition, "float"))
index_list = range(len(X_train_list_2))
for i in range(50):
#1000000 3489.25
#batch_xs, batch_ys = shuffle(X_train_list, Y_train_list, random_state=i)
#for (x,y) in zip(batch_xs, batch_ys):
#sess.run(train_step, feed_dict={x: x, y_: y})
random.shuffle(index_list)
tempx=[]
tempy=[]
##print index_list
count=0
for j in index_list:
if count > 500:
break
tempx.append(X_train_list_2[j])
tempy.append(Y_train_list[j])
sess.run(train_step, feed_dict={x: tempx, y_: tempy})
if i%10 == 0:
print sess.run(accuracy, feed_dict={x: tempx, y_: tempy})
#print sess.run(sse, feed_dict={x: tempx, y_: tempy})
#sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#sess.run(train_step, feed_dict={x: X_train[i], y_: Y_train2[i]})
print "Done training"
#print sess.run(sse, feed_dict={x: X_test, y_: Y_test2})
print "W:"
print sess.run(W)
print "b:"
print sess.run(b)
print sess.run(accuracy, feed_dict={x: X_validate_list_2, y_: Y_validate_list})
print "y:"
print sess.run(y, feed_dict={x: X_validate_list_2})
print "y_:"
print sess.run(y_, feed_dict={y_: Y_validate_list})
#print sess.run(y, feed_dict={x: X_validate_list})
#print Y_validate_list
#test= pd.read_csv('santander/test.csv')
#print sess.run(y, feed_dict={x: X_validate_list})