How to modify Adaline Stochastic gradient descent


May I know how to modify my own Python programming so that I will get the
same picture as refer to the attached file - Adaline Stochastic gradient descent

(I am using the Anaconda Python 3.7)


Tron Orino Yeong

import numpy as np

class AdalineSGD(object):

      def __init__(self, eta=0.01, epochs=15):
          self.eta = eta
          self.epochs = epochs
      def train(self, X, y, reinitialize_weights=True):
          """Fit training data
          X : Training vectors, X.shape : [#samples, #features]
          y : Target values, y.shape : [#samples]
      if reinitialize_weights:    
          # weights
          self.w_ = np.zeros(1 + X.shape[1])
          # Cost function
          self.cost_ = []

          for i in range(self.epochs):
              for xi, target in zip(X, y):
                  output = self.net_input(xi)
                  error = (target - output)
                  self.w_[1:] += self.eta *
                  self.w_[0] += self.eta * error
                  cost = ((y - self.activation(X))**2).sum() / 2.0
     return self

def net_input(self, X):
         """Calculate net input"""
         return, self.w_[1:]) + self.w_[0]

def activation(self, X):
         """Compute linear activation"""
         return self.net_input(X)

def predict(self, X):
         """Return class label after unit step"""
         return np.where(self.activation(X) >= 0.0, 1, -1)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv('', header=None)

y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0, 2]].values
aln = AdalineSGD(eta=0.01, epochs=15, random_state=1), y)    
plt.plot(range(1, len(aln.cost_) + 1), aln.cost_ /100, marker='o', label='SGD1')
plt.plot(range(1, len(aln.cost_) + 1), aln.cost_ / 50, marker='x', label='SGD2')
plt.plot(range(1, len(aln.cost_) + 1), aln.cost_ / 10, marker='-', label='SGD3')

plt.ylabel('Average Cost')
plt.title('Mini-Batch Learning')

Dear @vokoyo,

From your description, it is not entirely clear what you are trying to achieve. Which textbook are you referring to?

Also, it sounds to me that this could be some kind of assignment for a class that you are taking? If so, this community is not the place to ask for solutions to assignments, in particular since your questions looks very unrelate to KNIME.


1 Like

I get the correct picture however I still not sure my programming is correct or not

Please refer to the attached file -

import numpy as np

class AdalineSGD (object):
    def __init__(self, eta=0.01, n_iter=10, shuffle=True, random_state=None, batch=10):
        self.batch = 100/batch 
        self.eta = eta
        self.n_iter = n_iter
        self.w_initialized = False
        self.shuffle = shuffle
        self.random_state = random_state
    def fit (self,X, y):
        self.cost_ = []
        for i in range(self.n_iter):
            if self.shuffle:
                X, y=self._shuffle(X,y)
            cost = []                
            mini_X = np.array_split(X,self.batch)
            mini_y = np.array_split(y,self.batch)
            for xi, target in zip (mini_X, mini_y):
                cost.append(self._update_weights(xi, target))
            avg_cost = sum(cost)/len(cost)
        return self
    def partial_fit(self, X, y):
        if not self.w_initialized:
        if y.ravel().shape[0] > 1:
           for xi, target in zip (X, y):
        return self
    def _shuffle(self, X, y):
        r = np.random.permutation(len(y))
        return X[r], y[r]
    def _initialize_weights(self, m):
        self.w_ = np.zeros(1 + m)
        self.w_initialized = True
    def _update_weights(self, xi, target):
        output = self.activation(self.net_input(xi))
        error = (target - output)
        self.w_[1:] += self.eta * 
        self.w_[0] += self.eta * error.sum()
        cost = 0.5 * (error**2).sum()
        return cost
    def net_input(self, X):
        return, self.w_[1:]) + self.w_[0]
    def activation (self, X):
        return X
    def predict(self, X):
        return np.where(self.activation(self.net_input(X))>= 0.0, 1, -1)

import pandas as pd
df = pd.read_csv('''machine-learning-databases/iris/',header=None)
import matplotlib.pyplot as plt
y = df.iloc[0:100,4].values
y = np.where(y=='Iris-setosa',-1,1)
X = df.iloc[0:100,[0,2]].values
X_std = np.copy(X)
X_std[:,0] = (X[:,0]-X[:,0].mean())/X[:,0].std()
X_std[:,1] = (X[:,1]-X[:,1].mean())/X[:,1].std()

ada1 = AdalineSGD(n_iter=15, eta=0.01, random_state=1, batch=1)
ada2 = AdalineSGD(n_iter=15, eta=0.01, random_state=1, batch=2)
ada3 = AdalineSGD(n_iter=15, eta=0.01, random_state=1, batch=10),y),y),y)

plt.plot(range(1, len(ada1.cost_) + 1), ada1.cost_, marker='o', color='blue', label='batch=1')
plt.plot(range(1, len(ada2.cost_) + 1), ada2.cost_, marker='o', color='orange', label='batch=2')
plt.plot(range(1, len(ada3.cost_) + 1), ada3.cost_, marker='o', color='green', label='batch=10')
plt.legend(loc='upper right')
plt.title('Mini-Batch Learning')
plt.ylabel('Avaerage Cost')

Please refer to the link -