Question

Split it into training and test data. Apply logistic regression and KNN algorithm. You can use...

Split it into training and test data. Apply logistic regression and KNN algorithm. You can use R or Python to implement them. Which one of the algorithm fares better?

The dataset can be found at https://www.kaggle.com/uciml/indian-liver-patient-records/version/1

Much much thx for help

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The python 3 code for KNN is given below. Make sure you have libraries like numpy, pandas and sklearn installed in python.

******************KNN_liver_model.py*******************************

import numpy as np
from sklearn import neighbors,model_selection

import pandas as pd
from math import nan

#load the dataset
df=pd.read_csv('indian_liver_patient.csv')

#replace NaN or empty cells with 0.5
df.replace(nan, 0.5, inplace=True)
#encode Male as 1 and Female as 2
df.replace({'Male':1.0,'Female':2.0},inplace=True)

#X contains the features and Y contains the final classification
X=np.array(df.drop(['Dataset'],1))
y=np.array(df['Dataset'])


#keep 0.1% as the test data
X_train,X_test,y_train,y_test=model_selection.train_test_split(X,y,test_size=0.1)

clf=neighbors.KNeighborsClassifier(n_neighbors=4);
clf.fit(X_train,y_train)
accuracy=clf.score(X_test,y_test)

print('The accuracy is:'+str(accuracy))

Add a comment
Know the answer?
Add Answer to:
Split it into training and test data. Apply logistic regression and KNN algorithm. You can use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Split it into training and test data. Apply logistic regression and KNN algorithm. You can use R ...

    Split it into training and test data. Apply logistic regression and KNN algorithm. You can use R or Python to implement them. Which one of the algorithm fares better? The dataset can be found at https://www.kaggle.com/uciml/indian-liver-patient-records/version/1 Much much thx for help

  • Problem 1 (Logistic Regression and KNN). In this problem, we predict Direction using the data Weekly.csv....

    Problem 1 (Logistic Regression and KNN). In this problem, we predict Direction using the data Weekly.csv. a. i. Split the data into one training set and one testing set. The training set contains observations from 1990 to 2008 (Hint: we can use a Boolean vector train=(Year < 2009)). The testing set contains observations in 2009 and 2010 (Hint: since train is a Boolean vector here, should use ! symbol to reverse the elements of a Boolean vector to obtain the...

  • Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm...

    Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm to solve a classification problem. The kNN is a simple and robust classifier, which is used in different applications. The goal is to train kNN algorithm to distinguish the species from one another. The dataset can be downloaded from UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/ (Links to an external site.)Links to an external site.. Download `iris.data` file from the Data Folder. The Data Set description...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT