Professional Documents
Culture Documents
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
#evaluating model
from sklearn.metrics import r2_score
r2_score=r2_score(y_test,y_pred)
print('R2 score:',r2_score)
EXPLANATION:
This code is a simple example of machine learning linear regression using python and
matplotlib. The necessary packages such as numpy and matplotlib are imported. Then the
necessary variables, x and y are defined. The graph is plotted using the plot function of
matplotlib, and labels and title are added to the graph. Then, the linear regression model from
sklearn is imported and the model is defined. The x and y variables are converted into 2D
arrays, and the model is fitted. The predicted values of y are calculated and the regression line
is plotted on the graph. Finally, the graph is shown.