I am trying to get the accuracy of the model and I am getting this error

TypeError: Expected sequence or array-like, got

Here's my code sample.

%matplotlib inline
import matplotlib.pyplot as plt 
import pandas as pd

dataframe = pd.read_csv("WA_Fn-UseC_-HR-Employee-Attrition.csv")


# spare input and target variables
inputs = dataframe.drop('PerformanceRating', axis='columns')

target = dataframe['PerformanceRating']

from sklearn.preprocessing import LabelEncoder

MaritalStatus_ = LabelEncoder()
JobRole_ = LabelEncoder()
Gender_ = LabelEncoder()
EducationField_ = LabelEncoder()
Department_ = LabelEncoder()
BusinessTravel_ = LabelEncoder()
Attrition_ = LabelEncoder()
OverTime_ = LabelEncoder()
Over18_ = LabelEncoder()

inputs['MaritalStatus_'] = MaritalStatus_.fit_transform(inputs['MaritalStatus'])
inputs['JobRole_'] = JobRole_.fit_transform(inputs['JobRole'])
inputs['Gender_'] = Gender_.fit_transform(inputs['Gender'])
inputs['EducationField_'] = EducationField_.fit_transform(inputs['EducationField'])
inputs['Department_'] = Department_.fit_transform(inputs['Department'])
inputs['BusinessTravel_'] = BusinessTravel_.fit_transform(inputs['BusinessTravel'])
inputs['Attrition_'] = Attrition_.fit_transform(inputs['Attrition'])
inputs['OverTime_'] = OverTime_.fit_transform(inputs['OverTime'])
inputs['Over18_'] = Over18_.fit_transform(inputs['Over18'])

inputs.drop(['MaritalStatus', 'JobRole', 'Attrition' , 'OverTime' , 
                'Gender', 'EducationField', 'Department', 'BusinessTravel', 'Over18'], axis=1, inplace=True)

                       'JobRole_' : 'JobRole',
                       'Gender_' : 'Gender',
                       'EducationField_' : 'EducationField',
                       'Department_' : 'Department',
                       'BusinessTravel_' : 'BusinessTravel',
                       'Attrition_' : 'Attrition',
                       'OverTime_' : 'OverTime',
                       'Over18_' : 'Over18'}, inplace=True)


from sklearn import preprocessing

scaler = preprocessing.StandardScaler()
scaled_df = scaler.fit_transform(inputs)
scaled_df = pd.DataFrame(scaled_df, columns=inputs.columns)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import numpy as np

inputs = scaled_df
X_train, X_test, y_train, y_test = train_test_split(inputs, target, test_size=0.2)

model = RandomForestClassifier(ccp_alpha=0.01)

# train
pred = model.fit(X_train, y_train)
from sklearn.metrics import accuracy_score
accuracy_score(X_test,pred) #--------------> this is where I get the error

Can someone help me?


1 Answer 1

# train
pred = model.fit(X_train, y_train)

Here you are juts fitting the model, not making any predictions.


Here you are supposed to pass y_test, and y_predict, since its the output you are comparing not the input data.

Try this-

pred = model.predict(X_test)
  • $\begingroup$ I am getting score as 1 every time for accuracy_score(y_test,pred). What is the possible reason? $\endgroup$ May 24, 2020 at 10:49
  • $\begingroup$ looks like dateset is too small. how much is train accuracy? $\endgroup$ May 24, 2020 at 12:07
  • $\begingroup$ 0.88 means 88% accuracy. I have over 1400+ of rows $\endgroup$ May 24, 2020 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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