Hello, Machine Learning
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 1 of 60.
Most programs follow rules a person wrote. A machine learning model finds its own rules, from examples.
Here are 150 iris flowers, each one a dot placed by its petal size:
See the three groups? A model can learn them too, then name flowers it has never seen.
Challenge
EasyThis program is ready. Press Run: the model studies 112 flowers, then names 38 new ones.
Open the Plots tab to see its guesses. Can you spot its one mistake? Then submit.
Try it yourself
# Press Run to watch a model learn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
# 150 iris flowers: their measurements and their species
iris = load_iris()
X_learn, X_new, y_learn, y_new = train_test_split(iris.data, iris.target, random_state=0)
# The model studies 112 flowers with their answers...
model = KNeighborsClassifier()
model.fit(X_learn, y_learn)
# ...then names 38 flowers it has never seen
guesses = model.predict(X_new)
right = (guesses == y_new).sum()
print(f"Studied {len(X_learn)} flowers")
print(f"Named {right} of {len(X_new)} new flowers correctly")
# Each new flower is a dot, colored by the model's guess; a red X marks a mistake
fig, ax = plt.subplots()
for i, name in enumerate(iris.target_names):
picked = guesses == i
ax.scatter(X_new[picked, 2], X_new[picked, 3], label=f"guessed {name}")
wrong = guesses != y_new
ax.scatter(X_new[wrong, 2], X_new[wrong, 3], marker="x", color="red", s=120, label="mistake")
ax.set_title("The model's guesses for 38 new flowers")
ax.set_xlabel("petal length (cm)")
ax.set_ylabel("petal width (cm)")
ax.legend()
plt.show()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler