STAT 361 Data Analysis - Yale University WORKSHEET 3.8: SUPERVISED LEARNING IN PYTHON Import house-votes-84 (edited).csv. Write codes necessary to import and examine this dataset. Which of the
following statements is no
...
STAT 361 Data Analysis - Yale University WORKSHEET 3.8: SUPERVISED LEARNING IN PYTHON Import house-votes-84 (edited).csv. Write codes necessary to import and examine this dataset. Which of the
following statements is not true? The target variable in this DataFrame is ‘party’.
A. The DataFrame has a total of 232 rows and 17 columns.
B. Except for party, all of the columns are of type int64.
C. The first row of the DataFrame consists of votes by a Democrat and the second row consists of votes by a
Republican.
D. There are 17 predictor variables, or features, in this DataFrame.
D
Output
2 Date:
Perform graphical exploratory data analysis on the house votes dataset. Use Seaborn’s countplot to visualize the votes to the
satellite testing bill, grouped by party. Include the following line before the show function: plt.xticks([0,1], [‘No’,
‘Yes’]). Do the same for the missile bill. Of the two bills, which one/s do Democrats vote resoundingly in favor of, compared
to Republicans?
Page 1 of 4
This study source was downloaded by 100000861003072 from CourseHero.com on 04-27-2023 09:03:54 GMT -05:00
https://www.coursehero.com/file/83165998/WS38STAMARIAdocx/Jian Karlo R. Sta. Maria APPLIED DATA SCIENCE
WORKSHEET 3.8: SUPERVISED LEARNING IN PYTHON
A. Missile bill
B. Satellite bill
C. Both Missile and Satellite bills
D. Neither Missile nor Satellite bill
C
Output
3 Date:
Predict the party affiliation of the House member whose votes have been recorded in the file named x_new.csv. Write the code
here to achieve the following output:
Party Prediction: [‘democrat’/’republican’]
Code
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
hv84=pd.read_csv('house-votes-84 (edited).csv')
x_new=pd.read_csv('x_new.csv')
y=hv84['party']. values
x=hv84.drop('party', axis=1).values
knn=KNeighborsClassifier(n_neighbors=16) # n_neighbors = no. of categories
knn.fit(x,y)
prediction=knn.predict(x_new)
print("Prediction: {}".format(prediction))
Output
Prediction: ['democrat']
4 Date:
Use train_test_split from sklearn on your House votes data. Use 70% of the data for training and the rest for testing.
Add the following arguments to train_test_split: random_state = 21, stratify = y. Print out the predictions for
the test set and the model score.
[Show More]