Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

(2) What do they not do so well?

Answered: 1 week ago

Question

(2) What do they not do so well?

Answered: 1 week ago

Question

(7) How are you measuring progress and benefits?

Answered: 1 week ago