Applying ML Algorithm to a dataset - Nori12/Machine-Learning-Tutorial GitHub Wiki

Machine Learning Tutorial

Applying ML Algorithm to a dataset

This example will use the KNearest Neighbors Algorithms

# Import the dataset library
from sklearn.datasets import load_iris

# Import the dataset split function
from sklearn.model_selection import train_test_split

# Import the ML Algorithm
from sklearn.neighbors import KNeighborsClassifier

# Store it in a variable
iris_dataset = load_iris()

# Split it into training set and test set. 75% and 25% is a good division.
X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'], iris_dataset['target'], random_state=0)

# Choose the algorithm
knn = KNeighborsClassifier(n_neighbors=1)

# Apply it
knn.fit(X_train, y_train)

# Verify the accuracy
print("Test set score: {:.2f}".format(knn.score(X_test, y_test)))

# Test set score: 0.97