0


I am currently learning about knn and tried to do some forecast,
but it ended up with the following error: "Expected 2D array, got 1D array instead".
Although I looked up some threads, I don't know how to reshape the array.

import numpy as np
import matplotlib.pyplot as plt 
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn import metrics

data = pd.read_csv('justAnUrl')
X = data.iloc[:,:-1]
y = data.iloc[:,13].values
X_train, y_train, X_test, y_test = train_test_split(X, y,test_size=0.25, random_state=100)

sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

# a)
knn = KNeighborsClassifier(n_neighbors=5)
knn_list = []

for x in range(25):
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    matches = 0
    if y_pred[x] == y_test[x]:
        matches = matches + 1
    precision = matches / len(y_pred)
    knn_list.append(precision)

print(knn_list)

table's header
The data is about heart diseases.

Hope you can help me and others can learn from this example as well.

5
  • stackoverflow.com/questions/59508553/… check if it helps you Commented Jun 23, 2020 at 11:17
  • @DouglasFerreira If I do so, I just get told, that X has just 1 feature but the StandardScaler is expecting 13. Commented Jun 23, 2020 at 11:21
  • does adding values to the end of data.iloc[:,:-1] change anything? Commented Jun 23, 2020 at 11:25
  • Then maybe you want .reshape(1,-1) instead of .reshape(-1,1) Commented Jun 23, 2020 at 11:28
  • If I add values it does change nothing. If I swap the reshape values it says X has 227 features but the StandardScaler is expecting 13 features as input Commented Jun 23, 2020 at 11:32

1 Answer 1

2

Your train_test_split order is wrong, it should be X_train, X_test, y_train, y_test = train_test_split()

eidt: i.e. you're fitting scaler on X_train and also trying to transform y_train using that scaler. Sort out the order and it should be fine.

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

1 Comment

I tried this train_test_split, but it does not change the result. I still get the 2d array error

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.