Menu
CoddyTech

Several Features

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 12 of 58.

A house's price depends on more than size: a long walk to the station lowers it. Learning from both, a model draws parallel lines:

Ten houses as dots, price against area: houses near the station in blue, far ones as orange squares. Two parallel model lines, for a 5 and a 25 minute walk; the 25 minute line sits 102 thousand dollars lower at every size

In code, X gets both columns and coef_ one weight per feature:

X = houses[["area_m2", "minutes_walk"]]
model = LinearRegression()
model.fit(X, houses["price"])
print(np.round(model.coef_, 1))

This prints:

[ 2.6 -5.1]

The weights follow X's columns. Each extra m² adds 2.6 thousand dollars; each extra minute's walk takes 5.1 thousand off, the other feature held still.

A weight's unit is the target's per unit of its feature: thousands per m², per minute.

challenge icon

Challenge

Medium

A caterer quotes new events from 40 past ones in events.csv: guests, courses, months_ahead (how early it was booked) and cost in dollars. The starter reads a new event into new_event.

  1. Fit a LinearRegression of the cost on the features.
  2. For each feature, print what one more adds or takes off (2 decimals).
  3. Print the new event's quote (2 decimals).

For another caterer:

Each extra guest adds 41.5 dollars
Each extra course adds 198.25 dollars
Each extra month booked ahead takes off 22.8 dollars
Quote: 4312.6 dollars

Try it yourself

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# One row per past event: guests, courses, months booked ahead, cost in dollars
events = pd.read_csv("events.csv")
features = ["guests", "courses", "months_ahead"]
units = ["guest", "course", "month booked ahead"]
# The new event, as a table with one row
guests = int(input())
courses = int(input())
months = int(input())
new_event = pd.DataFrame({"guests": [guests], "courses": [courses], "months_ahead": [months]})

# 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