设置具有序列的数组元素。请求的数组在 afters1dimensions 后具有非均匀形状。检测到的形状为(18,)+非均匀部分

Setting an array element with a sequence.The requested array has an inhomogeneous shape after1dimensions.The detected shapewas(18,)+inhomogeneouspart

提问人:Kkura 提问时间:2/1/2023 最后编辑:Kkura 更新时间:2/1/2023 访问量:3867

问:

我正在尝试在这两个变量之间做一个散点图,但它给了我这个错误。

ValueError:设置带有序列的数组元素。请求的数组在 1 维后具有不均匀的形状。检测到的形状是(18,)+不均匀部分。

def plot():
    Age_list=[]
    Lactate_list=[]
    for l in range(1,19):
        Age = test[test.ID == l]['age']
        Lactate = (test[test.ID == l]['VO2'].nlargest(n=5).mean())*(80/100)
        Lactate_list.append(Lactate)
        Age_list.append(Age)

    plt.scatter(Age_list, Lactate_list,color='purple')
    a, b = np.polyfit(Age_list, Lactate_list, 1)
    plt.plot(Age_list, a*np.array(Age_list)+b)
    plt.xlabel('Age')
    plt.ylabel('Lactate threshold')
    plt.title('Correlation between Age and Lactate threshold')
    plt.show()

如果我打印,那么 Age_list 的长度和 Lactate_list 它给出的长度相同。我不明白问题出在哪里。乳酸是括号内的 80%。我是怎么做到的可以吗?

Python 数组 绘制 序列 维度

评论


答:

2赞 Yacine 2/1/2023 #1

引发错误是因为 Age_list 和 Lactate_list 的元素不是具有相同形状的数组。Age_list的元素是级数,而Lactate_list的元素是标量。

试试这个

def plot():
age_list = []
lactate_list = []
for l in range(1, 19):
    age = test[test.ID == l]['age'].values[0]
    lactate = (test[test.ID == l]['VO2'].nlargest(n=5).mean())*(80/100)
    lactate_list.append(lactate)
    age_list.append(age)

plt.scatter(age_list, lactate_list, color='purple')
a, b = np.polyfit(age_list, lactate_list, 1)
plt.plot(age_list, a*np.array(age_list) + b)
plt.xlabel('Age')
plt.ylabel('Lactate Threshold')
plt.title('Correlation between Age and Lactate Threshold')
plt.show()

评论

0赞 Kkura 2/1/2023
是的!对不起,我是新来的