Skip to content

Instantly share code, notes, and snippets.

@felipextrindade
Created September 24, 2018 00:49
Show Gist options
  • Save felipextrindade/a476a590ffac2c9021656a2d0ab2e8ad to your computer and use it in GitHub Desktop.
Save felipextrindade/a476a590ffac2c9021656a2d0ab2e8ad to your computer and use it in GitHub Desktop.
Machine Learning Example: Iris Flower Dataset
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
iris_dataset = load_iris()
print("Target names: {}".format(iris_dataset['target_names']))
print("Feature names: {}".format(iris_dataset['feature_names']))
print("Type of data: {}".format(type(iris_dataset['data'])))
print("Shape of data: {}".format(iris_dataset['data'].shape))
print("Type of target: {}".format(type(iris_dataset['target'])))
print("Shape of target: {}".format(iris_dataset['target'].shape))
print("Target:\n{}".format(iris_dataset['target']))
X_train, X_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state=0)
print("X_train shape: {}".format(X_train.shape))
print("y_train shape: {}".format(y_train.shape))
# Same for the test samples
print("X_test shape: {}".format(X_test.shape))
print("y_test shape: {}".format(y_test.shape))
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
X_new = np.array([[5, 2.9, 1, 0.2]])
print("X_new.shape: {}".format(X_new.shape))
prediction = knn.predict(X_new)
print("Prediction: {}".format(prediction))
print("Predicted target name: {}".format(iris_dataset['target_names'][prediction]))
y_pred = knn.predict(X_test)
print("Test set predictions:\n {}".format(y_pred))
print("Test set score (np.mean): {:.2f}".format(np.mean(y_pred == y_test)))
print("Test set score (knn.score): {:.2f}".format(knn.score(X_test, y_test)))
@priyansh17
Copy link

dataset link?

Classic dataset is fetched. its present already there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment