Sklearn Linear Regression


       Sklearn Linear Regression

Linear regression is a fundamental algorithm in machine learning for predicting a quantitative response. Scikit-learn (often called sklearn), a popular Python library for machine learning, provides an easy-to-use implementation of linear regression. Here’s a brief overview of how to use linear regression in sklearn:

  1. Import the Library: First, import the LinearRegression class from sklearn.linear_model.
  2. pythonCopy code
  3. from sklearn.linear_model import LinearRegression
  5. Prepare Your Data: Your data must be split into features (independent variables) and a target (dependent variable). In sklearn, features are typically represented as a 2D array or DataFrame (X), and the target as a 1D array (y).
  6. Create a Linear Regression Model: Instantiate the LinearRegression class.
  7. pythonCopy code
  8. model = LinearRegression()
  10. Fit the Model to Your Data: Use the fit() method to train your model on your data.
  11. pythonCopy code
  12., y)
  14. Making Predictions: After fitting the model, you can use the predict() method to make predictions.
  15. pythonCopy code
  16. y_pred = model.predict(X_new)
  18. Model Evaluation: Evaluate your model’s performance using metrics like mean squared error, R-squared, etc. Sklearn provides various metrics under sklearn. Metrics.
  19. Coefficients and Intercept: You can examine the coefficients and intercept of the trained model, which can provide insights into the relationships between features and the target variable.
  20. pythonCopy code
  21. Coefficients = model. coef_
  22.  Intercept = model. intercept_
  24. Assumptions of Linear Regression: It’s important to remember that linear regression makes certain assumptions like linearity, independence, homoscedasticity, and normal distribution of residuals. Violation of these assumptions can affect the model’s accuracy and interpretation.
  25. Advanced Techniques: For more complex datasets, consider using techniques like polynomial regression, regularization (Ridge, Lasso), or transforming features to meet the assumptions of linear regression better.
  26. Data Splitting: In practice, you should split your data into training and test sets using train_test_split from sklearn.model_selection to evaluate the model’s performance on unseen data.

Here is a simple example code snippet:

The PythonCopy code

imports numpy as np

 from sklearn.linear_model import LinearRegression

 from sklearn.model_selection import train_test_split

 from sklearn.metrics import mean_squared_error

# Sample data

X = np. array([[1, 1], [1, 2], [2, 2], [2, 3]])

y =, np.array([1, 2])) + 3

# Splitting the data

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Creating a linear regression model

model = LinearRegression()

# Training the model, y_train)

# Making predictions

y_pred = model.predict(X_test)

# Evaluating the model

me = mean_squared_error(y_test, y_pred)

print(“Mean Squared Error:”, msg)

In this example, we have a straightforward dataset, and we’re fitting a linear regression model, making predictions, and then evaluating the model using mean squared error. Remember, the real power of linear regression is realized in larger, more complex datasets.

Machine Learning Training Demo Day 1

You can find more information about Machine Learning in this Machine Learning Docs Link



Unogeeks is the No.1 Training Institute for Machine Learning. Anyone Disagree? Please drop in a comment

Please check our Machine Learning Training Details here Machine Learning Training

You can check out our other latest blogs on Machine Learning in this Machine Learning Blogs

💬 Follow & Connect with us:


For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at:

Our Website ➜

Follow us:





Leave a Reply

Your email address will not be published. Required fields are marked *