如果您想了解TypeError:“ML模型”无法迭代“numpy.float64”对象的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于'numpy.float64'对象不可迭代——独立工作
如果您想了解TypeError:“ ML模型”无法迭代“ numpy.float64”对象的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于'numpy.float64' 对象不可迭代——独立工作但不在 for 循环中、'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解、'numpy.float64' 对象在曲线拟合时不可调用、'numpy.float64' 类型的对象没有 len():我该如何解决这个问题?的有价值的信息。
本文目录一览:- TypeError:“ ML模型”无法迭代“ numpy.float64”对象
- 'numpy.float64' 对象不可迭代——独立工作但不在 for 循环中
- 'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解
- 'numpy.float64' 对象在曲线拟合时不可调用
- 'numpy.float64' 类型的对象没有 len():我该如何解决这个问题?
TypeError:“ ML模型”无法迭代“ numpy.float64”对象
如何解决TypeError:“ ML模型”无法迭代“ numpy.float64”对象
我如何避免在突出显示的行上出现错误,它使我不断产生关于float64类型不可迭代的错误。本质上,我试图在第一个函数中计算SGD的成本,然后在第二个函数中计算SGD。
def calculate_cost_gradient(W,X_batch,Y_batch):
# if only one example is passed (eg. in case of SGD)
if type(Y_batch) == np.float64:
Y_batch = np.array([Y_batch])
X_batch = np.array([X_batch]) # gives multidimensional array
distance = 1 - (Y_batch * np.dot(X_batch,W))
dw = np.zeros(len(W))
######## Error is here ########
for ind,d in enumerate(distance):
if max(0,d) == 0:
di = W
else:
di = W - (regularization_strength * Y_batch[ind] * X_batch[ind])
dw += di
dw = dw/len(Y_batch) # average
return dw
def sgd(features,outputs):
max_epochs = 5000
weights = np.zeros(features.shape[1])
nth = 0
prev_cost = float("inf")
cost_threshold = 0.01 # in percent
# stochastic gradient descent
for epoch in range(1,max_epochs):
# shuffle to prevent repeating update cycles
X,Y = shuffle(features,outputs)
for ind,x in enumerate(X):
ascent = calculate_cost_gradient(weights,x,Y[ind])
weights = weights - (learning_rate * ascent)
# convergence check on 2^nth epoch
if epoch == 2 ** nth or epoch == max_epochs - 1:
cost = compute_cost(weights,features,outputs)
print("Epoch is:{} and Cost is: {}".format(epoch,cost))
# stoppage criterion
if abs(prev_cost - cost) < cost_threshold * prev_cost:
return weights
prev_cost = cost
nth += 1
return weights
解决方法
distance
很可能是一个numpy整数,并且代码应为:
for ind,d in enumerate(range(distance)):
'numpy.float64' 对象不可迭代——独立工作但不在 for 循环中
如何解决''numpy.float64'' 对象不可迭代——独立工作但不在 for 循环中
当我把代码写成:
{ [key in string]: ItemType }
我收到一个错误:
for i in range(1,k):
idx = np.random.choice(data.shape[0],1,p=squared_distances/sum(squared_distances))
centroids[i]=data.[idx,:].toarray()
squared_distances=np.min(pairwise_distances(data,centroids[0:i+1],metric=''euclidean'')**2
但是当我运行单独的行时:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-71-dff0c6ebb89d> in <module>
1 for i in range(1,k):
2
----> 3 idx = np.random.choice(data.shape[0],p=squared_distances/sum(squared_distances))
4
5 centroids[i]=data[idx,:].toarray()
TypeError: ''numpy.float64'' object is not iterable
它给出的输出为:
idx = np.random.choice(data.shape[0],p=squared_distances/sum(squared_distances))
如果代码在触发上述错误的 for 循环中,我想知道有什么问题。
供参考:
idx
array([5147])
解决方法
错误是由 sum(squared_distances)
产生的,这意味着在某个时刻 squared_distances
不再是 numpy.ndarray
而变成 numpy.float64
。显然,这发生在 squared_distances = np.min(...)
,因为 np.min
默认产生一个标量,计算整个数组的最小值。为了防止它,您需要根据 np.min
结果的维度为 axis
提供正确的 np.min(...,axis=0)
参数(np.min(...,axis=1)
或 pairwise_distances
)。
'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解
如何解决''numpy.float64'' 对象不能解释为整数,Python 中的 ODE 求解
我正在做一个热 Simulink 工作,我收到一条错误消息,说“''numpy.float64'' 对象不能被解释为一个整数”。我找不到那个错误以及如何解决这个问题。
首先我读取一个文本文档并为初始条件创建一个数组。(ycut)
接下来,我将创建一个函数“EPCMchg”
然后我正在使用“odeint”进行模拟。实际上我正在转换 MATLAB 代码,它具有 ODE45 微分方程求解器。它在 python 中的等价物是 odeint。 我阅读了许多与错误“''numpy.float64'' 对象不能被解释为整数”和 odeint 相关的文档,但无法捕获错误。
如果有人可以帮助我,非常感谢。提前致谢 这是代码和错误。
import numpy as np
import pandas as pd
import matplotlib
from math import sqrt
from scipy.integrate import odeint
ICHG = 1 #charge or ICHG=-1 discharge
print(ICHG)
df2 = pd.read_csv(ycut_file,sep=r''\\s{2,}'',engine=''python'',header=None,names=[''Value''])
ycut = df2.to_numpy().flatten()
**Output:**
ycut: [-4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01
5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02
-4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01
5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02
-4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01
5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02
-4.000000e+01 5.500000e+02 -4.000000e+01 5.500000e+02 -4.000000e+01
5.500000e+02 -3.999990e+01 5.500001e+02 -3.999980e+01 5.500003e+02
-3.999950e+01 5.500011e+02 -3.999840e+01 5.500034e+02 -3.999540e+01
5.500098e+02 -3.998740e+01 5.500267e+02 -3.996830e+01 5.500675e+02
-3.992660e+01 5.501580e+02 -3.984240e+01 5.503420e+02 -3.968600e+01
5.506877e+02 -3.941640e+01 5.512901e+02 -3.898240e+01 5.522699e+02
-3.832740e+01 5.537646e+02 -3.739550e+01 5.559138e+02 -3.613990e+01
5.588393e+02 -3.453170e+01 5.626245e+02 -3.256610e+01 5.672970e+02
-3.026550e+01 5.728186e+02 -2.767850e+01 5.790859e+02 -2.487450e+01
5.859400e+02 -2.193520e+01 5.931861e+02 -1.894460e+01 6.006170e+02
-1.597890e+01 6.080382e+02 -1.309800e+01 6.152899e+02 -1.034030e+01
6.222625e+02 -7.719300e+00 6.289058e+02 -5.224500e+00 6.352294e+02
-2.824200e+00 6.412981e+02 -4.698000e-01 6.472213e+02 1.899100e+00
6.531412e+02 4.350000e+00 6.592214e+02 6.956000e+00 6.656405e+02
9.798900e+00 6.725974e+02 1.298260e+01 6.803386e+02 1.666380e+01
6.892234e+02 2.110970e+01 6.998522e+02 2.679910e+01 7.132907e+02
3.458400e+01 7.314356e+02 4.593790e+01 7.575796e+02 6.331840e+01
7.972448e+02 9.068090e+01 8.593479e+02 1.355955e+02 9.271171e+02]
def EPCMchg(x,t,NSTAGES,Tf1,Tf2,Tf3,Hf1,Hf2,Hf3,TankD,TankH,deltaz,porosity,dp,Cpf,kf,rhof,Cpb,kb,rhob,ufluid,mdotsaltflow,hotHTF):
#Models an encapsulated Thermal Energy Storage system with multiple salt PCM Assumed salt properties
#for NaNO3 with freezing temp=frzHTF
#(650F),cold HTF fluid from steam generator=coldHTF (550f),and hot temp from solar=hotHTF (1050F).
#Intermediate Calculations of parameters used in differential equations
# Surface area of Tank for calculating Mass veLocity of fluid
pi = 3.14159
atank = pi*TankD*TankD/4 #ft^2
#Volume of each element of height deltaz
deltaV = pi*TankD*TankD*deltaz/4 # ft^3
#Properties of heat transfer fluid used in differential eqns
rhocpf=rhof*Cpf*porosity # btu/ft3/F
#Particle surface area per unit volume ft2/ft3
dparticle=dp/304.8 #[ft] dp mm particle
av=(6/dparticle) #ft2/ft3
#heat transfer coefficnet inside particle (use a simple thermal conductivit/particle radius
hsl0=kb/dparticle #heat transfer coeff particle solid,Btu/h/ft2/F
#heat transfer coefficnet of liquid inside particle (use a simple thermal conductivit/particle radius
#150% of solid to account for convection
hl0=hsl0*1.5 #heat transfer coeff particle liquid,Btu/h/ft2/F
#Input fluid Temperature at top of the tank during charging from the solar field
prevTf= hotHTF #deg F
#Bottom Salt 1 is the bottom,NSTAGES=top
rany =NSTAGES+1
#xdot = np.zeros(rany)
for STAGE in range(rany,-1):
HbSTAGE = x[2*STAGE-1] #Solid Enthalpy (btu/lb)
TfSTAGE = x[2*STAGE] # FLuid Temperature,F
#[ Tf,Hf,dh ] = saltstage( STAGE )
#Bottom Stages
Tf,dh=Tf1,0
#Middle stages
if STAGE > NSTAGES/3:
Tf,dh=Tf3,Cpb*(Tf-Tf1)
#Top stages
if STAGE > 2 * (NSTAGES/3):
Tf,dh=Tf2,Cpb*(Tf-Tf1)
# Determine conditions inside capsules (molten,solid or fractional
#solid
if HbSTAGE<(Hf+dh):
xsolid=1-(HbSTAGE-dh)/Hf #fraction solidification inside capsule
if xsolid<=0:
xsolid=0
else:
xsolid=1
TbSTAGE=Tf
hsl=hsl0/sqrt(xsolid) #heat transfer coeff of solid varies as sqrt of soli farction
hl=hl0/(1-sqrt(xsolid)) #heat transfer coeff of liquid remaining
u=1/((1/ufluid)+(1/hsl)+(1/hl))
uav=u*av
tauconst=(mdotsaltflow*Cpf/atank)/rhocpf #constnts in diff equations
aconst=uav/rhocpf #constnts in diff equations
cconst=uav/rhob/(1-porosity) #constnts in diff equations
#solid cooling
if HbSTAGE<dh:
xsolid=1
TbSTAGE = Tf+(HbSTAGE-dh)/Cpf
hsl=hsl0/sqrt(xsolid)
u=1/((1/ufluid)+(1/hsl))
uav=u*av
tauconst=(mdotsaltflow*Cpf/atank)/rhocpf
aconst=uav/rhocpf
cconst=uav/rhob/(1-porosity)
# sensible heat in liquid free convection hl=90
if HbSTAGE>(Hf+dh):
xsolid=0
TbSTAGE=Tf+(HbSTAGE-Hf-dh)/Cpf
hl=hl0/(1-sqrt(xsolid))
u=1/((1/ufluid)+(1/hl))
uav=u*av
tauconst=(mdotsaltflow*Cpf/atank)/rhocpf
aconst=uav/rhocpf
cconst=uav/rhob/(1-porosity)
xdot[2*STAGE-1] = cconst*(TfSTAGE - TbSTAGE) # capsule enthalpy
xdot[2*STAGE] = tauconst*(prevTf - TfSTAGE)+ aconst * (TbSTAGE-TfSTAGE) #HTF Temp
#PREPARE FOR NEXT STAGE :
prevTf = TfSTAGE
xs[STAGE]=xsolid
xs4=xs
xdot = xdot
return xs4,xdot;
## Start simulation:
t_final=4.0
tspan = np.linspace(0,t_final)
tspan
**Output:**
<class ''numpy.ndarray''>
array([0.,0.08163265,0.16326531,0.24489796,0.32653061,0.40816327,0.48979592,0.57142857,0.65306122,0.73469388,0.81632653,0.89795918,0.97959184,1.06122449,1.14285714,1.2244898,1.30612245,1.3877551,1.46938776,1.55102041,1.63265306,1.71428571,1.79591837,1.87755102,1.95918367,2.04081633,2.12244898,2.20408163,2.28571429,2.36734694,2.44897959,2.53061224,2.6122449,2.69387755,2.7755102,2.85714286,2.93877551,3.02040816,3.10204082,3.18367347,3.26530612,3.34693878,3.42857143,3.51020408,3.59183673,3.67346939,3.75510204,3.83673469,3.91836735,4. ])
if ICHG == 1:
[tchg,ychg]=odeint(EPCMchg,ycut,tspan,args=(NSTAGES,hotHTF))
plt.plot(tchg,ychg)
plt.show()
**Output:**
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-4b38269782f5> in <module>
1 #call ode45 with function EPCMchg to charge
2 if ICHG == 1:
----> 3 [tchg,hotHTF))
4 plt.plot(tchg,ychg)
5 plt.show()
C:\\ProgramData\\Anaconda3\\lib\\site-packages\\scipy\\integrate\\odepack.py in odeint(func,y0,args,Dfun,col_deriv,full_output,ml,mu,rtol,atol,tcrit,h0,hmax,hmin,ixpr,mxstep,mxhnil,mxordn,mxords,printmessg,tfirst)
239 t = copy(t)
240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func,242 full_output,243 ixpr,<ipython-input-11-6746119c69d3> in EPCMchg(x,hotHTF)
26 rany =NSTAGES+1
27 #xdot = np.zeros(rany)
---> 28 for STAGE in range(rany,-1):
29 HbSTAGE = x[2*STAGE-1] #Solid Enthalpy (btu/lb)
30 TfSTAGE = x[2*STAGE] # FLuid Temperature,F
TypeError: ''numpy.float64'' object cannot be interpreted as an integer
'numpy.float64' 对象在曲线拟合时不可调用
如何解决''numpy.float64'' 对象在曲线拟合时不可调用
我正在尝试在 CSV 数据中拟合正弦曲线以找到曲线的最大值。数据类似于
Time Voltage Current
0 -0.02500 -1.4 0.38
1 -0.02498 -1.6 0.32
2 -0.02496 -1.8 0.40
3 -0.02494 -1.4 0.44
4 -0.02492 -1.6 0.30
5 -0.02490 -2.0 0.40
6 -0.02488 -1.8 0.44
7 -0.02486 -1.4 0.32
8 -0.02484 -1.4 0.42
9 -0.02482 -1.6 0.46
10 -0.02480 -1.8 0.36
这是代码:
from numpy import sin
from scipy.optimize import curve_fit
from matplotlib import pyplot
# input and output variables
x,y = dfi["Time"],dfi["Voltage"]
# defining the function
def func(x,a,b,c,d):
return a * sin(b(x-c)) + d
# curve fit
popt,_ = curve_fit(func,x,y)
它给了我这个错误:
<ipython-input-20-f95822761dd9> in objective(x,d)
9 # defining the function
10 def objective(x,d):
---> 11 return a * sin(b(x-c)) + d
12
13 # curve fit
TypeError: ''numpy.float64'' object is not callable
这是我第一次尝试曲线拟合,我不明白这个错误。
'numpy.float64' 类型的对象没有 len():我该如何解决这个问题?
如何解决''numpy.float64'' 类型的对象没有 len():我该如何解决这个问题?
我正在尝试计算我的 10,000 个数字列表中大于 1.6 的值总数。我尝试了几种方法:
for value in chi_sq_values:
if value > 1.6:
print(len(value))
给了我标题中的错误,但以下有效
greater = [i for i in chi_sq_values if i > 1.6]
total = len(greater)
print(total)
我想尝试两种方法来验证我的答案,我该如何修复第一种方法以使其有效?
解决方法
在第一个代码片段中,您尝试打印 len
的 value
。由于 numpy.float64
没有 len,您会收到错误消息。
因此,为了计算所有大于 1.6 的值,您可以简单地使用计数器:
counter = 0
for value in chi_sq_values:
if value > 1.6:
counter += 1
print(counter)
,
在您的第一个示例中,您迭代列表的项目并直接访问该值。但是,该项目没有“len()”。您需要自己生成列表,以便评估长度。
chi_sq_values = [1,2,3]
greater = []
for value in chi_sq_values:
if value > 1.6:
print(value)
greater.append(value)
print(len(greater))
,
该错误意味着您无法在 len
对象上调用 numpy.float64
,因此您需要以不同的方式执行此操作。
假设 chi_sq_values
是普通列表,这里是如何快速完成
np.count_nonzero(np.asarray(chi_sq_values) > 1.6)
关于TypeError:“ ML模型”无法迭代“ numpy.float64”对象的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于'numpy.float64' 对象不可迭代——独立工作但不在 for 循环中、'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解、'numpy.float64' 对象在曲线拟合时不可调用、'numpy.float64' 类型的对象没有 len():我该如何解决这个问题?的相关信息,请在本站寻找。
本文标签: