CSE 163
Section AX
Week 7
TA 1 & TA 2
Housekeepingđź§ą
Important Dates:
Curious about exam format?
2
Section Plan
In today’s section, we’ll go over
3
Visualize the split of data
4
feat_train
lab_train
feat_test
lab_test
fit
Empty model
Trained model
Visualize the split of data
5
feat_train
lab_train
feat_test
lab_test
fit
Empty model
Trained model
predictions
Compare with accuracy_score
predict
ML pipeline for regression tasks
6
# Separate data
features = data.loc[:, data.columns != 'target']
labels = data['target']
# Create and train model
model = DecisionTreeRegressor()
model.fit(features, labels)
# Predict on some data
predictions = model.predict(features)
# Assess accuracy
mean_squared_error(labels, predictions)
ML pipeline for classification tasks
7
# Separate data
features = data.loc[:, data.columns != 'target']
labels = data['target']
# Create and train model
model = DecisionTreeClassifier()
model.fit(features, labels)
# Predict on some data
predictions = model.predict(features)
# Assess accuracy
accuracy_score(labels, predictions)
Train-test split
The goal of machine learning is not to memorize the labels of the data we already have, but to learn a model that generalizes to future, unseen data.
from sklearn.model_selection import train_test_split
# Breaks data into 80% train and 20% test
X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=0.2)
Underfitting vs. overfitting
Models won’t always perform well on test data.
Suppose we’re training a classification model…
We want our models to be somewhere in between the two!
Practice Problems!
10
Section Code:
11
Solutions
12
Solutions
13