In [1]:
%matplotlib notebook 
import numpy as np
import os, string
from matplotlib import pyplot as plt
import scipy as sp
import sklearn as sk
import tensorflow as tf
from sklearn.datasets import load_iris 
iris = load_iris()
data=iris['data']
target = iris['target']
print(data.shape)


(150, 4)

In [2]:
#pca
mean = data.mean(axis=0)
#print(mean)
data1 = np.mat(data - mean)
#print(data1[:3,:])
cor = data1.T.dot(data1)
#print(cor)
s,v,d = np.linalg.svd(cor)
print(s,v)


[[-0.36158968 -0.65653988  0.58099728  0.31725455]
 [ 0.08226889 -0.72971237 -0.59641809 -0.32409435]
 [-0.85657211  0.1757674  -0.07252408 -0.47971899]
 [-0.35884393  0.07470647 -0.54906091  0.75112056]] [ 629.50127448   36.09429217   11.70006231    3.52877104]

In [3]:
tv = s[:2,:]
#print(tv)
pca = data1.dot(tv.T)
#print(pca[:2,:])
plt.scatter(pca[:50,0],pca[:50,1],c='r')
plt.scatter(pca[50:100,0],pca[50:100,1],c='g')
plt.scatter(pca[100:150,0],pca[100:150,1],c='b')


Out[3]:
<matplotlib.collections.PathCollection at 0x11083c8d0>

In [4]:
#import mpl_toolkits.mplot3d as plt3
fig = plt.figure()
ax = fig.add_subplot(111)
#ax = fig.add_subplot(111, projection='3d')
#ax3D.scatter(x, y, z, s=30, c=col, marker='o', cmap=cm)
#ax.scatter(np.array(data[:50,3]),np.array(data[:50,1]),np.array(data[:50,1]), s=20, c='r')
#ax.scatter(np.array(data[50:100,3]),np.array(data[50:100,0]),np.array(data[50:100,1]), s=20, c='g')
#ax.scatter(np.array(data[100:150,3]),np.array(data[100:150,0]),np.array(data[100:150,1]), s=20, c='b')
ax.scatter(np.array(data[:50,0]),np.array(data[:50,1]), s=20, c='r')
ax.scatter(np.array(data[50:100,0]),np.array(data[50:100,1]), s=20, c='g')
ax.scatter(np.array(data[100:150,0]),np.array(data[100:150,1]), s=20, c='b')


Out[4]:
<matplotlib.collections.PathCollection at 0x11214efd0>

In [5]:
from sklearn.cluster import KMeans
clf=KMeans(n_clusters=3)
model=clf.fit(data)
predicted=model.predict(data)
print(predicted)

model=clf.fit(pca)
predicted=model.predict(pca)
print(predicted)


[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 2 0 2 2 2 2
 2 2 0 0 2 2 2 2 0 2 0 2 0 2 2 0 0 2 2 2 2 2 0 2 2 2 2 0 2 2 2 0 2 2 2 0 2
 2 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2
 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 2 1 1 1 1
 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 1
 1 1]

In [6]:
from sklearn.manifold import TSNE
model = TSNE(n_components=2, random_state=0,n_iter=10000, learning_rate=100)
model.fit(data)
restsne=model.fit_transform(data)
print(restsne.shape)


(150, 2)

In [7]:
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
draw = np.array(restsne)
#draw[:,0]=np.sqrt(draw[:,1])
#print(np.array(restsne[:,1]))
ax2.scatter(draw[:50,0],draw[:50,1],c='r')
ax2.scatter(draw[50:100,0],draw[50:100,1],c='g')
ax2.scatter(draw[100:150,0],draw[100:150,1],c='b')


Out[7]:
<matplotlib.collections.PathCollection at 0x113778be0>

In [8]:
#calc pij
max0 = np.max(data,0)
min0 = np.min(data,0)
ndata = (data-min0)/(max0-min0)
print(ndata[:3,:])
sigma=1.0
pij = np.zeros((150,150))
ind =0
for i in ndata:
    #print(i)
    pij[:,ind]=np.exp(-np.sum(np.square(i - ndata),1)/sigma)
    ind +=1

for i in range(0,150):
    pij[i,i]=0
    
print(pij[:5,:5])
    
for i in pij:
    i /= np.sum(i)
    
print(pij[:5,:5])
#sym pij
spij = (pij + np.transpose(pij))/2.
spij = np.maximum(0.000001,spij)

print(spij[:5,:5])


[[ 0.22222222  0.625       0.06779661  0.04166667]
 [ 0.16666667  0.41666667  0.06779661  0.04166667]
 [ 0.11111111  0.5         0.05084746  0.04166667]]
[[ 0.          0.95457487  0.97213757  0.95374859  0.99749543]
 [ 0.95457487  0.          0.98973491  0.99107226  0.93868849]
 [ 0.97213757  0.98973491  0.          0.99634987  0.96559625]
 [ 0.95374859  0.99107226  0.99634987  0.          0.94550535]
 [ 0.99749543  0.93868849  0.96559625  0.94550535  0.        ]]
