Mean car prices#
Creating a visualization that displays the mean price of each car manufacturer of the automobiles dataset.
Importing libraries and packages#
1# Warnings
2import warnings
3
4# Mathematical operations and data manipulation
5import pandas as pd
6
7# Visualisation
8from bokeh.plotting import figure, show
9from bokeh.models import LinearColorMapper
10
11# Output
12from bokeh.io import output_notebook
13
14output_notebook()
15warnings.filterwarnings("ignore")
Set paths#
1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"
Loading dataset#
1dataset = pd.read_csv(f"{data_path}/automobiles.csv")
Exploring dataset#
1# Shape of the dataset
2print("Shape of the dataset: ", dataset.shape)
3# Head
4dataset.head()
Shape of the dataset: (201, 14)
make | fuel-type | num-of-doors | body-style | engine-location | length | width | height | num-of-cylinders | horsepower | peak-rpm | city-mpg | highway-mpg | price | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | alfa-romero | gas | two | convertible | front | 168.8 | 64.1 | 48.8 | four | 111 | 5000 | 21 | 27 | 13495 |
1 | alfa-romero | gas | two | convertible | front | 168.8 | 64.1 | 48.8 | four | 111 | 5000 | 21 | 27 | 16500 |
2 | alfa-romero | gas | two | hatchback | front | 171.2 | 65.5 | 52.4 | six | 154 | 5000 | 19 | 26 | 16500 |
3 | audi | gas | four | sedan | front | 176.6 | 66.2 | 54.3 | four | 102 | 5500 | 24 | 30 | 13950 |
4 | audi | gas | four | sedan | front | 176.6 | 66.4 | 54.3 | five | 115 | 5500 | 18 | 22 | 17450 |
Preprocessing#
1# Adding a new column with the indices called index
2dataset["index"] = dataset.index
Visualisation#
1# plotting a point for each car with its price
2plot = figure(
3 title="Car prices", x_axis_label="Car Index", y_axis_label="Price"
4)
5plot.scatter(dataset["index"], dataset["price"])
6
7show(plot)
1# Grouping the dataset by the make column and aggregating it by the mean
2grouped_average = dataset.groupby(["make"], as_index=False).mean()
3grouped_average
make | length | width | height | city-mpg | highway-mpg | price | index | |
---|---|---|---|---|---|---|---|---|
0 | alfa-romero | 169.600000 | 64.566667 | 50.000000 | 20.333333 | 26.666667 | 15498.333333 | 1.0 |
1 | audi | 184.766667 | 68.850000 | 54.833333 | 19.333333 | 24.500000 | 17859.166667 | 5.5 |
2 | bmw | 184.500000 | 66.475000 | 54.825000 | 19.375000 | 25.375000 | 26118.750000 | 12.5 |
3 | chevrolet | 151.933333 | 62.500000 | 52.400000 | 41.000000 | 46.333333 | 6007.000000 | 18.0 |
4 | dodge | 160.988889 | 64.166667 | 51.644444 | 28.000000 | 34.111111 | 7875.444444 | 24.0 |
5 | honda | 160.769231 | 64.384615 | 53.238462 | 30.384615 | 35.461538 | 8184.692308 | 35.0 |
6 | isuzu | 171.650000 | 63.500000 | 52.450000 | 24.000000 | 29.000000 | 8916.500000 | 42.5 |
7 | jaguar | 196.966667 | 69.933333 | 51.133333 | 14.333333 | 18.333333 | 34600.000000 | 45.0 |
8 | mazda | 170.805882 | 65.588235 | 53.358824 | 25.705882 | 31.941176 | 10652.882353 | 55.0 |
9 | mercedes-benz | 195.262500 | 71.062500 | 55.725000 | 18.500000 | 21.000000 | 33647.000000 | 67.5 |
10 | mercury | 178.400000 | 68.000000 | 54.800000 | 19.000000 | 24.000000 | 16503.000000 | 72.0 |
11 | mitsubishi | 168.030769 | 65.253846 | 50.692308 | 24.923077 | 31.153846 | 9239.769231 | 79.0 |
12 | nissan | 170.988889 | 65.088889 | 53.633333 | 27.000000 | 32.944444 | 10415.666667 | 94.5 |
13 | peugot | 191.136364 | 68.390909 | 57.181818 | 22.454545 | 26.636364 | 15489.090909 | 109.0 |
14 | plymouth | 164.900000 | 64.271429 | 51.971429 | 28.142857 | 34.142857 | 7963.428571 | 118.0 |
15 | porsche | 168.900000 | 65.825000 | 51.250000 | 17.500000 | 25.500000 | 31400.500000 | 123.5 |
16 | renault | 179.150000 | 66.550000 | 52.850000 | 23.000000 | 31.000000 | 9595.000000 | 126.5 |
17 | saab | 186.600000 | 66.500000 | 56.100000 | 20.333333 | 27.333333 | 15223.333333 | 130.5 |
18 | subaru | 168.858333 | 64.950000 | 53.750000 | 26.333333 | 30.750000 | 8541.250000 | 139.5 |
19 | toyota | 171.934375 | 65.090625 | 53.721875 | 27.500000 | 32.906250 | 9885.812500 | 161.5 |
20 | volkswagen | 172.533333 | 65.616667 | 55.183333 | 28.583333 | 34.916667 | 10077.500000 | 183.5 |
21 | volvo | 188.800000 | 67.963636 | 56.236364 | 21.181818 | 25.818182 | 18063.181818 | 195.0 |
1# Plotting the manufacturers and their mean car prices
2grouped_plot = figure(
3 title="Car Manufacturer Mean Prices",
4 x_axis_label="Car Manufacturer",
5 y_axis_label="Mean Price",
6 x_range=grouped_average["make"],
7)
8grouped_plot.scatter(grouped_average["make"], grouped_average["price"])
9
10show(grouped_plot)
1# Assigning the x label orientation the value of vertical
2grouped_plot.xaxis.major_label_orientation = "vertical"
3
4show(grouped_plot)
1# Adding color based on the mean price to our elements
2
3color_mapper = LinearColorMapper(
4 palette="Magma256",
5 low=min(grouped_average["price"]),
6 high=max(grouped_average["price"]),
7)
8
9grouped_colored_plot = figure(
10 title="Car Manufacturer Mean Prices",
11 x_axis_label="Car Manufacturer",
12 y_axis_label="Mean Price",
13 x_range=grouped_average["make"],
14)
15grouped_colored_plot.scatter(
16 grouped_average["make"],
17 grouped_average["price"],
18 color={"field": "y", "transform": color_mapper},
19 size=15,
20)
21
22grouped_colored_plot.xaxis.major_label_orientation = "vertical"
23
24show(grouped_colored_plot)