0% found this document useful (0 votes)
90 views2 pages

Knn1 HouseVotes

This document summarizes code that uses a k-nearest neighbors (KNN) classifier to predict whether members of the US House of Representatives are democrats or republicans based on their votes on 16 issues. The code loads voting data, splits it into feature (X) and target (y) variables, trains a KNN model with 6 neighbors on the data, predicts the labels of both the training data and new data, and prints the prediction for the new data which is 'democrat'.

Uploaded by

Joe1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views2 pages

Knn1 HouseVotes

This document summarizes code that uses a k-nearest neighbors (KNN) classifier to predict whether members of the US House of Representatives are democrats or republicans based on their votes on 16 issues. The code loads voting data, splits it into feature (X) and target (y) variables, trains a KNN model with 6 neighbors on the data, predicts the labels of both the training data and new data, and prints the prediction for the new data which is 'democrat'.

Uploaded by

Joe1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9/7/2018 komal_knn1_sayan_houseVotes

In [4]: # Import plotting modules


import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn import datasets
plt.style.use('ggplot')

from sklearn.neighbors import KNeighborsClassifier

In [2]: # Numerical EDA


# Predict party affiliation based on votes
# made by US House of Representatives Congressmen
#https://archive.ics.uci.edu/ml/datasets/Congressional+Voting+Records
location = "D:\komal\SIMPLILEARN\MY COURSES\IN PROGRESS\MACHINE LEARNING RECOR
DINGS\Jul 28 Sat - Aug 25 Sat\Drive downloads\Machine Learning _ Jul 28 - Aug
25 _ Sayan\datasets\house-votes-84.csv"
column_names=['party','infants','water','budget','physician',
'salvador','religious','satellite','aid','missile',
'immigration','synfuels','education','superfund',
'crime','duty_free_exports','eaa_rsa']
df = pd.read_csv(location,header=None,names=column_names)

df.replace({'n':0,'y':1,'?':0},inplace=True)

In [5]: # Create arrays for the features and the response variable
y = df['party'].values
X = df.drop('party', axis=1).values

# Create a k-NN classifier with 6 neighbors


knn = KNeighborsClassifier(n_neighbors=6)

# Fit the classifier to the data


knn.fit(X,y)

Out[5]: KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',


metric_params=None, n_jobs=1, n_neighbors=6, p=2,
weights='uniform')

In [6]: X_new = pd.DataFrame([0.700181,0.620683,0.916841,0.722895,0.272337,


0.660382,0.250985,0.75609,0.784475,0.752666,
0.074864,0.597837,0.647635,0.685137,0.739113,
0.417089]).T

In [7]: X_new

Out[7]:
0 1 2 3 4 5 6 7

0 0.700181 0.620683 0.916841 0.722895 0.272337 0.660382 0.250985 0.75609 0.78447

file:///D:/komal/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/codes%20in%20pdf/komal_knn1_sayan_houseVotes.html 1/2
9/7/2018 komal_knn1_sayan_houseVotes

In [8]: # Predict the labels for the training data X


y_pred = knn.predict(X)

# Predict and print the label for the new data point X_new
new_prediction = knn.predict(X_new)
print("Prediction: {}".format(new_prediction))

Prediction: ['democrat']

file:///D:/komal/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/codes%20in%20pdf/komal_knn1_sayan_houseVotes.html 2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy