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.
valuesto the end ofdata.iloc[:,:-1]change anything?