Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: x and y must have same first dimension

I get an error message

ValueError: x and y must have same first dimension.

Here is the code:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np

date,bid,ask = np.loadtxt('GBPUSD1d.txt', unpack=True,
               delimiter =',',converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')}) 
avgLine = ((bid+ask)/2)
patternAr = []
performanceAr = []
patForRec = []
eachPattern = []

def percentChange(startPoint, currentPoint):
        return ((float(currentPoint)- startPoint)/abs(startPoint))*100.00

def patternStorage():
        patStartTime = time.time()
        x = (len(avgLine))-30
        y = 11
        while y < x:
                pattern = []
                p1 = percentChange(avgLine[y-10], avgLine[y-9])
                ...
                p10 = percentChange(avgLine[y-10], avgLine[y])

                outcomeRange = avgLine[y+20:y+30]
                currentPoint = avgLine[y]
                try:
                        avgOutcome = reduce(lambda x, y: x + y, outcomeRange) / len(outcomeRange)
                except Exception, e:
                        print str(e)
                        avgOutcome = 0

                futureOutcome = percentChange(currentPoint, avgOutcome)
                pattern.append(p1)
                pattern.append(p2)
                pattern.append(p3)
                pattern.append(p3)
                pattern.append(p4)
                pattern.append(p5)
                pattern.append(p6)
                pattern.append(p7)
                pattern.append(p8)
                pattern.append(p9)
                pattern.append(p10)
                patternAr.append(pattern)
                performanceAr.append(futureOutcome)
                y += 1

        patEndTime = time.time()
        print len (patternAr)
        print len (performanceAr)
        print 'Patten storage took:', patEndTime - patStartTime, 'seconds'

def currentPattern():
    cp1 = percentChange(avgLine[-11], avgLine[-10])
    ...
    cp10 = percentChange(avgLine[-11], avgLine[-1])
    patForRec.append(cp1)
    ...  
    patForRec.append(cp10)
    print patForRec

def patternRecognition():
    for eachPattern in patternAr:
        sim1 = 100.00 - abs(percentChange(eachPattern[0], patForRec[0]))
        ...
        sim10 = 100.00 - abs(percentChange(eachPattern[9], patForRec[9]))
        howSim =((sim1+sim2+sim3+sim4+sim5+sim6+sim7+sim8+sim9+sim10))/float(10)

        if howSim > 70:
            patdex = patternAr.index(eachPattern)
            print 'predicted outcome',performanceAr[patdex]
            xp = [1,2,3,4,5,6,7,8,9,10]
            fig = plt.figure()
            plt.plot(xp, patForRec)
            plt.plot(xp, eachPattern)
            plt.show()

patternStorage()
currentPattern()
patternRecognition()
print (len(patForRec))
print (len(eachPattern))

Full error message

Traceback (most recent call last):
  File "C:\Python27\ANN.py", line 165, in <module>
    patternRecognition()
  File "C:\Python27\ANN.py", line 131, in patternRecognition
    plt.plot(xp, eachPattern)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3093, in plot
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 303, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 281, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
like image 916
user24534 Avatar asked Sep 02 '14 04:09

user24534


People also ask

What is Matplotlib Pyplot as PLT?

matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.


1 Answers

The problem is that eachPattern has a 11 elements in it, whereas all xp has 10. The reason for this is probably on lines 52 and 53 in the patternStorage function of your code where you append p3 to your list twice:

pattern.append(p3)
pattern.append(p3)

if you get rid of one of these the graph plots fine. Though it is stored in a loop to plot multiple times, don't know if you wanted to do that...

If you try and do more things inside loops, so you have to write less code, this sort of problem where you accidentally do something twice will happen less.

like image 107
John Sharp Avatar answered Oct 13 '22 00:10

John Sharp