Skip to content

Linear regression

In this tutorial, we will show you how to use linear regression. We will explore how to perform Ordinary Least Squares (OLS) and Ridge regression using crandas. We will use a dummy weather dataset to demonstrate the process. This involves modelling the relationship between temperature and other weather attributes such as humidity and wind speed. Let's dive into the steps needed to create, train, and score a multiple linear regression model using crandas.

# import the necessary modules
import crandas as cd
from crandas.crlearn.linear_model import LinearRegression, Ridge
from crandas.crlearn.model_selection import train_test_split
from crandas.crlearn.metrics import score_r2

# read the data from a csv file
tab = cd.read_csv(
    "data/linreg_data/dummy_weather_data.csv", auto_bounds=True
)

Next we define the predictor variables (X) and the target variable (y). The data is then split into training and test sets using a 70-30 split.

# define the predictor variables
X = tab[
    [
        "Apparent Temperature (C)",
        "Humidity",
        "Wind Speed (km/h)",
        "Wind Bearing (degrees)",
        "Visibility (km)",
        "Cloud Cover",
        "Pressure (millibars)",
    ]
]

# define the target variable
y = tab[["Temperature (C)"]]

# split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

A simple ordinary least squares (OLS) linear regression model is trained on the data in the next code block. The coefficients (beta) of the model are displayed, as are the first five predictions and the model's \(R^2\) score.

# create a linear regression model (OLS)
ols_model = LinearRegression()
ols_model = ols_model.fit(X_train, y_train)
beta_ols = ols_model.open()["beta_"]
print(f"OLS Beta:\n{beta_ols}")
y_test_pred_ols = ols_model.predict(X_test)
print(f"First five OLS predictions:\n{y_test_pred_ols.open().head()}")
ols_score = score_r2(y_test, y_test_pred_ols).open()
print(f"OLS R2 Score: {ols_score}")
OLS Beta:
[55.30970764160156, -0.7270193099975586, -5.299004554748535, -0.679509162902832, -0.09122562408447266, 0.2653636932373047, 0.0, 0.0]
First five OLS predictions:
   Temperature (C)
0        19.129745
1        19.006504
2        20.832978
3        19.129745
4        19.006504
OLS R2 Score: -0.11085891723632812

If we want to do Ridge regression instead, we can look at the following code blocks. In the first block, a ridge regression model with a Cholesky solver is trained. In the second block, a ridge regression model with a Cholesky solver and a regularization parameter alpha of 0.5 is trained. For each model, the coefficients, first five predictions, and \(R^2\) score are displayed.

# create a Ridge regression model with 'cholesky' solver
ridge_model = Ridge(solver='cholesky')
ridge_model = ridge_model.fit(X_train, y_train)
beta_ridge = ridge_model.open()["beta_"]
print(f"Ridge Beta:\n{beta_ridge}")
y_test_pred_ridge = ridge_model.predict(X_test)
print(f"First five Ridge predictions:\n{y_test_pred_ridge.open().head()}")
ridge_score = score_r2(y_test, y_test_pred_ridge).open()
print(f"Ridge R2 Score: {ridge_score}")
Ridge Beta:
[-0.019850730895996094, -0.5241470336914062, -0.4974966049194336, -1.0132207870483398, -0.048277854919433594, 0.997406005859375, 0.0, 0.03871440887451172]
First five Ridge predictions:
   Temperature (C)
0        19.045611
1        19.046930
2        20.769311
3        19.045611
4        19.046930
Ridge R2 Score: -0.13656330108642578
# create a Ridge regression model with 'cholesky' solver and alpha=0.5
ridge_alpha_model = Ridge(solver="cholesky", alpha=0.5)
ridge_alpha_model = ridge_alpha_model.fit(X_train, y_train)
beta_ridge_alpha = ridge_alpha_model.open()["beta_"]
print(f"Ridge (alpha=0.5) Beta:\n{beta_ridge_alpha}")
y_test_pred_ridge_alpha = ridge_alpha_model.predict(X_test)
print(
    f"First five Ridge (alpha=0.5) predictions:\n{y_test_pred_ridge_alpha.open().head()}"
)
ridge_alpha_score = score_r2(y_test, y_test_pred_ridge_alpha).open()
print(f"Ridge (alpha=0.5) R2 Score: {ridge_alpha_score}")
Ridge (alpha=0.5) Beta:
[-0.029523849487304688, -0.5316267013549805, -0.6231155395507812, -1.047307014465332, -0.050273895263671875, 1.0474824905395508, 0.0, 0.03926277160644531]
First five Ridge (alpha=0.5) predictions:
   Temperature (C)
0        19.022510
1        19.051657
2        20.811252
3        19.022510
4        19.051657
Ridge (alpha=0.5) R2 Score: -0.13579845428466797