python - 如何将参数传递给python函数错误
问题描述
我有这段代码:
points = np.array([[207.0,489.0], [500.0,58.0], [84.0,17.0],[197.0,262.0]])
#read data from the csv file
x = df["XC"]
y = df["YC"]
# make the x,y a point type
pointP = np.array([x,y])
#print(pointP)
rW,rV,rU = calcRatios(points,pointP,ratioU,ratioV,ratioW)
这是 calcRatios 函数
def calcRatios(points,centreP,ratioU,ratioV,ratioW):
v0 = points[1] - points[0]
v1 = points[2] - points[0]
v2 = centreP - points[0]
#dot product of the vects
d00 = np.dot(v0, v0)
d01 = np.dot(v0, v1)
d11 = np.dot(v1, v1)
d20 = np.dot(v2, v0)
d21 = np.dot(v2, v1)
#calc denom
denom = d00 * d11 - d01 * d01
#barycentric ratios of v,w,u
ratioV = (d11 * d20 - d01 * d21) / denom
ratioW = (d00 * d21 - d01 * d20) / denom
ratioU = 1.0 - ratioV - ratioW
return ratioV,ratioW,ratioU
数据帧中的数据存储如下:
Index XC YC R G B
1 0 0 227 227 227
2 1 0 237 237 237
3 2 0 0 0 0
4 3 0 232 232 232
5 4 0 233 233 233
... ... ... ... ... ... ...
但是,现在我传递给函数的 centerP 点似乎存在问题,我不知道为什么。
我得到的错误说:ValueError: operands could not be broadcast together with shapes (2,28686) (2,)
对于这一行 v2 = centreP - points[0]
有人能告诉我为什么会发生这种情况以及应该如何解决吗?
谢谢!
解决方案
您centreP
是两个长数组的数组:[[0, 1, 2...], [0,0,0...]]
, whilepoints[0]
是长度为 2 的单个数组, [207.0,489.0]
。numpy 的广播规则无法处理这两个形状的减法。但是,如果您转置centreP
到[[0,0], [1,0], [2,0]...]
它将通过points[0]
从每一行中减去来处理此问题:
v2 = centreP.T - points[0]
更好的是,通过df[[XC, YC]]
而不是pointP
- 它将被强制转换为正确形状的 numpy 数组。
rW,rV,rU = calcRatios(points, df[["XC","YC"]]) # no need to transpose
推荐阅读
- python - 为什么归一化直方图给出了错误的 y 值(都应该加起来为 1)?Python matplotlib
- python - 测试返回 render_template 的 Flask 方法
- load - 散景负载平衡策略中的 --num-procs 选项
- c# - 如何使用 C# 比较数据库中的日期和 ms 访问中的当前日期?
- css - JavaFX:多个 TabPanes 的自定义样式
- angular - I want the data in input fields to be removed when I click on the current page link
- flutter - How to draw vertical lines of different heights in flutter?
- android - 尝试使用 Flutter 从 Firebase 下载 PDF
- botframework - Botframework V4 提示中的额外新行
- python - 如何保存使用 tf.keras.estimator.model_to_estimator 创建的张量流估计器?