Question: Show me the steps to solve Problem: The dataset SDSS contains 1 7 observational features and one class feature for 1 0 0 0 0

Show me the steps to solve
Problem:
The dataset SDSS contains 17 observational features and one class feature for 10000 deep sky objects observed by the Sloan Digital Sky Survey. Use sklearn's KNeighborsClassifier function to perform kNN classification to classify each object by the object's redshift and u-g color.
Import the necessary modules for kNN classification
Create a dataframe X with features redshift and u_g
Create dataframe y with feature class
Initialize a kNN model with k=3
Fit the model using the training data
Find the predicted classes for the test data
Calculate the accuracy score and confusion matrix
Code Snippet:
# Import needed packages for classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
# Import packages for evaluation
import pandas as pd
import numpy as np
# Load the dataset
skySurvey = pd.read_csv('SDSS.csv')
# Create a new feature from u - g
skySurvey['u_g']= skySurvey['u']- skySurvey['g']
# Create dataframe X with features redshift and u_g
X = skySurvey[['redshift','u_g']]# Your code here
# Create dataframe y with feature class
y = skySurvey['class']# Your code here
np.random.seed(42)
# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Initialize model with k=3
skySurveyKnn = KNeighborsClassifier(n_neighbors=3)# Your code here
# Fit model using X_train and y_train
skySurveyKnn.fit(X_train,y_train)
# Find the predicted classes for X_test
y_pred = skySurveyKnn.predict(X_test)# Your code here
# Calculate accuracy score
score = accuracy_score(y_test, y_pred)# Your code here
# Print accuracy score
print('Accuracy score is ', end="")
print('%.3f'% score)
# Print confusion matrix
print(confusion_matrix(y_test,y_pred))
But the ouput is coming as like this
Output differs. See highlights below.
Special character legend
Your output
Accuracy score is 0.984
[[1463529]
[122740]
[301214]]
Expected output
Accuracy score is 0.984
[[1463123]
[52740]
[2901214]]
Kindly help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!