Summary - ML fundamentals
- Machine Learning course
- Beginner
- Lesson 6/112
- 6 min
In this group you learned the definition, types, and core concepts of ML.
Summary: the ML workflow
In this section you saw the basic machine learning loop:
- Prepare the data as
X(two-dimensional) andy(one-dimensional). - Split it into training and test sets with
train_test_split. - Train the model with
fit(X_train, y_train). - Predict with
predictand measure on the test set withscore.
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)
model = KNeighborsClassifier().fit(X_tr, y_tr)
print(model.score(X_te, y_te))Practice End to end
Split the data with test_size=0.25, random_state=0, train a KNeighborsClassifier(n_neighbors=3) and print the test accuracy.
Starter code
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X = [[i] for i in range(40)]
y = [0 if i < 20 else 1 for i in range(40)]
# buraya yazSample solution
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X = [[i] for i in range(40)]
y = [0 if i < 20 else 1 for i in range(40)]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))Expected output
1.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With which data should the model's success be measured?
- There's no need to measure
- With test data not used in training Correct answer
- With the training data
- With all the data
Explanation: Real success is success on examples the model has never seen.
2 Measure success on the test set.
print(model.____(X_test, y_test))
print(model.score(X_test, y_test))
Explanation: score gives accuracy for classifiers and the R² value for regression models.
3 Match the steps with their methods.
| Term | Meaning |
|---|---|
Splitting |
train_test_split |
Training |
fit |
Prediction |
predict |
Measuring |
score |