In [2]:
import tensorflow as tf
import pandas as pd
import numpy as np

In [16]:
data = pd.read_csv('ex2data1.txt')
print(data.mean())
print(data.mean().to_string())


34.62365962451697    65.957614
78.0246928153624     66.102779
0                     0.606061
dtype: float64
34.62365962451697    65.957614
78.0246928153624     66.102779
0                     0.606061

In [9]:
result = pd.DataFrame()
for d in data:
    print d
    result.append(d)
print result


34.62365962451697
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-2bf7c104d1b9> in <module>()
      2 for d in data:
      3     print d
----> 4     result.append(d)
      5 print result

/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in append(self, other, ignore_index, verify_integrity)
   3970             to_concat = [self, other]
   3971         return concat(to_concat, ignore_index=ignore_index,
-> 3972                       verify_integrity=verify_integrity)
   3973 
   3974     def join(self, other, on=None, how='left', lsuffix='', rsuffix='',

/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.pyc in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
    752                        keys=keys, levels=levels, names=names,
    753                        verify_integrity=verify_integrity,
--> 754                        copy=copy)
    755     return op.get_result()
    756 

/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.pyc in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy)
    803         for obj in objs:
    804             if not isinstance(obj, NDFrame):
--> 805                 raise TypeError("cannot concatenate a non-NDFrame object")
    806 
    807             # consolidate

TypeError: cannot concatenate a non-NDFrame object

In [3]:
y = np.float32(data[:, 2])
print(y.shape)
x = np.float32(data[:, :2]).T
mean = np.mean(x, axis=1)
std = np.std(x, axis=1)
print(np.mean(x, axis=1))
print(np.std(x, axis=1))
print(x.shape)
x[0,:] = (x[0,:] - mean[0]) / std[0]
x[1,:] = (x[1,:] - mean[1]) / std[1]
print(x)


(99,)
[ 65.95761108  66.1027832 ]
[ 19.20427513  18.54450226]
(2, 99)
[[-1.85744572 -1.56789064 -0.30071497  0.6808446  -1.08696282 -0.2525973
   0.47214127  0.5280686   0.96203643  1.55715048  0.47156402  0.85134387
   0.1774071  -1.37569749 -0.62416106  0.16207504  0.10358332  0.24494003
   0.57389158  0.07365097  1.235098   -0.80309314 -1.65304589  0.62311566
  -0.19196746  0.7411145   1.41410053 -0.21492107 -1.41488326 -0.23840126
   1.01263428 -0.72117442 -0.72443271 -1.33932257 -0.58958256 -1.66848838
  -0.09272021  0.45987895 -1.654526    0.93441623 -0.75034797  1.48330283
   0.85455662 -0.77638227 -0.19448461  0.58504784  1.65660965 -0.20227517
   1.3334198   0.72833812  1.73476577  1.28039742 -1.63677609 -0.81602228
  -0.85246295  1.65005064 -1.73817599  0.43173078  0.30403918  0.49145326
  -1.59711826 -0.50529337 -1.86931229 -1.10857344  0.03141419 -1.32783246
  -0.87923378  0.74576938  0.04109024 -1.73059273 -0.09988865  0.33268037
  -0.28638026 -0.37057653  1.7636826  -0.97339475 -0.80708337 -0.28650162
   0.84715778  1.19537365  1.50366998  0.07090341 -0.45400861  0.75030905
   0.13074745 -1.24358547  0.49572754  0.66015565 -0.70867592  1.46512783
   1.2752856  -0.54547483  0.44443646  1.24390006  0.91289854 -1.23388731
   1.73697782 -0.55287647  0.45918334]
 [-1.19754016  0.36665398  1.08958292  0.49834675 -0.52772576  1.6397661
  -1.05415452  1.14954758 -1.21703935 -1.50327611 -1.91428804  0.55969048
   1.70486677  0.5356859   1.2458986  -0.72055382 -1.04743767  1.44648564
  -0.99904656 -1.25451434 -0.01636178 -0.93003148 -1.18057919  0.15473996
   0.20769913 -1.14757216 -1.47224832 -0.85452151 -0.05969971  0.36156812
  -0.48805842 -0.16043346  0.17957225  0.27312484 -0.74894959  1.76692021
   0.79836464 -1.32272995  0.49259523 -0.52817482 -1.03785443 -0.02878801
  -1.37423623 -1.09359002 -0.75719422  0.23486289  1.11219156  1.65364575
   1.2183398   0.43464813 -0.27521646 -1.22473931 -0.30771598 -0.87887204
  -0.33939067  0.14876588  1.59053957  0.20069487  0.66600746  1.05999875
  -1.02899861 -1.44739997 -0.89028043  0.01872821 -1.34868467  1.69497156
  -0.76678091  1.40274858 -0.27562827 -1.22923803  0.64325809  1.62446034
   0.37705073  0.52606797  0.33791533  1.2064538   0.52344769 -1.27231109
  -1.26090765  0.19957419 -1.1005137   0.02623809 -0.35528055  1.34041715
   1.05106747  0.68710417  1.31153452  1.64709985 -0.28759351  0.59620476
   1.15430474 -1.64642227  1.01066911 -1.11863339 -0.95567387  1.1324687
   0.14411941 -0.06313704  1.26328719]]

In [ ]:


In [4]:
b = tf.Variable(tf.zeros([1]))
w = tf.Variable(tf.random_uniform([1,2], -1.0, 1.0))

In [5]:
result = tf.matmul(w, x) + b

In [6]:
loss = tf.reduce_mean(-y * (tf.log(tf.clip_by_value(tf.sigmoid(result),1e-10,1.0)))
                      - (1 - y) * tf.log(1 - tf.clip_by_value(tf.sigmoid(result), 1e-10, 1.0)))
#loss = (-y * (tf.log(tf.clip_by_value(tf.sigmoid(result),1e-10,1.0)))
#                      - (1 - y) * tf.log(tf.clip_by_value(1 - tf.sigmoid(result), 1e-10, 1.0)))
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(result, y))

In [7]:
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
predict = tf.sigmoid(result)

In [8]:
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for step in xrange(5001):
    sess.run(train)
    if step % 500 == 0:
        print(step, sess.run(w), sess.run(b), sess.run(loss))


(0, array([[ 0.79882932,  0.06533477]], dtype=float32), array([ 0.01051466], dtype=float32), 0.53058338)
(500, array([[ 2.47829819,  2.32016063]], dtype=float32), array([ 1.06112695], dtype=float32), 0.22380283)
(1000, array([[ 3.00805116,  2.83314633]], dtype=float32), array([ 1.32222354], dtype=float32), 0.21104674)
(1500, array([[ 3.29736972,  3.11431885]], dtype=float32), array([ 1.46360791], dtype=float32), 0.20731446)
(2000, array([[ 3.47904944,  3.29110098]], dtype=float32), array([ 1.55184197], dtype=float32), 0.20585249)
(2500, array([[ 3.60059595,  3.40943193]], dtype=float32), array([ 1.61062586], dtype=float32), 0.20520034)
(3000, array([[ 3.68484569,  3.49147511]], dtype=float32), array([ 1.65125883], dtype=float32), 0.20488764)
(3500, array([[ 3.74454093,  3.54961634]], dtype=float32), array([ 1.67999411], dtype=float32), 0.20473085)
(4000, array([[ 3.78745127,  3.5914135 ]], dtype=float32), array([ 1.70062149], dtype=float32), 0.2046499)
(4500, array([[ 3.81860447,  3.62176013]], dtype=float32), array([ 1.71558213], dtype=float32), 0.20460725)
(5000, array([[ 3.84137797,  3.64394426]], dtype=float32), array([ 1.72651076], dtype=float32), 0.20458446)

In [9]:
print np.round(sess.run(predict))
output = np.round(sess.run(predict)).T


[[ 0.  0.  1.  1.  0.  1.  0.  1.  1.  1.  0.  1.  1.  0.  1.  0.  0.  1.
   1.  0.  1.  0.  0.  1.  1.  1.  1.  0.  0.  1.  1.  0.  0.  0.  0.  1.
   1.  0.  0.  1.  0.  1.  1.  0.  0.  1.  1.  1.  1.  1.  1.  1.  0.  0.
   0.  1.  1.  1.  1.  1.  0.  0.  0.  0.  0.  1.  0.  1.  1.  0.  1.  1.
   1.  1.  1.  1.  1.  0.  1.  1.  1.  1.  0.  1.  1.  0.  1.  1.  0.  1.
   1.  0.  1.  1.  1.  1.  1.  0.  1.]]

In [10]:
print(len(y), len(output))
correct = [y[i] == output[i] for i in xrange(len(y))]
print(np.mean(correct) * 100)


(99, 99)
88.8888888889

In [ ]: