import numpy as np import networkx as nx import matplotlib.pyplot as plt from scipy…
- User
import numpy as np import networkx as nx import matplotlib.pyplot as plt from scipy import stats import pandas as pd
def fractal_dimension_box_counting(G, box_sizes): """ 盒覆盖算法计算分形维数 :param G: networkx图,节点需包含坐标属性(如'pos') :param box_sizes: 盒子大小列表(单位与坐标一致) :return: 分形维数Df, (log(1/s), log(N(s))) """ positions = np.array([G.nodes[n]['pos'] for n in G.nodes]) min_coords = np.min(positions, axis=0) max_coords = np.max(positions, axis=0)
codelog_n = [] log_inv_s = [] for s in box_sizes: # 生成覆盖网格 x_bins = np.arange(min_coords[0], max_coords[0] + s, s) y_bins = np.arange(min_coords[1], max_coords[1] + s, s) # 统计非空盒子数量 H, _, _ = np.histogram2d(positions[:, 0], positions[:, 1], bins=(x_bins, y_bins)) N = np.sum(H > 0) log_n.append(np.log(N)) log_inv_s.append(np.log(1 / s)) # 线性拟合分形维数 slope, intercept, r_value, _, _ = stats.linregress(log_inv_s, log_n) Df = slope # 绘制双对数坐标图 plt.figure(figsize=(10, 6)) # plt.loglog(degrees_unique, prob, 'o', markersize=4, label='Degree distribution') plt.plot(log_inv_s, log_n, 'o', markersize=4, label='Degree distribution') return Df, (log_inv_s, log_n)def detect_change_point(log_inv_s, log_n): """ 检测双对数曲线中的拐点(变点检测简化版) :return: 拐点索引(对应box_sizes中的位置) """ residuals = [] for i in range(1, len(log_inv_s) - 1): # 分段拟合 x1, y1 = log_inv_s[:i], log_n[:i] x2, y2 = log_inv_s[i:], log_n[i:] slope1, intercept1, _, _, _ = stats.linregress(x1, y1) slope2, intercept2, _, _, _ = stats.linregress(x2, y2) residuals.append(np.sum((y1 - (slope1 * x1 + intercept1)) ** 2) + np.sum( (y2 - (slope2 * x2 + intercept2)) ** 2))
codeif len(residuals) == 0: return 0 change_point = np.argmin(residuals) + 1 return change_pointdef assign_hierarchy(G, box_sizes, change_point_idx): """ 根据拐点划分主干与支线 :param change_point_idx: 拐点在box_sizes中的索引 :return: 带有层级标签的图('hierarchy'属性) """ s_threshold = box_sizes[change_point_idx]
code - Assistant
你的代码整体上是实现了基于盒子覆盖法的分形维数计算和主干/支线的检测和分类,但在实现上可能会遇到几个问题和改进建议。以下是一些你可能需要注意的事项和优化建议:
-
坐标属性检查:
- 在你使用节点的坐标属性时,确保每个节点都包含 属性。如果某个节点缺少该属性,代码会抛出异常。
-
- User
为什么会报错 File "F:\code\Pycharm\pythonProject1\box_covering2.py", line 57, in detect_change_point residuals.append(np.sum((y1 - (slope1 * x1 + intercept1)) ** 2) + np.sum( ~~~~~~~^~~~ TypeError: can't multiply sequence by non-int of type 'numpy.float64'
- Assistant
这个错误提示表明在进行
slope1 * x1的乘法操作时,x1的类型可能是一个列表或其他非NumPy数组类型,而不是一个可以进行数值运算的NumPy数组。这导致了TypeError错误,因为非数值类型不能与浮点数相乘。为了修复这个问题,你可以确保在使用之前将
x1和y1转换为 NumPy 数组。这样可以确保它们的操作不会引发类型错误。修改你的
detect_change_point函数,确保在进行线性回归计算之前,x1和y1被转换为 NumPy 数组。以下是相应的修改:pythondef detect_change_point(log_inv_s, log_n): """ 检测双对数曲线中的拐点(变点检测简化版) :return: 拐点索引(对应box_sizes中的位置) """ residuals = [] for i in range(1, len(log_inv_s) - 1): # 分段拟合 x1, y1 = np.array(log_inv_s[:i]), np.array(log_n[:i]) x2, y2 = np.array(log_inv_s[i:]), np.array(log_n[i:]) slope1, intercept1, _, _, _ = stats.linregress(x1, y1) slope2, intercept2, _, _, _ = stats.linregress(x2, y2) residuals.append(np.sum((y1 - (slope1 * x1 + intercept1)) ** 2) + np.sum( (y2 - (slope2 * x2 + intercept2)) ** 2)) if len(residuals) == 0: return 0 change_point = np.argmin(residuals) + 1 return change_point