gpt4 book ai didi

python - 为什么 numpy 不会计算一个 5 元素列表而不是另一个的标准偏差?

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:00 25 4
gpt4 key购买 nike

我正在尝试使用 Python 来解决一些简单的静态问题并生成结果图。出于某种原因,NumPy 在尝试计算我的计算结果的标准差时不接受我的数据(但对原始数据列表成功)。我需要更改 yerr=[std(f10)...第 61 行至 yerr=[std(solf10)... .然而,每次我尝试时,python 环境都会抛出以下错误:

Traceback (most recent call last):
File "C:\Users\evanlane\Dropbox\School\f13\homework\statics\lab1\data.py", line 70, in <module>
ax.errorbar(x, [solf10avg,solf12avg,solf15avg], yerr=[std(solf10),std(f12),std(f15)], lw=1.5)
File "C:\Program Files\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 2590, in std
keepdims=keepdims)
File "C:\Program Files\Python33\lib\site-packages\numpy\core\_methods.py", line 107, in _std
ret = um.sqrt(ret)
AttributeError: 'Float' object has no attribute 'sqrt'

我试图找出数据的结构是否与 print(type(f10), type(solf10)) 不同。但这表明它们都是 <class 'list'>类型。我应该如何按摩数据以更好地适应?我是 python 的新手,所以如果您有任何其他样式更正,也请告诉我。

完整代码:

# Imports
from sympy import *
from numpy import *
import matplotlib.pyplot as plt

# Constants
g = 9.81

# Given data
l1, l2, l3 = 0.023, 0.07492, 0.0325
mw = 0.220
w = g*mw

# Collected data
m10 = [1540,1500,1400,1400,1670]
m10kg = [x/1000 for x in m10]

m12 = [1220, 1300, 1200, 1050, 900]
m12kg = [x/1000 for x in m12]

m15 = [770, 790, 740, 760, 750]
m15kg = [x/1000 for x in m15]

# Conversion from mass to force in Newtons due to gravity
f10, f12, f15 = [x*g for x in m10kg], [y*g for y in m12kg], [z*g for z in m15kg]

# Averages of the data
f10avg, f12avg, f15avg = mean(f10), mean(f12), mean(f15)

# Instantiate symbolic variables
fr, my = symbols('fr, my')

# Equation of moment about the origin
sumMoments = Eq(fr, (w*l2+my*(l1+l2))/(l1+l2+l3))

# Newtons acting axially on the straw, solved from equation
solf10 = [solve(sumMoments.subs(my,x)) for x in f10]
solf12 = [solve(sumMoments.subs(my,x)) for x in f12]
solf15 = [solve(sumMoments.subs(my,x)) for x in f15]

solf10 = [x for sub1 in solf10 for x in sub1]
solf12 = [x for sub1 in solf12 for x in sub1]
solf15 = [x for sub1 in solf15 for x in sub1]

solf10avg, solf12avg, solf15avg = mean(solf10), mean(solf12), mean(solf15)

# Plotting section
# ------------------

# X positions
x = [10,12,15]

#Uncomment for hand-drawn style
#plt.xkcd()

fig = plt.figure()
ax = fig.add_subplot(111)

offset = .5

ax.errorbar(x, [solf10avg,solf12avg,solf15avg], yerr=[std(f10),std(f12),std(f15)], lw=1.5)
plt.text(x[0],solf10avg + offset, r' $F_{10 cm}=\ %.3f \ N$' %(solf10avg), fontsize=18)
plt.text(x[2],solf15avg + offset, r' $F_{15 cm}=\ %.3f \ N$' %(solf15avg), fontsize=18)
plt.text(x[1],solf12avg + offset, r' $F_{12 cm}=\ %.3f \ N$' %(solf12avg), fontsize=18)

plt.xlim([9,20])
plt.ylim([0,20])

plt.title("Straw Yield Point Test", fontsize=24)
plt.xlabel("Length (cm)", fontsize=18)
plt.ylabel("Axial Force on Straw\n at Yield (N)", fontsize=18)

plt.minorticks_on()
plt.grid(which="both")

#plt.savefig('fig_1.pdf')

plt.show()

最佳答案

您的一个 sympy 计算的输出是一个 sympy Float 对象,它不是 numpy 识别为应该强制转换为 C double 的对象。相反,它只是从中创建一个对象数组(即 dtype=object)。 numpy ufuncs 在对象数组上的工作方式是在对象上寻找同名的方法,所以 numpy.sqrt(solf10) 所做的相当于 numpy.array([x .sqrt() for x in solf10]).

明确地将列表中的值强制为真正的 float

solf10 = [float(x) for sub1 in solf10 for x in sub1]

关于python - 为什么 numpy 不会计算一个 5 元素列表而不是另一个的标准偏差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19369787/

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