Train/test split + computing accuracy
It's time to practice splitting your data into training and test sets with the churn_df
dataset!
NumPy arrays have been created for you containing the features as X
and the target variable as y
.
This exercise is part of the course
Supervised Learning with scikit-learn
Exercise instructions
- Import
train_test_split
fromsklearn.model_selection
. - Split
X
andy
into training and test sets, settingtest_size
equal to 20%,random_state
to42
, and ensuring the target label proportions reflect that of the original dataset. - Fit the
knn
model to the training data. - Compute and print the model's accuracy for the test data.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the module
from ____ import ____
X = churn_df.drop("churn", axis=1).values
y = churn_df["churn"].values
# Split into training and test sets
X_train, X_test, y_train, y_test = ____(____, ____, test_size=____, random_state=____, stratify=____)
knn = KNeighborsClassifier(n_neighbors=5)
# Fit the classifier to the training data
____
# Print the accuracy
print(knn.score(____, ____))