Question: Q: Look for the multiclass classifiers in Logistic Regression, k - Nearest Neighbors and Support Vector Machine. Apply them to analyze AutoMPG and discuss the
Q: Look for the multiclass classifiers in Logistic Regression, kNearest Neighbors and Support Vector
Machine. Apply them to analyze AutoMPG and discuss the results. The target is to classify the origin of the
car and mpg can be included in the X
Code:
import pandas as pd
import numpy as np
from sklearn.linearmodel import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.modelselection import traintestsplit
from sklearn.metrics import accuracyscore
# Load the dataset
auto pdreadcsvrC:Userscsv
# Create X and y
X autompg 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model year'
y autoorigin
# Split the data into training and testing sets
Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate
# Create and fit the model
lr LogisticRegressionmulticlass'multinomial', solver'newtoncg
lrfitXtrain, ytrain
# Make predictions on the test set
lrpred lrpredictXtest
# Evaluate the model
lraccuracy accuracyscoreytest, lrpred
printAccuracy of LR model:lraccuracy,
# Create and fit the model
knn KNeighborsClassifiernneighbors
knnfitXtrain, ytrain
# Make predictions on the test set
knnpred knnpredictXtest
# Evaluate the model
knnaccuracy accuracyscoreytest, knnpred
printAccuracy of KNN model: knnaccuracy
# Create and fit the model
svc SVCkernel'linear', C decisionfunctionshape'ovr'
svcfitXtrain, ytrain
# Make predictions on the test set
svcpred svcpredictXtest
# Evaluate the model
svcaccuracy accuracyscoreytest, svcpred
printAccuracy of SVM model: svcaccuracy
As for the question, I got the above code to calculate the "accuracy". Can you give me the code to calculate "sensitivity", "specificity", and "precision"? And improve the above code. Thank you!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
