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:
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
MediumA 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.
- Fit a
LinearRegressionof the cost on thefeatures. - For each feature, print what one more adds or takes off (2 decimals).
- 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 dollarsTry 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
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 Sorter2Nearest 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 Model3Linear Regression
Predicting NumbersSeveral FeaturesMeasuring ErrorsResidual PlotsRecap: Bike RentalsPractice on your own: Online Python compiler