Home > Software design >  How to show linear regression in plot Python
How to show linear regression in plot Python

Time:05-18

I have a problem because I'm creating a linear regression model. I calculated R2 but I have to show it on the chart. However, I do not know where to start. And what chart to show? You can advise me ? What else do I need for the chart?


train=  pd.get_dummies(train)
y = train['SalePrice'].values
X= train.drop('SalePrice',axis=1).values


X_train, X_test,y_train, y_test= train_test_split(X,y,test_size=0.3,random_state=42)
reg= LinearRegression()

#Fit training set to the regressor
regr = reg.fit(X_train,y_train)

#Make predictions with the regressor
y_pred = reg.predict(X_test)

#Calculate accuracy
R2= reg.score(X_test,y_test)
print(R2)

CodePudding user response:

here is how to plot an ols linear equation using slope and intercept

masses=[7.812435,7.698824,7.817183,7.872703,8.176541] 
volumes=[2.0,2.1,2.2,2.3,2.4]
df=pd.DataFrame({'masses':masses,'volumes':volumes})

model_fit = ols(formula="masses ~ volumes", data=df)
model_fit = model_fit.fit()

a0 = model_fit.params['Intercept']
a1 = model_fit.params['volumes']

# Print model parameter values with meaningful names, and compare to summary()
print( "container_mass   = {:0.4f}".format(a0) )
print( "solution_density = {:0.4f}".format(a1) )

x=np.linspace(0,15,16)
predicted_mass=a0 a1*x
plt.plot(x,predicted_mass)
plt.show()

here is how to plot using linear regressor

legs= np.array([35. ,
  36.5,
  38. ,
  39.5,
  41. ,
  42.5,
  44. ,
  45.5,
  47. ,
  48.5,
  50. ,
  51.5,
  53. ,
  54.5,
  56. ,
  57.5,
  59. ,
  60.5,
  62. ,
  63.5,
  65.]) 

heights= np.array([145.75166215,
  154.81989548,
  147.45149903,
  154.53270424,
  166.17450311,
  171.45325818,
  149.44608871,
  164.73275841,
  168.82025028,
  171.32607675,
  182.07638078,
  188.37513159,
  188.08738789,
  196.95181717,
  192.85162151,
  201.60765816,
  210.66135402,
  202.06143758,
  215.72224422,
  207.04958807,
  215.8394592 ])

model = LinearRegression(fit_intercept=True)

# Prepare the measured data arrays and fit the model to them
#shape(1,1)
legs = legs.reshape(len(heights),1)
heights = heights.reshape(len(heights),1)
model_fit=model.fit(legs, heights)

# Use the fitted model to make a prediction for the found femur

fossil_leg = np.array([50.7]).reshape(1,-1)
fossil_height = model.predict(fossil_leg)
#index with [0,0]
print("Predicted fossil height = {:0.2f} cm".format(fossil_height[0,0]))

a0 = model_fit.intercept_[0]
a1 = model_fit.coef_[0,0]

min_fossil_leg=np.amin(legs)
max_fossil_leg=np.amax(legs)

input_fossil_legs=np.linspace(min_fossil_leg,max_fossil_leg,100)

predicted_height_predictions=[]
for fossil_leg in input_fossil_legs:
    fossil_leg=np.array(fossil_leg).reshape(1,-1)
    fossil_height = model.predict(fossil_leg)
    predicted_height_predictions.append(fossil_height[0,0])
    
plt.plot(legs,heights)
plt.plot(np.array(input_fossil_legs),predicted_height_predictions)
plt.show()

CodePudding user response:

I can show on the example of financial data. Where 'price' is the prices for the index, and 'ind' is an array ranging from 0 to the entire length of the 'price' array (in other words, these are indexes along the x axis).

from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader.data as web

df = web.DataReader('^GSPC', 'yahoo', start='2021-01-15', end='2021-10-01')
price = df['Close']
ind = np.arange(len(df)).reshape((-1, 1))

model = LinearRegression()
model.fit(ind, price)
reg = model.predict(ind)

fig, ax = plt.subplots()
ax.plot(ind, price)
ax.plot(ind, reg)

fig.autofmt_xdate()
plt.show()

enter image description here

  • Related