Menu
CoddyTech

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:

Horizontal bars of four scaled weights. Toward buying, in blue: pages_viewed 0.87, minutes 0.12. Away from buying, in orange: from_ad -0.30, price_seen -0.72

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 weights

This 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 icon

Challenge

Medium

A 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.

  1. Fit a LogisticRegression on the scaled training rows.
  2. Print every weight, largest first (2 decimals), with its direction.
  3. 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: friends

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler