In [1]:
%pylab inline
from classy import *
In [2]:
data=load_excel('data/iris.xls')
data_train,data_test=split(data,test_size=0.2)
In [3]:
len(data.targets),len(data_train.targets),len(data_test.targets),
Out[3]:
In [4]:
C=Perceptron()
In [5]:
timeit(reset=True)
C.fit(data_train.vectors,data_train.targets)
print(("Training time: ",timeit()))
In [6]:
print(("On Training Set:",C.percent_correct(data_train.vectors,data_train.targets)))
print(("On Test Set:",C.percent_correct(data_test.vectors,data_test.targets)))
In [7]:
C.coef_ # these are the weights
Out[7]:
In [8]:
C=BackProp(hidden_layer_sizes = [4],max_iter=10000,tol=1e-4)
In [9]:
timeit(reset=True)
C.fit(data_train.vectors,data_train.targets)
print(("Training time: ",timeit()))
In [10]:
data_train.vectors.shape,data_train.targets.shape
Out[10]:
In [11]:
print(("On Training Set:",C.percent_correct(data_train.vectors,data_train.targets)))
print(("On Test Set:",C.percent_correct(data_test.vectors,data_test.targets)))
In [12]:
C.weights
Out[12]:
In [13]:
W_inp_hid,W_hid_out=C.weights
print(W_inp_hid)
print("==")
print(W_hid_out)
In [14]:
data=load_csv('data/xor.csv')
print()
print((data.vectors))
print()
print((data.targets))
In [15]:
C=Perceptron()
In [16]:
C.fit(data.vectors,data.targets)
In [17]:
print((C.predict(data.vectors)))
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
In [18]:
plot2D(data,classifier=C,axis_range=[-.55,1.5,-.5,1.5])
In [19]:
data.vectors
Out[19]:
In [20]:
data.targets
Out[20]:
In [21]:
C=BackProp(hidden_layer_sizes = [5],max_iter=10000,tol=1e-4)
In [22]:
C.fit(data.vectors,data.targets)
In [23]:
print((C.predict(data.vectors)))
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
In [24]:
plot2D(data,classifier=C,axis_range=[-.55,1.5,-.5,1.5])
In [25]:
print((data.vectors))
print()
print((data.targets))
In [26]:
C.output(data.vectors)
Out[26]:
In [27]:
h,y=C.output(data.vectors)
print(h)
print()
print((np.round(h)))
print()
print(y)
In [29]:
C.weights
Out[29]:
In [30]:
data.vectors.shape
Out[30]:
In [31]:
N=30
x1=randn(N)*.2
y1=randn(N)*.2
plot(x1,y1,'bo')
a=linspace(0,3*pi/2,N)
x2=cos(a)+randn(N)*.2
y2=sin(a)+randn(N)*.2
plot(x2,y2,'rs')
Out[31]:
In [32]:
vectors=vstack([hstack([atleast_2d(x1).T,atleast_2d(y1).T]),
hstack([atleast_2d(x2).T,atleast_2d(y2).T]),
])
targets=concatenate([zeros(N),ones(N)])
target_names=['center','around']
feature_names=['x','y']
In [33]:
data=Struct(vectors=vectors,targets=targets,
target_names=target_names,feature_names=feature_names)
In [34]:
C=Perceptron()
C.fit(data.vectors,data.targets)
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
plot2D(data,classifier=C,axis_range=[-2,2,-2,2])
In [42]:
C=BackProp(hidden_layer_sizes = [6],max_iter=10000,tol=1e-4)
C.fit(data.vectors,data.targets)
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
plot2D(data,classifier=C,axis_range=[-2,2,-2,2])
In [43]:
C=kNearestNeighbor()
C.fit(data.vectors,data.targets)
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
plot2D(data,classifier=C,axis_range=[-2,2,-2,2])
In [44]:
C=CSC()
C.fit(data.vectors,data.targets)
print(("On Training Set:",C.percent_correct(data.vectors,data.targets)))
C.plot_centers()
plot2D(data,classifier=C,axis_range=[-2,2,-2,2])
In [45]:
vectors=eye(8)
targets=arange(1,9)
print((vectors,targets))
In [47]:
C=BackProp(activation='logistic',hidden_layer_sizes = [3],max_iter=10000,tol=1e-4)
C.fit(vectors,targets)
print((C.predict(vectors)))
In [48]:
h,y=C.output(vectors)
In [49]:
h
Out[49]:
In [50]:
h.round()
Out[50]:
In [51]:
y.round()
Out[51]:
In [52]:
C.predict(vectors)
Out[52]:
In [53]:
y.shape
Out[53]:
In [54]:
imshow(h,interpolation='nearest',cmap=cm.gray)
colorbar()
Out[54]:
In [56]:
weights_xh,weights_hy=C.weights
In [57]:
plot(weights_xh,'-o')
Out[57]:
In [58]:
plot(weights_hy,'-o')
Out[58]:
In [59]:
data=load_excel('data/iris.xls')
data_train,data_test=split(data,test_size=0.75)
select which number of hidden units to use
In [61]:
hidden=list(range(1,10))
percent_correct=[]
for n in hidden:
C=BackProp(hidden_layer_sizes = [n],tol=1e-4,max_iter=10000)
C.fit(data_train.vectors,data_train.targets)
percent_correct.append(C.percent_correct(data_test.vectors,data_test.targets))
plot(hidden,percent_correct,'-o')
xlabel('Number of Hidden Units')
ylabel('Percent Correct on Test Data')
Out[61]:
In [ ]:
In [ ]:
In [ ]: