my_project.visualizations

  1import numpy as np
  2import plotly
  3import pandas as pd
  4import plotly.express as px
  5import seaborn as sns
  6import matplotlib.pyplot as plt
  7
  8def car_model_count(df: pd.DataFrame) -> plotly.graph_objects.Figure:
  9    """
 10    Makes a plot showing how many cars each car model has.
 11        
 12    Parameters
 13    ----------
 14    df : pd.DataFrame
 15        Dataframe with the data
 16        
 17    Returns
 18    -------
 19    fig : plotly.graph_objects.Figure
 20        Bar plot showing the car amounts per car model
 21    """
 22    
 23    car_counts = df["Make"].value_counts().reset_index()
 24    car_counts.columns = ["Make", "Count"]
 25
 26    fig = px.bar(
 27        car_counts,
 28        x="Make",
 29        y="Count",
 30        title="Automobile Dataset: Car Model Count",
 31        labels={
 32            "Make": "Car Model",
 33            "Count": "Count"
 34        }
 35    )
 36
 37    fig.update_layout(
 38        xaxis_tickangle=-90
 39    )
 40
 41    return fig
 42
 43
 44def correlation_plot(df: pd.DataFrame) -> plotly.graph_objects.Figure:
 45    """
 46    Creates a plot heatmap showing the correlation of features.
 47        
 48    Parameters
 49    ----------
 50    df : pd.DataFrame
 51        Dataframe with the data
 52        
 53    Returns
 54    -------
 55    fig : plotly.graph_objects.Figure
 56        Plot showing a correlation heatmap
 57    """
 58    
 59    corr = df.select_dtypes(include=np.number).corr()
 60
 61    fig = px.imshow(
 62        corr,
 63        text_auto=".2f",
 64        color_continuous_scale="RdBu_r",
 65        title="Correlation Heatmap"
 66    )
 67
 68    return fig
 69
 70
 71def scatterplots(df: pd.DataFrame) -> plotly.graph_objects.Figure:
 72    """
 73    Creates a scatterplot showing selling price and Mileage against each other.
 74        
 75    Parameters
 76    ----------
 77    df : pd.DataFrame
 78        Dataframe with the data
 79        
 80    Returns
 81    -------
 82    fig : plotly.graph_objects.Figure
 83        Scatterplot between selling price and Mileage
 84    """
 85    
 86    fig = px.scatter(
 87        df,
 88        x="Selling_Price",
 89        y="Mileage",
 90        color="Make",
 91        title="Selling Price vs Mileage",
 92        labels={
 93            "Selling_Price": "Selling Price",
 94            "Mileage": "Mileage",
 95            "Make": "Make"
 96        },
 97        hover_data=df.columns
 98    )
 99
