#!/usr/bin/env python
# coding: utf-8
#
Table of Contents
#
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from spkit.data import dataGen as ds
from spkit.ml import LR
# # Example 1
# ## Data
# In[20]:
np.random.seed(1)
dtype = ['MOONS','GAUSSIANS','LINEAR','SINUSOIDAL','SPIRAL']
X, y,_ = ds.create_dataset(200, dtype[3],0.05,varargin = 'PRESET')
#Normalizing
means = np.mean(X,0)
stds = np.std(X,0)
X = (X-means)/stds
#In cureent version LR takes X and y as shape (nf,n) and (n,1)
X = X.T
y = y[None,:]
print(X.shape, y.shape)
# ## Fitting
# In[6]:
get_ipython().run_line_magic('matplotlib', 'notebook')
# In[21]:
clf = LR(X,y,alpha=0.0003,polyfit=True,degree=3,lambd=2)
fig=plt.figure(figsize=(8,4))
ax1=fig.add_subplot(121)
ax2=fig.add_subplot(122)
for i in range(100):
clf.fit(X,y,itr=10,verbose=False)
ax1.cla()
clf.Bplot(ax1,hardbound=False)
ax2.cla()
clf.LCurvePlot(ax2)
fig.canvas.draw()
#plt.pause(0.001)
plt.close(fig)
# ## Weights
# In[22]:
W,b = clf.getWeights()
print('W: ',W)
print('b: ',b)
# # Example 2
# ## Data
# In[15]:
dtype = ['MOONS','GAUSSIANS','LINEAR','SINUSOIDAL','SPIRAL']
X, y,_ = ds.create_dataset(200, dtype[4],0.01,varargin = 'PRESET')
#Normalizing
means = np.mean(X,0)
stds = np.std(X,0)
X = (X-means)/stds
#In cureent version LR takes X and y as shape (nf,n) and (n,1)
X = X.T
y = y[None,:]
print(X.shape, y.shape)
# ## Fitting
# In[17]:
clf = LR(X,y,alpha=0.0003,polyfit=True,degree=5,lambd=1.5)
fig=plt.figure(figsize=(8,5))
gs=GridSpec(3,2)
ax1=fig.add_subplot(gs[0:2,0])
ax2=fig.add_subplot(gs[0:2,1])
ax3=fig.add_subplot(gs[2,:])
for i in range(100):
clf.fit(X,y,itr=10,verbose=False)
ax1.cla()
clf.Bplot(ax1,hardbound=True)
ax2.cla()
clf.LCurvePlot(ax2)
ax3.cla()
clf.Wplot(ax3)
fig.canvas.draw()
plt.close(fig)
# ## Weights
# In[19]:
W,b = clf.getWeights()
print('W: ',W)
print('b: ',b)
# In[ ]: