Linear Regression

Set Up

Before Starting make sure to have Observable-Jupyter and any other needed libraries installed in your local environment.

[1]:
from observable_jupyter import embed
from sklearn.datasets import load_wine
import pandas as pd
import json

Load and Format Data

[2]:
wine = load_wine()
wine_df = pd.DataFrame(data=wine.data, columns=wine.feature_names)
wine_df.head()
[2]:
alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue od280/od315_of_diluted_wines proline
0 14.23 1.71 2.43 15.6 127.0 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065.0
1 13.20 1.78 2.14 11.2 100.0 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050.0
2 13.16 2.36 2.67 18.6 101.0 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185.0
3 14.37 1.95 2.50 16.8 113.0 3.85 3.49 0.24 2.18 7.80 0.86 3.45 1480.0
4 13.24 2.59 2.87 21.0 118.0 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735.0

The following block of code structures the data into a format accepted by Observable.

[3]:
result = wine_df.to_json(orient="records")
parsed = json.loads(result)
data = json.dumps(parsed, indent=4)
Formated_Data = json.loads(data)

Embed your data into the visualization

The Linear Regression visualization consists of one cell:

  1. Linear_Regression_Chart : Depicts the graph and the associated data

To make your visualization work you will need to access the input variables.

  1. csv_data : set csv_data equal to your structured data.

  2. x_variable : set to the name of column you want as the x Axis.

  3. y_variable : set to the name of column you want as the Y Axis.

  4. groupings : set to a column that acts as an identifyer for various data points

[4]:
embed(
    '@rstorni/plot-regression-demo',
    cells=['Linear_Regression_Chart'],
    inputs = {
        "csv_data" : Formated_Data,
        "x_variable" : "flavanoids",
        "y_variable" : "color_intensity",
        'groupings' : 'hue'
     }
)

Images/Thumbnails/Linear_Regression.png