This file contains hidden or 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
X_train, X_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state=0) | |
knn = KNeighborsClassifier(n_neighbors=1) | |
knn.fit(X_train, y_train) | |
print("Test set score: {:.2f}".format(knn.score(X_test, y_test))) |
This file contains hidden or 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
y_pred = knn.predict(X_test) | |
print("Test set score: {:.2f}".format(np.mean(y_pred == y_test))) |
This file contains hidden or 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
import numpy as np | |
X_new = np.array([[5, 2.9, 1, 0.2]]) | |
prediction = knn.predict(X_new) | |
print("Predicted target name: {}".format(iris_dataset['target_names'][prediction])) |
This file contains hidden or 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 sklearn.neighbors import KNeighborsClassifier | |
# Instanciar la clase en un objeto. | |
# Establecemos el numero de vecinos en 1 | |
knn = KNeighborsClassifier(n_neighbors=1) | |
# Construir el modelo a partir del conjunto de entrenamiento | |
knn.fit(X_train, y_train) |
This file contains hidden or 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
import pandas as pd | |
import mglearn | |
# Crea dataframe a partir de datos en X_train | |
# Etiquetar las columnas usando iris_dataset.feature_names | |
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names) | |
# Crea una matriz de dispersión a partir de iris_dataframe, coloreado por y_train | |
grr = pd.pandas.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o', | |
hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3) |
This file contains hidden or 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
# Cargamos el conjunto de datos de Iris | |
from sklearn.datasets import load_iris | |
iris_dataset = load_iris() | |
# Claves del objeto iris_dataset | |
# Podemos ver la descripcion, los datos, las caraterísticas, etc. del conjunto de datos | |
print("Keys of iris_dataset: \n{}\n\n".format(iris_dataset.keys())) | |
print(iris_dataset['feature_names']) | |
# Divide los datos de entrenamiento (75%) y de prueba (25%) |