gpt4 book ai didi

python - 值错误 : x and y must have same first dimension

转载 作者:太空狗 更新时间:2023-10-30 01:01:28 26 4
gpt4 key购买 nike

我收到一条错误消息

ValueError: x and y must have same first dimension.

代码如下:

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))

完整的错误信息

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

最佳答案

问题是 eachPattern 有 11 个元素,而 xp 有 10 个元素。原因可能在 的第 52 和 53 行您的代码的 >patternStorage 函数,您将 p3 两次添加到您的列表中:

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

如果您去掉其中的一个,图表就可以绘制得很好。虽然它存储在一个循环中多次绘制,但不知道你是否想这样做......

如果你尝试在循环内做更多的事情,那么你必须编写更少的代码,这种你不小心做了两次的问题就会更少发生。

关于python - 值错误 : x and y must have same first dimension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com