import numpy as np
from scipy.sparse import csc_matrix

import networkx as nx


class network(object):
    def __init__(self, w=None, nnum=1000, trainable=False):
        if w!=None:
	    self.w = w    
        else:
	    self.w = csc_matrix((nnum,nnum),dtype=np.float32) 
        self.v = csc_matrix((1,nnum),dtype=np.float32)
	self.trainable = trainable

    def run(self,times=10):
        if self.trainable == True:
	    print "Train"
	    for n in range(times):
	        self.step_train()
	else:
	    print "Predict"
	    for n in range(times):
	        self.step_predict()
    
    def step_predict(self):
        res = self.v.dot(self.w)  
        self.v = res.multiply(res > 0)

	print np.sum(self.v)

    def step_train(self):
        v_stash = self.v
        res = self.v.dot(self.w)  
        self.v = res.multiply(res > 0.1)
        #self.w = self.w + 0.01 * v_stash.T * self.v
	print np.sum(self.v)

def getdata():
    num = np.random.randint(low=0,high=255,size=(200,1),dtype=np.uint8)
    x =  np.unpackbits(num,axis=1)
    y = num >125
    
    data = (x,y)
    return data    

    
if __name__=='__main__':
    BA = nx.random_graphs.barabasi_albert_graph(1000, 1)
    idx = np.array(BA.edges())
    #data = np.random.uniform(0,1,idx.shape[0])
    data = np.ones(idx.shape[0])
    w = csc_matrix((data, (idx[:,0], idx[:,1])), shape=(1000,1000))

    print np.sum(w,axis=1)

    n = network(w=w)
    #n.v[:,:20] = np.random.uniform(0,1,20)
    n.v[:,:20] = np.ones(20)
    n.trainable = True
    n.run(times=20)



@@ -47,16 +47,60 @@
 c_matrix
+%0Afrom sklearn.preprocessing import normalize
 %0A%0Aimport
@@ -720,16 +720,18 @@
 y(res %3E 
+0.
 0)%0A%0A%09pri
@@ -733,29 +733,27 @@
 %0A%09print 
-np.sum(
 self.v
+.sum(
 )%0A%0A    d
@@ -871,17 +871,17 @@
 res %3E 0.
-1
+0
 )%0A      
@@ -882,17 +882,16 @@
         
-#
 self.w =
@@ -895,50 +895,122 @@
 w = 
+1.1*
 self.w
- + 0.01 * v_stash.T * self.v
+.multiply(v_stash.T%3E0).multiply(self.v%3E0)%0A        self.w = normalize(self.w, norm='l1', axis=1)%0A
 %0A%09print 
 np.s
@@ -1009,21 +1009,19 @@
 int 
-np.sum(
 self.v
+.sum(
 )%0A%0Ad
@@ -1205,36 +1205,19 @@
   %0A%0A
-    %0Aif __name__=='__main__'
+def graph()
 :%0A  
@@ -1271,11 +1271,11 @@
 000,
- 
 1)
+ 
 %0A   
@@ -1301,16 +1301,167 @@
 edges())
+%0A%0A    np.save('../data/ba_network.npy',idx)%0A    print 'save ok'%0A %0A    %0Aif __name__=='__main__':%0A    graph()%0A    idx = np.load('../data/ba_network.npy')
 %0A    #da
@@ -1606,28 +1606,40 @@
 0))%0A
-%0A
     
-print np.sum(w,
+w = normalize(w, norm='l1', 
 axis
@@ -1717,24 +1717,25 @@
    n.v%5B:,:20
+0
 %5D = np.ones(
@@ -1736,16 +1736,17 @@
 .ones(20
+0
 )%0A    n.

normalize w