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.
Questo esercizio fa parte del corso
Supervised Learning with scikit-learn
Istruzioni dell'esercizio
- Import
train_test_splitfromsklearn.model_selection. - Split
Xandyinto training and test sets, settingtest_sizeequal to 20%,random_stateto42, and ensuring the target label proportions reflect that of the original dataset. - Fit the
knnmodel to the training data. - Compute and print the model's accuracy for the test data.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# 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(____, ____))