How to Implement a Simple Linear Regression Model in Python

How to Implement a Simple Linear Regression Model in Python

How to Implement a Simple Linear Regression Model in Python

Linear regression is the simplest machine learning algorithm used to establish a linear relationship between independent variables and dependent variables. In this post, we’ll discuss how to implement a simple linear regression model using the scikit-learn library in Python.

What is Linear Regression?

Linear regression is a statistical method used to model the linear relationship between independent variables (features) and a dependent variable (target). Mathematically, the linear regression model can be expressed as:


from sklearn.datasets import load_boston import pandas as pd boston = load_boston() data = pd.DataFrame(boston.data, columns=boston.feature_names) data['PRICE'] = boston.target X = data.drop('PRICE', axis=1) y = data['PRICE']

Next, we split the dataset into training and test sets. Typically, we use 80% of the dataset for training and 20% for testing.

from sklearn.model_selection import train_test_split

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

Then, we can use the LinearRegression class to build a linear regression model.

from sklearn.linear_model import LinearRegression 

lr = LinearRegression() 
lr.fit(X_train, y_train)

After model training, we can use the test data to evaluate the model’s performance.

from sklearn.metrics import mean_squared_error

y_pred = lr.predict(X_test)

mse = mean_squared_error(y_test, y_pred)

print(f'Mean Squared Error: {mse}')

Finally, we can use the trained model to make predictions on new data.

new_data = pd.DataFrame({ 
'CRIM': [0.03], 
'ZN': [0.0], 
'INDUS': [2.18], 
'CHAS': [0.0], 
'NOX': [0.458], 
'RM': [6.579], 
'AGE': [45.8], 
'DIS': [6.998], 
'RAD': [3.0], 
'TAX': [222.0], 
'PTRATIO': [18.7], 
'B': [394.63], 
'LSTAT': [2.94] 
}) 

prediction= lr.predict(new_data) 
print(f'Predicted Price: {prediction[0]}') 

The above code demonstrates how to implement a simple linear regression model using the scikit-learn library in Python. By preparing a dataset, training a model, evaluating performance, and predicting new data, we can quickly build a linear regression model and make predictions.

Conclusion

Linear regression is a simple yet effective machine learning algorithm that achieves good results in many practical situations. By using the scikit-learn library in Python, we can easily implement a linear regression model and make predictions on data.

Leave a Reply

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