Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

knn.score sklearn

# Author: Pablo Marcos Manchón
# License: MIT
# https://fda.readthedocs.io/en/latest/auto_examples/plot_k_neighbors_classification.html
  
import skfda
from skfda.ml.classification import KNeighborsClassifier

from sklearn.model_selection import (train_test_split, GridSearchCV,
                                     StratifiedShuffleSplit)

import matplotlib.pyplot as plt
import numpy as np

X, y = skfda.datasets.fetch_growth(return_X_y=True, as_frame=True)
X = X.iloc[:, 0].values
y = y.values

# Plot samples grouped by sex
X.plot(group=y.codes, group_names=y.categories)

y = y.codes

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
                                                    stratify=y, random_state=0)
                                                    
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)

pred = knn.predict(X_test)
print(pred)

# The score() method allows us to calculate 
# the mean accuracy for the test data.
score = knn.score(X_test, y_test)
print(score)
 
PREVIOUS NEXT
Tagged: #sklearn
ADD COMMENT
Topic
Name
5+7 =