gpt4 book ai didi

python - 类型错误 : 'numpy.float64' object cannot be interpreted as an integer and casting to int fails

转载 作者:行者123 更新时间:2023-12-01 07:42:51 37 4
gpt4 key购买 nike

我的代码-

import networkx as nx
import random
import numpy as np
import matplotlib.pyplot as plt
import math

def avg_deg(self,num_nodes):
return self.number_of_edges() * 2 / num_nodes


def avg_degree(num_nodes,target_deg):

G=nx.Graph()

G.add_nodes_from(range(num_nodes))
while avg_deg(G,num_nodes) < target_deg:
n1, n2 = random.sample(G.nodes(), 2)
G.add_edge(n1, n2, weight=1)

return G

a=np.arange(0,1, 0.001)
p_values=a.tolist()
p_values.pop(0)

graph=avg_degree(10000,4)

n_original=nx.number_of_nodes(graph)

n_edges = graph.number_of_edges()
graph.remove_edges_from(random.sample(graph.edges(),k=int(0.9*n_edges)))
data=[len(c) for c in sorted(nx.connected_components(graph), key=len, reverse=True)]



xx= list(set(data))

yy= [data.count(x) for x in set(data)]

xx = [math.log(record) for record in xx]
yy = [math.log(record) for record in yy]

plt.plot(xx,yy,'ro')
plt.xlabel('log(cluster_size)')
plt.ylabel('log(frequency)')
#plt.show()

plt.figure()

##################calculating exponent
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


def func(x, a, b, c):
return a* np.exp(-b * x) + c

popt, pcov = curve_fit(func, xx, yy,maxfev=5000)


plt.plot(xx, func(xx, *popt), 'r-',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))


plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

错误出现-

  plt.plot(xx, func(xx, *popt), 'r-',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
File "gaussian.py", line 82, in func
return a * np.exp(-b * x) + c

我试图通过将所有 a、b、c 转换为 int 来解决这个问题,但这也给了我一个错误 -

D:\anaconda\lib\site-packages\scipy\optimize\minpack.py:785: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
Traceback (most recent call last):
File "gaussian.py", line 87, in <module>
plt.plot(xx, func(xx, *popt), 'r-',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
File "D:\anaconda\lib\site-packages\matplotlib\pyplot.py", line 3261, in plot
ret = ax.plot(*args, **kwargs)
File "D:\anaconda\lib\site-packages\matplotlib\__init__.py", line 1717, in inner
return func(ax, *args, **kwargs)
File "D:\anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 1372, in plot
for line in self._get_lines(*args, **kwargs):
File "D:\anaconda\lib\site-packages\matplotlib\axes\_base.py", line 404, in _grab_next_args
for seg in self._plot_args(this, kwargs):
File "D:\anaconda\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_args
x, y = self._xy_from_xy(x, y)
File "D:\anaconda\lib\site-packages\matplotlib\axes\_base.py", line 243, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (11,) and (0,)
TypeError: 'numpy.float64' object cannot be interpreted as an integer

我的代码正在图表中绘制日志(频率)与日志(簇大小)。现在我想找到指数曲线的 a、b 和 c,因此我使用 scipy 函数来实现。基本上我试图找到斜率 ~ pk^-y 并且我试图找到 y,因此我想使用 scipy 的曲线拟合方法来找到它。

最佳答案

您应该将函数function更改为

def func(x, a, b, c):
return a* np.exp(-b * np.array(x)) + c

因为这里的参数应该是 numpy 数组 而不是 python 列表

关于python - 类型错误 : 'numpy.float64' object cannot be interpreted as an integer and casting to int fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56605289/

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