繁体   English   中英

设置一个带有序列的数组元素。请求的数组在1维后形状不均匀。检测到的形状为(18,)+inhomogeneouspart

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

我试图在这 2 个变量之间绘制散点图,但它给了我这个错误。

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%。 我是怎么做到的好吗?

抛出错误是因为 Age_list 和 Lactate_list 的元素不是具有相同形状的 arrays。 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()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM