In this tutorial, you will learn
1. How to import the Scikit-Learn libraries?
2. How to import the dataset from Scikit-Learn?
3. How to explore dataset?
4. How to split the data using Scikit-Learn train_test_split?
5. How to implement a Linear Regression Model in Scikit-Learn?
6. How to predict the output using a trained Linear Regression Model?
7. How to calculate Mean Squared Error (MSE)?
Linear Regression
In linear regression, we try to build a relationship between the training dataset (X) and the output variable (y). We predict the output variable (y) based on the relationship we have implemented.
1. Import the Libraries
from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.datasets import load_iris from sklearn.metrics import mean_squared_error import numpy as np
2. Import the Dataset
data = load_iris() X=data['data'] y=data['target']
3. Explore the Dataset
X[:10]

print(y) print(y.shape)

4. Splitting the Dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) X_train.size

5. Model Implementation and Fitting
linearregression=LinearRegression() linearregression.fit(X_train,y_train)

6. Model Prediction
predictions=linearregression.predict(X_test) predictions

7. Calculate Mean Squared Error
mean_squared_error(predictions, y_test)

Summary
1. datasets : To import the Scikit-Learn datasets.
2. shape : To get the size of the dataset.
3. train_test_split : To split the data using Scikit-Learn.
4. LinearRegression( ) : To implement a Linear Regression Model in Scikit-Learn.
5. predict( ) : To predict the output using a trained Linear Regression Model.
6. mean_squared_error( ) : To calculate Mean Squared Error (MSE).
Related Tutorials
- How to implement a Support Vector Machine(SVM) Regressor Model in Scikit-Learn?
- How to implement a K-Nearest Neighbors (KNN) Regressor Model in Scikit-Learn?
- How to implement a Decision Trees Regressor Model in Scikit-Learn?
- How to implement a Random Forest Regressor Model in Scikit-Learn?
- How to implement a Multi-Layer Perceptron (MLP) Regressor Model in Spark?