Weights as Evidence
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 18 of 58.
What makes a shop visitor buy? Scale the features, so every weight is per std, then read the weights as evidence:
X_scaled = StandardScaler().fit_transform(visits)
model = LogisticRegression(max_iter=1000)
model.fit(X_scaled, bought)
print(np.round(model.coef_[0], 2)) # one row of weightsThis prints the weights in the columns' order:
[ 0.87 0.12 -0.3 -0.72]The sign is the direction: positive pushes toward class 1, buying; negative, away. The size is the strength.
Scaling makes sizes comparable: unscaled, a price in cents would get a weight 100 times smaller than in dollars.
Challenge
MediumA language app asks which habits go with keeping a 30-day streak, or finishing a course. learners.csv holds six features and both labels; the program reads which label to study. The starter splits and scales.
- Fit a
LogisticRegressionon the scaled training rows. - Print every weight, largest first (2 decimals), with its direction.
- Print the strongest evidence, either way.
For another app:
friends: 1.02, pushes toward yes
reminder_on: 0.42, pushes toward yes
age: 0.06, pushes toward yes
daily_goal_min: -0.11, pushes toward no
lessons_week1: -0.2, pushes toward no
night_owl: -0.64, pushes toward no
Strongest evidence: friendsTry it yourself
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
learners = pd.read_csv("learners.csv")
features = ["daily_goal_min", "friends", "reminder_on", "lessons_week1", "night_owl", "age"]
label = input() # kept_streak or finished_course
X_train, X_test, y_train, y_test = train_test_split(learners[features], learners[label], test_size=0.25, random_state=0)
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter4Logistic Regression
Yes-or-No PredictionsThe Decision ThresholdWeights as EvidenceSeveral ClassesRecap: Spam Filter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler