Lab 4 of 5about 40 minutes
From a table to a first prediction
It uses what you learned in Tables with pandas, Plotting to see data and The scikit-learn pattern.
Start here
Run itA new table: 398 cars sold from 1970 to 1982, made in the USA, Europe or Japan. For each one, its weight and the litres of fuel it burns to go 100 km. It comes from Auto MPG, in the UCI Machine Learning Repository (CC BY 4.0), with the weights turned into kilograms and the fuel into litres.
1The table
Run itFrom Tables with pandas
Run the cell: it shows the first five rows. The table has 398 rows and 7 columns, so pandas puts ... in place of the columns in the middle, as it did with the penguins. Horsepower is written 130.0: a column with a gap in it keeps decimals.
2The heavy cars
Your turnFrom Tables with pandas
Keep only the cars that weigh more than 1,500 kg, in heavy.
Hint
A comparison on one column gives True or False for each row. The table keeps the True rows.
One way to do it
heavy = cars[cars["weight_kg"] > 1500]
heavy.shape3Between two weights
Your turnFrom Tables with pandas
Now the cars between 1,000 and 1,500 kg. Try cars[cars["weight_kg"] > 1000 and cars["weight_kg"] < 1500]. pandas stops: The truth value of a Series is ambiguous. Mend it, and keep the rows in middle.
Hint
and wants one True or False. Join two columns of True and False with &, each comparison in its own round brackets.
One way to do it
middle = cars[(cars["weight_kg"] > 1000) & (cars["weight_kg"] < 1500)]
middle.shape4Fuel by where it was made
Your turnFrom Tables with pandas
Which cars burn the most fuel: those made in the USA, in Europe or in Japan? Put the rows in groups by origin, and take the average of litres_100km for each group. Keep it in by_origin.
Hint
First groupby, then the column, then the summary, as with the penguins' mass.
One way to do it
by_origin = cars.groupby("origin")["litres_100km"].mean()
by_origin5Does weight cost fuel?
Your turnFrom Tables with pandas and Plotting to see data
Is a heavier car one that burns more fuel? Draw the plot that answers it, with the weight across and the litres up, and keep what it draws in dots.
Hint
A histogram shows how one value spreads, and a line shows one value over steps. Here there are two values for each car.
One way to do it
dots = plt.scatter(cars["weight_kg"], cars["litres_100km"])6Where was it made?
Your turnFrom Tables with pandas and The scikit-learn pattern
Teach a decision tree to guess where a car was made from its weight and its litres. Then ask it about every car, and keep the answers in guesses. The tree takes one more input here, random_state=0, so it learns the same way on every run.
Hint
fit takes the table and the answers. predict takes the table and gives back one answer for each row.
One way to do it
X = cars[["weight_kg", "litres_100km"]]
model = tree.DecisionTreeClassifier(random_state=0)
model.fit(X, cars["origin"])
guesses = model.predict(X)
guesses[:5]7A second model, and the gaps
On your ownNow try another model, svm.SVC(), on three columns: the weight, the litres and the horsepower. Six cars have no horsepower written down, and SVC stops: Input X contains NaN. Fill the gaps with impute.SimpleImputer(), then fit SVC and keep its answers for every car in svc_guesses.
Hint
The imputer has the same fit as a model, and then transform, or both in one: fit_transform. Then the model's three lines, as in step 6.
One way to do it
X3 = cars[["weight_kg", "litres_100km", "horsepower"]]
filler = impute.SimpleImputer()
X3 = filler.fit_transform(X3)
svc = svm.SVC()
svc.fit(X3, cars["origin"])
svc_guesses = svc.predict(X3)
svc_guesses[:5]Run on Pyodide 314, Python 3.14, pandas 3.0, matplotlib 3.10 and scikit-learn 1.8. Checked on .