[[ 0.          0.0109686   0.0111704   0.0109591   0.01146178]
 [ 0.01090174  0.          0.01130329  0.01131856  0.01072031]
 [ 0.01142146  0.01162821  0.          0.01170593  0.01134461]
 [ 0.01114563  0.0115818   0.01164348  0.          0.0110493 ]
 [ 0.01170232  0.01101242  0.01132809  0.01109239  0.        ]]
[[  1.00000000e-06   1.09351692e-02   1.12959328e-02   1.10523697e-02
    1.15820521e-02]
 [  1.09351692e-02   1.00000000e-06   1.14657470e-02   1.14501812e-02
    1.08663632e-02]
 [  1.12959328e-02   1.14657470e-02   1.00000000e-06   1.16747029e-02
    1.13363501e-02]
 [  1.10523697e-02   1.14501812e-02   1.16747029e-02   1.00000000e-06
    1.10708473e-02]
 [  1.15820521e-02   1.08663632e-02   1.13363501e-02   1.10708473e-02
    1.00000000e-06]]

In [29]:
with tf.device('/cpu:0'):
    X = tf.placeholder('float',(150,150))
    initial = tf.random_normal([150,2]) * 0.0001
    Y = tf.Variable(initial)
    A = tf.reduce_sum(Y*Y, axis=1)
    A = tf.reshape(r, [-1, 1])
    #pair wise distance
    pairD = A - 2*tf.matmul(Y, tf.transpose(Y)) + tf.transpose(A) + 1.
    qij = 1./pairD
    sumq = tf.reduce_sum(qij,axis=1)
    qij /= sumq
    test = tf.log(X / qij) 
    loss = tf.reduce_sum( X*tf.log(X / qij) )
    global_step = tf.Variable(0, name = 'global_step',trainable=False)
    starter_learning_rate = 0.1
    learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,20, 0.95, staircase=True)
    train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss=loss,global_step = global_step) 
    
config = tf.ConfigProto(allow_soft_placement = True)
sess = tf.Session(config = config)
sess.run(tf.global_variables_initializer())  
#print(sess.run(test,feed_dict={X: spij}))

In [30]:
for i in range(3000):
    _,lr,y = sess.run([train_op,learning_rate,Y], feed_dict={X: spij})
    if i % 300 == 0:
        print(i,lr,y[:1,:])
    
res = sess.run(Y)


0 0.1 [[ 0.09565323 -0.09595427]]
300 0.0463291 [[ 0.31000367 -0.47743663]]
600 0.0214639 [[ 0.31059787 -0.48026198]]
900 0.00994402 [[ 0.31068161 -0.48106751]]
1200 0.00460698 [[ 0.3106544  -0.48135358]]
1500 0.00213437 [[ 0.31062666 -0.48148882]]
1800 0.000988835 [[ 0.31060877 -0.4815591 ]]
2100 0.000458119 [[ 0.31059873 -0.48159584]]
2400 0.000212242 [[ 0.31059265 -0.48161638]]
2700 9.833e-05 [[ 0.31059232 -0.48162594]]

In [31]:
print(res)


