Machine Learning
Published: 2021-09-03 07:34:08

Hello, I am working on a project to test my skills on Classification models but seems like I made a error and am not sure how to solve it. Below is my code and error:    

Required: Implement different algorithms like decision trees, logistic regression, and svm to see which gives better accuracy. Compare the results of each algorithm and understand the behavior of models.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn import svm
from sklearn import metrics
 
def main():
    x, y = loadData()
    x_train, x_test, y_train, y_test = trainTest(x,y)
    logisticAccuracy = logisticRegressionModel(x_train, x_test, y_train, y_test)
    dectreeAccuracy = decisionTreeModel(x_train, x_test, y_train, y_test)
    svmAccuracy = svmModel(x_train, x_test, y_train, y_test)
    printComparisonResults(logisticAccuracy,dectreeAccuracy,svmAccuracy)
    
def loadData():
    df = pd.read_csv('D:\Tutorials\Resources\creditcard.csv')
    x = df[['Time','V1','V2','V3','V5','V6','V7','V8','V9','V10','V11','V12',\
            'V13','V14','V5','V16','V17','V18','V19','V20','V21','V22','V23',\
                'V24','V25','V26','V27', 'V28','Amount']]
    y = df['Class']    
    return x, y
 
def trainTest(x,y):
    x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.25, random_state=0)
    return x_train, x_test, y_train, y_test
 
#LogisticRegression model
def logisticRegressionModel(x_train, x_test, y_train, y_test):
    logistic_reg = LogisticRegression().fit(x_train, y_train)
    y_pred = logistic_reg.predict(x_test)
    
    #evaluate accuracy of our model
    acc = metrics.accuracy_score(y_test, y_pred)
    
    return acc
def decisionTreeModel(x_train, x_test, y_train, y_test):
    dec_tree = DecisionTreeClassifier()
    dec_tree = dec_tree.fit(x_train, y_train)
    y_pred = dec_tree.predict(x_test)
    
    #evaluate model
    acc = metrics.accuracy_score(y_test, y_pred)
    
    return acc
 
def svmModel(x_train, x_test, y_train, y_test):
    svm_model = svm.SVC(kernel='linear')
    svm_model = svm_model.fit(x_train, y_train)
    y_pred = svm_model.predict(y_test)
    
    #evaluate model
    acc = metrics.accuracy_score(y_test, y_pred)
    
    return acc
 
def printComparisonResults(logisticAccuracy,dectreeAccuracy,svmAccuracy):
    print("Printing Results")
    
    lr = "Logistic Regression"
    dt = "Decision Tree"
    sv = "SVM is Accurate"
    
    if logisticAccuracy > (dectreeAccuracy and svmAccuracy):
        print(lr, "is accurate than", dt, "and", sv)
    elif dectreeAccuracy > (logisticAccuracy and svmAccuracy):
        print(dt, "is accurate than", lr, "and", sv)
    else:
        print(sv, "is accurate than", lr, "and", dt)
        
    print("Done")
        
if __name__ == '__main__': main()

#error am getting
ValueError: Expected 2D array, got 1D array instead:
array=[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.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. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
Have reshaped my array to x = np.array(df[[...]].reshape((1,-1)) but still getting an error:

 raise ValueError("Found input variables with inconsistent numbers of"

ValueError: Found input variables with inconsistent numbers of samples: [1, 2470]

Have reshaped my array to x = np.array(df[[...]].reshape((-1,1)) but still getting an error, how should I go about it?

 raise ValueError("Found input variables with inconsistent numbers of"

ValueError: Found input variables with inconsistent numbers of samples: [71630, 2470]