In this article, I will explain an Example of Linear Regression in Python.
To begin with, let us first understand what is meant by regression? Basically, regression is a technique that determines the relationship between two variables. One of the variables is known as dependent variable. Whereas, the other one is known as independent variable. Furthermore, for arbitrary value of the independent variable, the value of independent variable may increase or decrease. when we plot the values of two variables, it takes the form of a straight line. We call this type of regression a Linear Regression.
The following list provides some examples of the Linear Regression.
- As the age of a person increases, his/her height also increases.
- When area of houses increase, their prices also increase.
- With the increase in number of Covid-19 cases, the associated deaths also increase.
Demonstrating an Example of Linear Regression in Python
The following code example demonstrates how to perform linear regression in python.
Basically, Linear Regression is a statistical method that is used to establish a relationship between a dependent variable and one or more independent variables. In Python, the
scikit-learn library provides an implementation of linear regression.
In order to use linear regression in Python, you will need to follow these steps.
- At first, import the
LinearRegressionclass from the
- After that, create an instance of the
- Then, fit the model to your training data by calling the
fitmethod and passing it the independent and dependent variables.
- Finally, use the
predictmethod to make predictions on new data.
from sklearn.linear_model import LinearRegression # Step 1: Load the data X = [, , , ] y = [2, 4, 6, 8] # Step 2: Create the model reg = LinearRegression() # Step 3: Fit the model to the data reg.fit(X, y) # Step 4: Make predictions print(reg.predict([])) # Output: [10.]
- Dot Net Framework
- Power Bi
- Scratch 3.0