0

I am trying to predict the insurance cost of an individual in the United States using K Nearest Neighbors

I have succesfully trained the data set and it started trowing the error when i tried to predict

ValueError: Expected 2D array, got 1D array instead:
array=[19.    27.896  0.     1.     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.

while while executing the following code:

import pandas as pd 
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)


insurance = pd.read_csv(r"C:\Users\USER\Desktop\data set\insurance.csv")
insurance2 = pd.read_csv(r"C:\Users\USER\Desktop\data set\insurance.csv")


insurance['charges'] = insurance['charges'].astype('int')

sex_s=[]
for i in insurance['sex']:
    if i == "female":
        sex_s.append(1)
    elif i == "male":
        sex_s.append(0)
    
insurance2['sx']=sex_s

smk=[]
for y in insurance['smoker']:
    if y == "yes":
        smk.append(1)
    elif y == "no":
        smk.append(0)

insurance2['smks']=smk



insurance2 = insurance2.drop(['charges',"region", 'sex','smoker'], axis =1)

insurance2.rename(columns={'smks':'smoker', 'sx':'sex'}, inplace = True)


insurance2['bmi'] = insurance2['bmi'].astype('int')
insurance2['children'] = insurance2['children'].astype('int')
insurance2['sex'] = insurance2['sex'].astype('int')
insurance2['smoker'] = insurance2['smoker'].astype('int')
insurance2['age'] = insurance2['age'].astype('int')
insurance2.dtypes


X = insurance2
y = insurance['charges']


knn.fit(X,y)

knn.predict([19, 27.896, 0, 1, 0])

this is my first time trying to predict

1 Answer 1

1

I think you are just missing an extra pair of square brackets on the final line, if you have a look in the docs (https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html) you will see they use predict with the following:

neigh.predict([[1.1]])

So just replace

knn.predict([19, 27.896, 0, 1, 0])

with

knn.predict([[19, 27.896, 0, 1, 0]])

For future reference, make sure to look up your question first to check it doesn't exist, as this is a duplicate of Error in Python script "Expected 2D array, got 1D array instead:"?.

Sign up to request clarification or add additional context in comments.

2 Comments

Your welcome, if you could accept the answer then other people will know it has been fixed too.
You just have to click on the check mark beside the answer to toggle it from greyed out to filled in.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.