ai newsDi., 1. Dez.

# Linear Regression – A Beginner's Guide with Python **Category:** Machine Learning | Data Science

Linear Regression – A Beginner's Guide with Python Category: Machine Learning | Data Science 1. Introduction In Data Science, predicting future outcomes using existing data is an important task. Businesses may want to estimate sales, banks may need to understand financial trends, and educators may

dev.toMontag 19:20 · 30.11.3 Min. Lesezeit
Bild: dev.to

Category: Machine Learning | Data Science

In Data Science, predicting future outcomes using existing data is an important task. Businesses may want to estimate sales, banks may need to understand financial trends, and educators may want to study student performance.

Machine Learning provides techniques that help us make these predictions.

One of the simplest and most widely used techniques is Linear Regression. It is a useful starting point for understanding machine learning because its predictions and relationships are relatively easy to interpret.

Linear Regression is a supervised machine learning algorithm used to predict a continuous numerical value.

It identifies the relationship between one or more input variables and an output variable.

A model can study the relationship between the number of hours a student studies and the marks they receive. It can then estimate the marks of another student based on their study hours.

Linear Regression finds a line that best represents the relationship between the data points.

It compares actual values with predicted values and tries to minimise the differences, commonly using the Least Squares Method.

Example: As study hours increase, student marks may also increase. Linear Regression learns this general trend.

Uses one independent variable to predict an output.

Example: Predicting student marks using study hours.

Uses two or more independent variables to predict an output.

Example: Predicting house prices using area, age and number of rooms.

We can implement Simple Linear Regression in Python using the Scikit-learn library.

In this example, we will predict student marks based on study hours.

pip install numpy scikit-learn matplotlib Enter fullscreen mode Exit fullscreen mode Step 2: Python Program import numpy as np import matplotlib. pyplot as plt from sklearn. linear_model import LinearRegression # Input: hours studied X = np. array ( [ 1, 2, 3, 4, 5, 6 ] ). reshape ( - 1, 1 ) # Output: marks obtained y = np. array ( [ 35, 40, 50, 55, 65, 70 ] ) # Create and train the model model = LinearRegression ( ) model. fit ( X, y ) # Predict marks for 7 study hours prediction = model. predict ( [ [ 7 ] ] ) print ( "Predicted marks:", round ( prediction [ 0 ], 2 ) ) # Draw the data and regression line plt. scatter ( X, y, color = "blue", label = "Actual marks" ) plt. plot ( X, model. predict ( X ), color = "red", label = "Regression line" ) plt. xlabel ( "Study Hours" ) plt. ylabel ( "Marks" ) plt. title ( "Linear Regression: Study Hours vs Marks" ) plt. legend ( ) plt. show ( ) Enter fullscreen mode Exit fullscreen mode Step 3: Sample Output Predicted marks: 77.0 Enter fullscreen mode Exit fullscreen mode Output Explanation:

The model predicts that a student who studies for 7 hours may score approximately 77 marks based on the sample dataset.

Linear Regression is used in many fields:

Linear Regression is an important starting point for understanding Machine Learning.

Although it is a basic algorithm, it is useful in many industries for prediction and data analysis.

Learning Linear Regression helps students understand how a machine learning model identifies patterns in data and uses them to estimate numerical outcomes.

Quelle: dev.to (dev.to). Original öffnen