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, k-Nearest 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.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the dataset
auto = pd.read_csv(r"C:\\Users\.csv")
# Create X and y
X = auto[['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model year']]
y = auto['origin']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create and fit the model
lr = LogisticRegression(multi_class='multinomial', solver='newton-cg')
lr.fit(X_train, y_train)
# Make predictions on the test set
lr_pred = lr.predict(X_test)
# Evaluate the model
lr_accuracy = accuracy_score(y_test, lr_pred)
print('Accuracy of LR model:',100*lr_accuracy, '%')
# Create and fit the model
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
# Make predictions on the test set
knn_pred = knn.predict(X_test)
# Evaluate the model
knn_accuracy = accuracy_score(y_test, knn_pred)
print('Accuracy of KNN model:', knn_accuracy)
# Create and fit the model
svc = SVC(kernel='linear', C=1, decision_function_shape='ovr')
svc.fit(X_train, y_train)
# Make predictions on the test set
svc_pred = svc.predict(X_test)
# Evaluate the model
svc_accuracy = accuracy_score(y_test, svc_pred)
print('Accuracy of SVM model: ', svc_accuracy)
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!
Q: Look for the multiclass classifiers in

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 Programming Questions!