본문 바로가기
Programming/Python

Linear Regression with Scikit-Learn or Sklearn

by sayshare 2023. 7. 24.
반응형

What is Scikit-Learn?

Scikit-Learn is an open-source machine-learning library for Python that builds upon NumPy , SciPy, and Matplotlib . With scikit-learn , we are able to easily implement machine learning models for regression, clustering, classification, etc. The purpose of this library is to minimize the complexity of mathematical calculation of those machine learning algorithms and instead focus on features.

 

In this article, we are going to implement Linear Regression with the help of Scikit-learn. Linear Regression is an algorithm used for predictive data analysis. The purpose of the Linear Regression equation is to find the straight line that best fits a set of scattered observable data points. The length from each data point to the point on the straight line is called residual or error.

Implementation

import pandas as pd
  import matplotlib.pyplot as plt
  from sklearn.linear_model import LinearRegression

data = pd.read_csv( 'score.csv' )
hours = data[ 'hours' ].values
scores = data[ 'score' ].values

hours = hours.reshape(len(hours), 1 )
scores = scores.reshape(len(scores), 1 )

model = LinearRegression(fit_intercept=True)
model.fit(hours, scores)
plt.scatter(hours, scores, s= 30 )
plt.plot(hours, model.predict(hours), color= 'b' , linewidth= 3 )
plt. xticks(())
plt.yticks(())
plt.show()
  • matplotlib (for data visualization)
  • pandas (data analysis) 

Result:

 

반응형

'Programming > Python' 카테고리의 다른 글

Linear Regression with Python  (0) 2023.07.23