Sie sind auf Seite 1von 2

9/7/2018

komal_knn1_sayan_houseVotes

from sklearn.neighbors import KNeighborsClassifier

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')

# 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 [2]: # Numerical EDA

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)

In [5]: # Create arrays for the features and the response variable

Out[5]: KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=6, p=2, weights='uniform')

0.660382,0.250985,0.75609,0.784475,0.752666,

0.074864,0.597837,0.647635,0.685137,0.739113,

0.417089]).T

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

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.7844

0.272337 0.660382 0.250985 0.75609 0.7844

9/7/2018

komal_knn1_sayan_houseVotes

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

In [8]: # Predict the labels for the training data X y_pred = knn.predict(X)

Prediction: ['democrat']