[[ 0.3105922  -0.48162973]
 [ 0.15524429 -0.57074422]
 [ 0.22726592 -0.59016794]
 [ 0.18021928 -0.59894282]
 [ 0.34191108 -0.4903574 ]
 [ 0.38017404 -0.30618197]
 [ 0.26180848 -0.55157661]
 [ 0.2709558  -0.49797723]
 [ 0.13318279 -0.66392303]
 [ 0.19802026 -0.57418895]
 [ 0.36708555 -0.40224847]
 [ 0.2632685  -0.5224703 ]
 [ 0.17725167 -0.61175328]
 [ 0.21694995 -0.7171585 ]
 [ 0.48421046 -0.35421813]
 [ 0.54186493 -0.26639655]
 [ 0.40921912 -0.35324186]
 [ 0.29186091 -0.45402667]
 [ 0.37055385 -0.2901125 ]
 [ 0.37837735 -0.41861016]
 [ 0.25836241 -0.40805331]
 [ 0.32942525 -0.39736778]
 [ 0.37882861 -0.60106564]
 [ 0.17124562 -0.39129749]
 [ 0.23913172 -0.49244401]
 [ 0.13817117 -0.53501773]
 [ 0.22609189 -0.43386087]
 [ 0.30301479 -0.45368439]
 [ 0.27962792 -0.47415563]
 [ 0.20130694 -0.56109399]
 [ 0.16956724 -0.55581492]
 [ 0.23867728 -0.37387231]
 [ 0.50503033 -0.44772649]
 [ 0.524598   -0.37597573]
 [ 0.19802026 -0.57418895]
 [ 0.23365581 -0.55008757]
 [ 0.32157218 -0.42431584]
 [ 0.19801962 -0.57418996]
 [ 0.17210552 -0.66155547]
 [ 0.27113998 -0.48075232]
 [ 0.29965881 -0.48224872]
 [-0.04736134 -0.70709819]
 [ 0.23262499 -0.64035851]
 [ 0.22432834 -0.36944246]
 [ 0.33164826 -0.34486941]
 [ 0.13563845 -0.5626958 ]
 [ 0.38879159 -0.43658635]
 [ 0.2198084  -0.59732145]
 [ 0.36621857 -0.41955668]
 [ 0.24783352 -0.51870507]
 [-0.28390056  0.45507613]
 [-0.26306367  0.34832543]
 [-0.36362875  0.45182127]
 [-0.45563716 -0.08139433]
 [-0.41075304  0.2918703 ]
 [-0.32942575  0.09395599]
 [-0.28260607  0.38798612]
 [-0.30617365 -0.29911923]
 [-0.32241166  0.28897077]
 [-0.33210844 -0.04507252]
 [-0.44550684 -0.32880238]
 [-0.28365701  0.1908949 ]
 [-0.42634141 -0.08126733]
 [-0.34853533  0.22588712]
 [-0.21009549  0.01692259]
 [-0.26526999  0.35419506]
 [-0.31293935  0.16062155]
 [-0.25527939 -0.01019461]
 [-0.59517616  0.10916606]
 [-0.33122247 -0.07982879]
 [-0.37703133  0.34529677]
 [-0.28130832  0.12568676]
 [-0.53744012  0.21715914]
 [-0.32880411  0.16161028]
 [-0.28305098  0.22688107]
 [-0.29473481  0.31496826]
 [-0.41460991  0.34247565]
 [-0.45655084  0.44069591]
 [-0.3529171   0.2131457 ]
 [-0.24084455 -0.1008006 ]
 [-0.35828853 -0.12557608]
 [-0.32945883 -0.1576235 ]
 [-0.28134033  0.01905791]
 [-0.51358813  0.24042436]
 [-0.31196928  0.12399966]
 [-0.2161424   0.33714107]
 [-0.33096009  0.39931893]
 [-0.50410837  0.09401533]
 [-0.21830176  0.08181562]
 [-0.38590491 -0.04346048]
 [-0.36519545 -0.01304982]
 [-0.30296418  0.23838019]
 [-0.3266452   0.00765739]
 [-0.33691046 -0.29866612]
 [-0.33419773  0.03072733]
 [-0.20073363  0.0873749 ]
 [-0.26332754  0.08895443]
 [-0.27869433  0.18964788]
 [-0.2663992  -0.24969228]
 [-0.2888422   0.06052972]
 [-0.69015473  0.64691526]
 [-0.59350324  0.26761624]
 [-0.66986954  0.63890517]
 [-0.56553954  0.41150174]
 [-0.66820699  0.55222094]
 [-0.74735945  0.76096767]
 [-0.53712666 -0.01831248]
 [-0.66553724  0.63157588]
 [-0.70564383  0.4157376 ]
 [-0.67834252  0.83474523]
 [-0.49253297  0.51156026]
 [-0.62284088  0.38551125]
 [-0.62376624  0.56436414]
 [-0.66869855  0.2256832 ]
 [-0.70185608  0.39079738]
 [-0.59890962  0.56278872]
 [-0.53177226  0.45989674]
 [-0.66728967  0.921646  ]
 [-0.86963302  0.76178759]
 [-0.63768053  0.11483342]
 [-0.65101981  0.66614974]
 [-0.56953782  0.25894165]
 [-0.7778489   0.73548067]
 [-0.55471355  0.31922853]
 [-0.56586927  0.62234777]
 [-0.56021506  0.65471703]
 [-0.51091117  0.31467587]
 [-0.45637277  0.34618166]
 [-0.67399645  0.46444118]
 [-0.53625429  0.56861031]
 [-0.69897389  0.63190705]
 [-0.6093815   0.91170323]
 [-0.69886214  0.48318315]
 [-0.45978373  0.29415748]
 [-0.54745352  0.23561086]
 [-0.75641555  0.77521586]
 [-0.60631496  0.62530863]
 [-0.50017476  0.4637132 ]
 [-0.4429408   0.32094249]
 [-0.59321076  0.59497899]
 [-0.68393087  0.6260255 ]
 [-0.62159783  0.61079258]
 [-0.59350687  0.2676135 ]
 [-0.66455901  0.66262615]
 [-0.67546594  0.68799448]
 [-0.64778626  0.56414676]
 [-0.65132755  0.30720147]
 [-0.55810791  0.47615257]
 [-0.55421191  0.57941419]
 [-0.47394854  0.32594612]]

In [32]:
fig3 = plt.figure()
ax3 = fig3.add_subplot(111)
ax3.scatter(res[:50,0],res[:50,1],c='r',s=20)
ax3.scatter(res[50:100,0],res[50:100,1],c='g',s=20)
ax3.scatter(res[100:150,0],res[100:150,1],c='b',s=20)


Out[32]:
<matplotlib.collections.PathCollection at 0x119450be0>

In [ ]: