Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Why I get the ValueError
#1




Dear


May I know why I get the error message -

line 49, in _update_weights

self.w_[1:]= self.eta*xi.dot(error)

ValueError: shapes (1,2) and (1,) not aligned: 2 (dim 1) != 1 (dim 0)

(Please refer to the attached file - Adaline Stochastic)

Prayerfully



Tron Orino Yeong
tcynotebook@yahoo.com
0916643858




    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._initialize_weights(X.shape[1])
            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(y)
                self.cost_.append(avg_cost)
                return self
        def partial_fit(self, X, y):
            if not self.w_initialized:
                self._inintialize_weights(X.shape[1])
            if y.ravel().shape[0]>1:
                for xi, target in zip (X, y):
                    self._update_weights(X,y)
            else:
                self._update_weights(X,y)
            return self
        
        def _shuffle(self, X,y):
            r=self.rgen.permutation(len(y))
            return X[r],y[r]
        
        def _initialize_weights(self, m):
            self.rgen=np.random.RandomState(self.random_state)
            self.w_=self.rgen.normal(loc=0.0,scale=0.01,size=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*xi.dot(error)
           self.w_[0] = self.eta*error
           cost = 0.5 * error**2
           return cost
        
        def net_input(self,X):
            return np.dot(X,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('https://archive.ics.uci.edu/ml/''machine-learning-databases/iris/iris.data',header=None)
    df.tail()
    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)
    ada1.fit(X_std,y)
    ada2.fit(X_std,y)
    ada3.fit(X_std,y)
    
    plt.plot(range(1,len(ada1.cost_)+1), ada1.cost_,marker='0',color='blue',label='batch=1')
    plt.plot(range(1,len(ada2.cost_)+1), ada2.cost_,marker='0',color='orange',label='batch=2')
    plt.plot(range(1,len(ada3.cost_)+1), ada3.cost_,marker='0',color='green',label='batch=10')
    
    plt.title('Mini-batch learnign')
    plt.legend(loc='upper right')
    plt.xlabel('Epochs')
    plt.ylabel('Avaerage Cost')
    plt.show()
Please refer to the link -

https://www.freecodecamp.org/forum/t/how...ent/265072






 
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  valueError: expected 2d, got 1d instead brkolvr 1 97 Aug-16-2019, 02:43 AM
Last Post: boring_accountant
  ValueError: The truth value of a Series is ambiguous ? firebird 1 172 Aug-01-2019, 12:33 AM
Last Post: scidam
  ValueError: Input contains infinity or a value too large for dtype('float64') Rabah_r 1 1,086 Apr-06-2019, 11:08 AM
Last Post: scidam
  ValueError: shapes naveen73 0 286 Jan-27-2019, 01:15 PM
Last Post: naveen73
  ValueError: Found input variables with inconsistent numbers of samples: [0, 3] ayaz786amd 2 3,014 Nov-27-2018, 07:12 AM
Last Post: ayaz786amd
  ValueError: could not convert the string to float Grin 3 5,141 Jun-14-2018, 08:17 PM
Last Post: killerrex
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 8,177 Apr-02-2018, 12:03 AM
Last Post: scidam
  Error: ValueError: could not convert string to float: 'L200 1.6 D/C' Jaarroy 2 2,890 Jan-18-2018, 02:00 PM
Last Post: Jaarroy
  ValueError: could not convert string to float: Close?? BlackHeart 3 18,136 Oct-27-2017, 08:42 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)