1 of 13

CSE 163

Section AX

​

Week 7

​

TA 1 & TA 2

2 of 13

Housekeepingđź§ą

Important Dates:

  • Section Check-in 8 due 11/20
  • Programming Practice 7 due 11/22
  • Homework 5: Mapping due 11/25
    • Only 1 resub opportunity!

​

​

Curious about exam format?

  • Past exams can be found under the Final Exam tab on the course website

​

2

3 of 13

Section Plan

​

In today’s section, we’ll go over

  • Machine learning
    • Training/testing procedure
    • Scikit-Learn pipeline for regression/tree models

3

4 of 13

Visualize the split of data

4

feat_train

lab_train

feat_test

lab_test

fit

Empty model

Trained model

5 of 13

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

6 of 13

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)

​

  • For tasks whose labels are numeric (e.g. House price prediction)
  • Models: DecisionTreeRegressor(), LinearRegression()

​

7 of 13

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)

​

  • For tasks whose labels are categorical (e.g. cat vs. dog)
  • Models: DecisionTreeClassifier()

​

8 of 13

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.

​

  • We need to hold out data our model will never see during training, then test it on that data set (test set)

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)

9 of 13

Underfitting vs. overfitting

Models won’t always perform well on test data.

Suppose we’re training a classification model…

  • Underfitting occurs when the model is too simple to capture the relationships in the data it’s trained on
    • 68% accuracy on training set, 65% accuracy on test set
  • Overfitting occurs when the model does too good of a job fitting to the specific data it’s trained on (a model that’s too complex “learns the noise”)
    • 98% accuracy on training set, 55% accuracy on test set

We want our models to be somewhere in between the two!

​

10 of 13

Practice Problems!

10

11 of 13

Section Code:

11

12 of 13

Solutions

12

13 of 13

Solutions

13