not understanding plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r') in ML

Viewed 646
plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r') **--this line**
plt.xlabel("Engine size")
plt.ylabel("Emission")

What is the meaning of this syntax?
train_x is the EngineSize vs formula ie. theta0+theta1*~x(mean) but what is this -r in the syntax & then how is it plotting... Kindly someone elaborately explain me dis

2 Answers

`plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS, color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r'))' before explain the code above let me clear that
The linear regression equation is the below :\

y=a+bx\

a: intercept
b: sloop/gradient(coefficient)

back to the code, it is simply a scatter plot between the actual values and the predicted values to compare the accuracy of a regression model.

the part

regr.coef_[0][0]*train_x + regr.intercept_[0]


regr.coef_: this is a list variable that holds the b variable (coefficient) , so regr.coef_[0][0] aiming to select only the first value in the list which is the coefficient.
regr.intercept_:it is also a list but it holds the a variable (intercept).\

The conclusion is that:

regr.coef_[0][0]*train_x + regr.intercept_[0] is equevelant to y=a+bx

'-r' : This is the line color in the plot
you can use '-y' :for yellow or '-b' for blue

I wish this makes it clear to you :).

This is the function for plot the trainning sets(blue points):

plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')

This is the function for plot the equation or the model (red line):

plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r')

In plot function you need to specify (X,Y) for plotting. So X = Train_x(ENGINESIZE Values) and Y = the equation (Y=0º+0¹X) -> 0º=regr.intercept_[0] and 0¹= regr.coef_[0][0]

In other words: plt.plot(X,0º+0¹*X,'-r')

'r -> color of line: red

I hope this explanation helps you. Good Luck!

Related