100    return fig
101
102
103def selling_vs_car(df: pd.DataFrame) -> plotly.graph_objects.Figure:
104    """
105    Creates a boxplot of the car model and the selling price.
106        
107    Parameters
108    ----------
109    df : pd.DataFrame
110        Dataframe with the data
111        
112    Returns
113    -------
114    fig : plotly.graph_objects.Figure
115        Boxplot of the car model on selling price
116    """
117    
118    fig = px.box(
119        df,
120        x="Make",
121        y="Selling_Price",
122        title="Selling Price vs Car",
123        labels={
124            "Make": "Car Model",
125            "Selling_Price": "Selling Price"
126        }
127    )
128
129    fig.update_layout(
130        xaxis_tickangle=-90
131    )
132
133    return fig
134
135
136def pairplots(df: pd.DataFrame) -> plotly.graph_objects.Figure:
137    """
138    Creates all pairplots of the features.
139        
140    Parameters
141    ----------
142    df : pd.DataFrame
143        Dataframe with the data
144        
145    Returns
146    -------
147    fig : plotly.graph_objects.Figure
148        Pairplots of the features
149    """
150    
151    numeric_columns = df.select_dtypes(include=np.number).columns
152
153    fig = px.scatter_matrix(
154        df,
155        dimensions=numeric_columns,
156        title="Automobile Dataset: Pairplot"
157    )
158
159    return fig
160
161
162def actual_vs_predicted(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
163    """
164    Plot actual prices against predicted prices.
165        
166    Parameters
167    ----------
168    targets : np.ndarray
169        True targets from the dataset
170    predictions : np.ndarray
171        Predicted targets on the dataset
172        
173    Returns
174    -------
175    fig : plotly.graph_objects.Figure
176        Plot showing the regression and the scattered data
177    """
178
179    fig, ax = plt.subplots(figsize=(6, 6))
180
181    ax.scatter(
182        targets,
183        predictions,
184        alpha=0.6
185    )
186
187    min_value = min(
188        targets.min(),
189        predictions.min()
190    )
191
192    max_value = max(
193        targets.max(),
194        predictions.max()
195    )
196
197    ax.plot(
198        [min_value, max_value],
199        [min_value, max_value],
200        linestyle="--"
201    )
202
203    ax.set_xlabel("Actual Price")
204    ax.set_ylabel("Predicted Price")
205    ax.set_title("Actual vs Predicted")
206
207    return fig
208
209
210def residual_distribution(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
211    """
212    Plot the distribution of prediction errors.
213        
214    Parameters
215    ----------
216    targets : np.ndarray
217        True targets from the dataset
218    predictions : np.ndarray
219        Predicted targets on the dataset
220        
221    Returns
222    -------
223    fig : plotly.graph_objects.Figure
224        Plot showing the prediction errors
225    """
226
227    residuals = predictions - targets
228
229    fig, ax = plt.subplots(figsize=(6, 4))
230
231    sns.histplot(
232        residuals,
233        kde=True,
234        ax=ax
235    )
236
237    ax.set_xlabel("Prediction Error")
238    ax.set_ylabel("Frequency")
239    ax.set_title("Residual Distribution")
240
241    return fig
242
243
244def residuals_vs_predictions(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
245    """
246    Plot residuals against predicted prices.
247        
248    Parameters
249    ----------
250    targets : np.ndarray
251        True targets from the dataset
252    predictions : np.ndarray
253        Predicted targets on the dataset
254        
255    Returns
256    -------
257    fig : plotly.graph_objects.Figure
258        Plot showing the residuals and the scattered data
259    """
260
261    residuals = predictions - targets
262
263    fig, ax = plt.subplots(figsize=(6, 4))
264
265    ax.scatter(
266        predictions,
267        residuals,
268        alpha=0.6
269    )
270
271    ax.axhline(
272        y=0,
273        linestyle="--"
274    )
275
276    ax.set_xlabel("Predicted Price")
277    ax.set_ylabel("Residual")
278    ax.set_title("Residuals vs Predictions")
279
280    return fig
def car_model_count(df: pandas.core.frame.DataFrame) -> plotly.graph_objs._figure.Figure:
 9def car_model_count(df: pd.DataFrame) -> plotly.graph_objects.Figure:
10    """
11    Makes a plot showing how many cars each car model has.
12        
13    Parameters
14    ----------
15    df : pd.DataFrame
16        Dataframe with the data
17        
18    Returns
19    -------
20    fig : plotly.graph_objects.Figure
21        Bar plot showing the car amounts per car model
22    """
23    
24    car_counts = df["Make"].value_counts().reset_index()
25    car_counts.columns = ["Make", "Count"]
26
27    fig = px.bar(
28        car_counts,
29        x="Make",
30        y="Count",
31        title="Automobile Dataset: Car Model Count",
32        labels={
33            "Make": "Car Model",
34            "Count": "Count"
35        }
36    )
37
38    fig.update_layout(
39        xaxis_tickangle=-90
40    )
41
42    return fig

Makes a plot showing how many cars each car model has.

Parameters
  • df (pd.DataFrame): Dataframe with the data
Returns
  • fig (plotly.graph_objects.Figure): Bar plot showing the car amounts per car model
def correlation_plot(df: pandas.core.frame.DataFrame) -> plotly.graph_objs._figure.Figure:
45def correlation_plot(df: pd.DataFrame) -> plotly.graph_objects.Figure:
46    """
47    Creates a plot heatmap showing the correlation of features.
48        
49    Parameters
50    ----------
51    df : pd.DataFrame
52        Dataframe with the data
53        
54    Returns
55    -------
56    fig : plotly.graph_objects.Figure
57        Plot showing a correlation heatmap
58    """
59    
60    corr = df.select_dtypes(include=np.number).corr()
61
62    fig = px.imshow(
63        corr,
64        text_auto=".2f",
65        color_continuous_scale="RdBu_r",
66        title="Correlation Heatmap"
67    )
68
69    return fig

Creates a plot heatmap showing the correlation of features.

Parameters
  • df (pd.DataFrame): Dataframe with the data
Returns
  • fig (plotly.graph_objects.Figure): Plot showing a correlation heatmap
def scatterplots(df: pandas.core.frame.DataFrame) -> plotly.graph_objs._figure.Figure:
 72def scatterplots(df: pd.DataFrame) -> plotly.graph_objects.Figure:
 73    """
 74    Creates a scatterplot showing selling price and Mileage against each other.
 75        
 76    Parameters
 77    ----------
 78    df : pd.DataFrame
 79        Dataframe with the data
 80        
 81    Returns
 82    -------
 83    fig : plotly.graph_objects.Figure
 84        Scatterplot between selling price and Mileage
 85    """
 86    
 87    fig = px.scatter(
 88        df,
 89        x="Selling_Price",
 90        y="Mileage",
 91        color="Make",
 92        title="Selling Price vs Mileage",
 93        labels={
 94            "Selling_Price": "Selling Price",
 95            "Mileage": "Mileage",
 96            "Make": "Make"
 97        },
 98        hover_data=df.columns
 99    )
100
101    return fig

Creates a scatterplot showing selling price and Mileage against each other.

Parameters
  • df (pd.DataFrame): Dataframe with the data
Returns
  • fig (plotly.graph_objects.Figure): Scatterplot between selling price and Mileage
def selling_vs_car(df: pandas.core.frame.DataFrame) -> plotly.graph_objs._figure.Figure:
104def selling_vs_car(df: pd.DataFrame) -> plotly.graph_objects.Figure:
105    """
106    Creates a boxplot of the car model and the selling price.
107        
108    Parameters
109    ----------
110    df : pd.DataFrame
111        Dataframe with the data
112        
113    Returns
114    -------
115    fig : plotly.graph_objects.Figure
116        Boxplot of the car model on selling price
117    """
118    
119    fig = px.box(
120        df,
121        x="Make",
122        y="Selling_Price",
123        title="Selling Price vs Car",
124        labels={
125            "Make": "Car Model",
126            "Selling_Price": "Selling Price"
127        }
128    )
129
130    fig.update_layout(
131        xaxis_tickangle=-90
132    )
133
134    return fig

Creates a boxplot of the car model and the selling price.

Parameters
  • df (pd.DataFrame): Dataframe with the data
Returns
  • fig (plotly.graph_objects.Figure): Boxplot of the car model on selling price
def pairplots(df: pandas.core.frame.DataFrame) -> plotly.graph_objs._figure.Figure:
137def pairplots(df: pd.DataFrame) -> plotly.graph_objects.Figure:
138    """
139    Creates all pairplots of the features.
140        
141    Parameters
142    ----------
143    df : pd.DataFrame
144        Dataframe with the data
145        
146    Returns
147    -------
148    fig : plotly.graph_objects.Figure
149        Pairplots of the features
150    """
151    
152    numeric_columns = df.select_dtypes(include=np.number).columns
153
154    fig = px.scatter_matrix(
155        df,
156        dimensions=numeric_columns,
157        title="Automobile Dataset: Pairplot"
158    )
159
160    return fig

Creates all pairplots of the features.

Parameters
  • df (pd.DataFrame): Dataframe with the data
Returns
  • fig (plotly.graph_objects.Figure): Pairplots of the features
def actual_vs_predicted( targets: numpy.ndarray, predictions: numpy.ndarray) -> plotly.graph_objs._figure.Figure:
163def actual_vs_predicted(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
164    """
165    Plot actual prices against predicted prices.
166        
167    Parameters
168    ----------
169    targets : np.ndarray
170        True targets from the dataset
171    predictions : np.ndarray
172        Predicted targets on the dataset
173        
174    Returns
175    -------
176    fig : plotly.graph_objects.Figure
177        Plot showing the regression and the scattered data
178    """
179
180    fig, ax = plt.subplots(figsize=(6, 6))
181
182    ax.scatter(
183        targets,
184        predictions,
185        alpha=0.6
186    )
187
188    min_value = min(
189        targets.min(),
190        predictions.min()
191    )
192
193    max_value = max(
194        targets.max(),
195        predictions.max()
196    )
197
198    ax.plot(
199        [min_value, max_value],
200        [min_value, max_value],
201        linestyle="--"
202    )
203
204    ax.set_xlabel("Actual Price")
205    ax.set_ylabel("Predicted Price")
206    ax.set_title("Actual vs Predicted")
207
208    return fig

Plot actual prices against predicted prices.

Parameters
  • targets (np.ndarray): True targets from the dataset
  • predictions (np.ndarray): Predicted targets on the dataset
Returns
  • fig (plotly.graph_objects.Figure): Plot showing the regression and the scattered data
def residual_distribution( targets: numpy.ndarray, predictions: numpy.ndarray) -> plotly.graph_objs._figure.Figure:
211def residual_distribution(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
212    """
213    Plot the distribution of prediction errors.
214        
215    Parameters
216    ----------
217    targets : np.ndarray
218        True targets from the dataset
219    predictions : np.ndarray
220        Predicted targets on the dataset
221        
222    Returns
223    -------
224    fig : plotly.graph_objects.Figure
225        Plot showing the prediction errors
226    """
227
228    residuals = predictions - targets
229
230    fig, ax = plt.subplots(figsize=(6, 4))
231
232    sns.histplot(
233        residuals,
234        kde=True,
235        ax=ax
236    )
237
238    ax.set_xlabel("Prediction Error")
239    ax.set_ylabel("Frequency")
240    ax.set_title("Residual Distribution")
241
242    return fig

Plot the distribution of prediction errors.

Parameters
  • targets (np.ndarray): True targets from the dataset
  • predictions (np.ndarray): Predicted targets on the dataset
Returns
  • fig (plotly.graph_objects.Figure): Plot showing the prediction errors
def residuals_vs_predictions( targets: numpy.ndarray, predictions: numpy.ndarray) -> plotly.graph_objs._figure.Figure:
245def residuals_vs_predictions(targets: np.ndarray, predictions: np.ndarray) -> plotly.graph_objects.Figure:
246    """
247    Plot residuals against predicted prices.
248        
249    Parameters
250    ----------
251    targets : np.ndarray
252        True targets from the dataset
253    predictions : np.ndarray
254        Predicted targets on the dataset
255        
256    Returns
257    -------
258    fig : plotly.graph_objects.Figure
259        Plot showing the residuals and the scattered data
260    """
261
262    residuals = predictions - targets
263
264    fig, ax = plt.subplots(figsize=(6, 4))
265
266    ax.scatter(
267        predictions,
268        residuals,
269        alpha=0.6
270    )
271
272    ax.axhline(
273        y=0,
274        linestyle="--"
275    )
276
277    ax.set_xlabel("Predicted Price")
278    ax.set_ylabel("Residual")
279    ax.set_title("Residuals vs Predictions")
280
281    return fig

Plot residuals against predicted prices.

Parameters
  • targets (np.ndarray): True targets from the dataset
  • predictions (np.ndarray): Predicted targets on the dataset
Returns
  • fig (plotly.graph_objects.Figure): Plot showing the residuals and the scattered data