Skip to main content

Linear Regression with Scikit-learn

1 min

Linear regression is a type of supervised machine learning algorithm used to model the relationship between a dependent variable and one or more independent variables. It is a versatile algorithm that is commonly used for predictive modeling, trend analysis, forecasting, and data analysis.

Below is a simple example to demonstrate the basic process of running a linear regression using Python with the NumPy and scikit-learn packages.

In practice, you will need to perform additional data cleaning, preprocessing, and feature engineering steps to prepare your data for modeling.

Import the necessary libraries. #

import numpy as np
from sklearn.linear_model import LinearRegression

Define your data. #

# input features
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 

# target variable
y = np.array([4, 5, 6])

Create a linear regression model. #

model = LinearRegression()

Fit the model to the data. #

model.fit(X,y)

Predict using the model. #

X_test = np.array([[10, 11, 12]])
y_pred = model.predict(X_test)
print(y_pred)