简体   繁体   English

ValueError:x 和 y 必须具有相同的第一维,但具有形状

[英]ValueError: x and y must have same first dimension, but have shapes

I wonder how to best solve the following problem in my script: "ValueError: x and y must have same first dimension, but have shapes (1531,) and (1532,)".我想知道如何最好地解决我的脚本中的以下问题:“ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1531,) 和 (1532,)”。

What is the problem here?这里有什么问题? The problem is that the x and y axis of the plot don't share the exact same number of values (input) to plot.问题是 plot 的 x 轴和 y 轴与 plot 共享的值(输入)数量不同。 The result is the error message above.结果是上面的错误消息。

Let us look at the code first:我们先来看代码:

# Initialize
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from matplotlib.pyplot import cm

# Numpy.loadtxt – Loads data from a textfile.
# Scipy.signal.welch – Creation of the power-spectrum via welch method. f, Welch creates the ideal frequencies (f, Welch = Power Spectrum or Power Spectral Density)
Subjects = ["Subject1" "Subject2"]

for Subject in Subjects:
    Txt = np.loadtxt("/datadir.../{0}/filename...{0}.txt".format(Subject), comments="#", delimiter=None,
                         converters=None, skiprows=0, usecols=0, unpack=False, ndmin=0, encoding=None, max_rows=None, like=None)

    f, Welch = signal.welch(Txt, fs=1.0, window="hann", nperseg=None, noverlap=None, nfft=3062, detrend="constant", return_onesided=True, scaling="density", axis=-1, average="mean")

    BypassZero1 = f[f > 0.00000000000001] # Avoids "RuntimeWarning: divide by zero encountered in log"
    BypassZero2 = Welch[Welch > 0.00000000000001]

    Log_f = np.log(BypassZero1, out=BypassZero1, where=BypassZero1 > 0)
    Log_Welch = np.log(BypassZero2, out=BypassZero2, where=BypassZero2 > 0)

    plt.plot(Log_f, Log_Welch)

The code lines "BypassZero1" and "BypassZero2" tell Python to only use values above 0.00000000000001 for both "f" and "Welch".代码行“BypassZero1”和“BypassZero2”告诉 Python 对于“f”和“Welch”仅使用高于 0.00000000000001 的值。 Otherwise the problem "RuntimeWarning: divide by zero encountered in log" would occur in the following step where I apply the logarithm for both axes (Log_f and Log_Welch).否则,问题“RuntimeWarning:在 log 中遇到的除以零”将出现在我对两个轴(Log_f 和 Log_Welch)应用对数的以下步骤中。

This is where the problem occurs for the last plt.plot line of the code.这是代码的最后 plt.plot 行出现问题的地方。 It seems that a different number of numeric values are "left over" for "f" and "Welch" after the previous step of using the Welch method and applying the logarithm for both axes.在使用 Welch 方法并为两个轴应用对数的上一步之后,“f”和“Welch”似乎“剩余”了不同数量的数值。

I wonder if there is a possibility to deal with the 0.xxx values provided in the.txt file.我想知道是否有可能处理 .txt 文件中提供的 0.xxx 值。 Currently, only values above 0.00000000000001 for both f and Welch are used.目前,对于 f 和 Welch,仅使用高于 0.00000000000001 的值。 This will lead to the different number of values for x and y, hence resulting in the impossibility of plotting the data.这将导致 x 和 y 的值数量不同,从而导致无法绘制数据。

What could be a solution for this problem?有什么办法可以解决这个问题?

As you pointed out, the error message indicates that your two arrays are of different length.正如您所指出的,错误消息表明您的两个 arrays 的长度不同。 This is because the mask of the second array should be the same as the mask of the first.这是因为第二个数组的掩码应该与第一个数组的掩码相同。 Therefore, replacing BypassZero2 = Welch[Welch > 0.00000000000001] with BypassZero2 = Welch[f > 0.00000000000001] should fix the issue.因此,将BypassZero2 = Welch[Welch > 0.00000000000001]替换为BypassZero2 = Welch[f > 0.00000000000001]应该可以解决问题。

#Plot
plt.figure(figsize = (16,8))
plt.plot(anos, ratings)
plt.xlabel('\nAno')
plt.ylabel('Mediana de Avaliação')
plt.title('\nMediana de Avaliação dos Filmes Em Relação ao Ano de Estréia\n')
plt.show()

ValueError: x and y must have same first dimension, but have shapes.

Basically, x and y coordinates we are plotting must be of same length, so that we can make sure it plots one on one.基本上,我们正在绘制的 x 和 y 坐标必须具有相同的长度,以便我们可以确保它一对一地绘制。 Thus, ensure their lengths are equal.因此,确保它们的长度相等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3) 谁能帮我? ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (0,) - Can anyone help me? ValueError: x and y must have same first dimension, but have shapes (10,) and (0,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)/ 多处理 - ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)/ Multiprocessing Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题 - ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem ValueError:x和y必须具有相同的第一尺寸,但形状为(4200,)和(16800,1) - ValueError: x and y must have same first dimension, but have shapes (4200,) and (16800, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM