0

I am learning Python and I have a side project to learn to display data using matplotlib.pyplot module. Here is an example to display the data using dates[] and prices[] as data. Does anyone know why we need line 5 and line 6? I am confused why this step is needed to have the graph displayed.

from sklearn import linear_model
import matplotlib.pyplot as plt

def showgraph(dates, prices):
  dates  = numpy.reshape(dates, (len(dates), 1))   # line 5
  prices = numpy.reshape(prices, (len(prices), 1)) # line 6

  linear_mod = linear_model.LinearRegression()
  linear_mod.fit(dates,prices)
  plt.scatter(dates,prices,color='yellow') 
  plt.plot(dates,linear_mod.predict(dates),color='green')
  plt.show()
0

3 Answers 3

1

try the following in terminal to check the backend:

import matplotlib
import matplotlib.pyplot
print matplotlib.backends.backend

If it shows 'agg', it is a non-interactive one and wont show but plt.savefig works.

To show the plot, you need to switch to TkAgg or Qt4Agg.

You need to edit the backend in matplotlibrc file. To print its location in terminal do the following.

import matplotlib
matplotlib.matplotlib_fname()

more about matplotlibrc

Sign up to request clarification or add additional context in comments.

Comments

0

Line 5 and 6 transform what Im assuming are row vectors (im not sure how data and prices are encoded before this transformation) into column vectors. So now you have vectors that look like this.

[0,
 1, 
 2, 
 3]

which is the form that linear_model.Linear_Regression.fit() is expecting. The reshaping was not necessary for plotting under the assumption that data and prices are row vectors.

Comments

0

My approach is exactly like yours but still without line 5 and 6 display is correct. I think those line are unnecessary. It seems that you do not need fit() function because of your input data are in row format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.