Question
Hi, I need help for the below question on the coding. Previously I received an answer for my KNN function in Chegg here. So now
Hi, I need help for the below question on the coding. Previously I received an answer for my KNN function in Chegg here. So now my issue is I unable to perform the KNN class for the confusion matrix in my next code cell. Please help me on the coding for my question in PART 3.
Let me show you the KNN class function below: THIS PART NO NEED TO PROVIDE CODING, IS JUST TO SHOW YOU ONLY.
class KNN:
"""
Class to store data for regression problems
"""
def __init__(self, x_train, y_train, K=5):
"""
Creates a kNN instance
:param x_train: numpy array with shape (n_rows,1)- e.g. [[1,2],[3,4]]
:param y_train: numpy array with shape (n_rows,)- e.g. [1,-1]
:param K: The number of nearest points to consider in classification
"""
# Import and build the BallTree on training features
from sklearn.neighbors import BallTree
self.balltree = BallTree(x_train)
# Cache training labels and parameter K
self.y_train = y_train
self.K = K
def majority(self, neighbor_indices, neighbor_distances=None):
"""
Given indices of nearest neighbors in training set, return the majority label.
Break ties by considering 1 fewer neighbor until a clear winner is found.
:param neighbor_indices: The indices of the K nearest neighbors in self.X_train
:param neighbor_distances: Corresponding distances from query point to K nearest neighbors.
"""
# your code here
K = self.K
while True:
labels = [self.y_train[i] for i in neighbor_indices[:K]]
label_counts = Counter(labels)
majority_label, count = label_counts.most_common(1)[0]
if count > K/2:
return majority_label
K -= 1
def classify(self, x):
"""
Given a query point, return the predicted label
:param x: a query point stored as an ndarray
"""
# your code here
distances, indices = self.balltree.query(x.reshape(1, -1), k=self.K)
return self.majority(indices[0])
def predict(self, X):
"""
Given an ndarray of query points, return yhat, an ndarray of predictions
:param X: an (m x p) dimension ndarray of points to predict labels for
"""
# your code here
m, p = X.shape
yhat = np.zeros(m)
for i in range(m):
yhat[i] = self.classify(X[i])
return yhat
MY QUESTION AND MY ERROR:
Part 3 : Checking how well your classifier does Use your KNN class to perform KNN on the validation data with =3 and do the following:
MY ANSWER:
from sklearn import neighbors
from sklearn.metrics import confusion_matrix
import sklearn.metrics as metrics
import seaborn as sns
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
knn = neighbors.KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train,y_train)
val_yhat = knn.predict(val_x)
cm = confusion_matrix(val_y, val_yhat)
f, ax =plt.subplots(figsize = (5,5))
sns.heatmap(cm,annot = True, linewidths= 0.5, linecolor="red", fmt=".0f", ax=ax)
plt.xlabel("y_pred")
plt.ylabel("y_true")
plt.show()
print(metrics.confusion_matrix(val_y, val_yhat))
print (metrics.classification_report(val_y,val_yhat))
My first submission error was: reshape
My second submission error was:
ValueError: Found input variables with inconsistent numbers of samples: [3, 70000]
My third submission error was:
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
I don't know how to code already. Please help me on the coding for the prediction of KNN class with Confusion Matrix. Thank you so much for the help.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started