I am a beginner and I am designing an binary classifier using Perceptron algorithm using FASHION-MNIST dataset. While designing the same I have written the following code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.optimizers import RMSprop,Adam
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
import warnings
import os

def read_dataset(train_path, test_path):

    train = pd.read_csv(train_path)
    test = pd.read_csv(test_path)

    y_train = train["label"]
    x_train = train.drop(labels = ["label"],axis = 1)  #throw the labels column

    y_test = test["label"]
    x_test = test.drop(labels = ["label"], axis = 1)

    x_train = x_train / 255.0
    x_test = x_test / 255.0

    x_train = x_train.values.reshape(-1,28,28,1)
    x_test = x_test.values.reshape(-1,28,28,1)

    y_train = to_categorical(y_train, num_classes = 10)
    #x_train = to_categorical(x_train, num_classes = 10)

    y_train = y_train.astype(int)
    y_test = y_test.astype(int)

    #y_train = y_train.reshape((y_train.shape[0]))
    #y_test = y_test.reshape((y_test.shape[0]))

    #x_train = x_train.iloc[:,:].values
    #x_test = x_test.iloc[:,:].values

    #y_train = y_train.iloc[:,:].values
    #y_test = y_test.iloc[:,:].values

    return (x_train, y_train, x_test, y_test)

def peptrain(x, y, x_var_test, y_var_test):
    w = np.zeros((x.shape[1]))
    maxiter = []
    errors = []
    train_acc = []
    test_acc = []

    for i in range(5):
        error = 0
        for t in range(x.shape[0]):
            y_cap = np.sign(np.dot(w,x[t]))

            if y_cap == 0:
                y_cap = -1

            if y_cap != y[t]:
                error = error + 1
                w = w + y[t] * x[t]

        maxiter.append(i + 1)

        trainacc = 1 - ( error / x.shape[0] ) 
        testacc = peptest(x_var_test, y_var_test, w) 


    return (train_acc, test_acc, maxiter, errors)

def peptest(x, y, w):
    mistakes = 0
    for t in range(x.shape[0]):
        y_cap = np.sign(np.dot(w,x[t]))

        if y_cap == 0:
            y_cap = -1

        if y_cap != y[t]:
            mistakes = mistakes + 1

    return (1 - (mistakes / x.shape[0])) 

if __name__ == '__main__':

    train_location = "/Users/coraljain/Downloads/fashionmnist/fashion-mnist_train.csv"
    test_location = "/Users/coraljain/Downloads/fashionmnist/fashion-mnist_test.csv"

    x_train, y_train, x_test, y_test = read_dataset(train_location, test_location)
    train_acc, test_acc, maxiter, errors = peptrain(x_train, y_train, x_test, y_test)

Here I am getting the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

for the following part.

if y_cap == 0:

What option do I have here? How am I supposed to use all and any function?


If I understood correctly your code, this should be fixed just by doing

if (y_cap == np.zeros(len(y_cap))).all():

assuming that y_cap is a numpy array (by the way, providing an example for y_cap would help).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.