Created
August 28, 2019 17:40
-
-
Save pablomm/e7232262eec210b1a25612ec5567e01a to your computer and use it in GitHub Desktop.
Test knn fda.usc $ scikit-fda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from skfda.datasets import fetch_growth | |
from skfda.ml.classification import KNeighborsClassifier | |
data = fetch_growth() | |
fd = data['data'] | |
girls = fd[data['target'] == 1] | |
boys = fd[data['target'] == 0] | |
labels = [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,0,1] | |
knn = KNeighborsClassifier(3) | |
knn.fit(girls, labels) | |
knn.predict(boys) | |
# array([0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
# 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(fda) | |
library(fda.usc) | |
# Random data and labels | |
data("growth") | |
girls_data <- t(matrix(growth$hgtf, 31, 54)) | |
boys_data <- t(matrix(growth$hgtm, 31, 39)) | |
ages <- growth$age | |
girls <- fdata(girls_data, ages) | |
boys <- fdata(boys_data, ages) | |
# Random labels | |
label <- factor(c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,0,1)) | |
# Train knn with k=3 | |
model <- classif.knn(label, girls, knn=3) | |
# Predict labels | |
res <- predict.classif(model, boys) | |
print(res) | |
# 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 | |
res_python <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1) | |
res == res_python | |
# True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment