不包含签名匹配类型的循环

不包含签名匹配类型的循环

numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U1580'), dtype('<U1580')) -> dtype('<U1580')


我正在尝试执行以下操作:

dist = np.linalg.norm(encoding - db_enc)


其中编码是通过POST请求发送的人脸的128维向量,而db_enc是我从Sqlite3数据库中获取的另一个128维向量。我在执行此步骤时遇到上述错误。这是详细的编码,

print("ENC ", type(encoding), encoding, np.shape(encoding))

ENC  <class 'numpy.ndarray'> [[ 0.09602562  0.05026172  0.106613    0.11637867  0.13730541  0.18687613
   0.13980152 -0.10983124  0.01835017 -0.06333953 -0.06147085  0.0549635
   0.02799656  0.00092633  0.11221859 -0.05192785  0.0525886   0.07087591
  -0.10972961  0.01514653  0.01200427  0.05329731  0.04005743  0.18915842
   0.04145951 -0.20928082 -0.16869229 -0.06036214 -0.04235162  0.07824261
   0.07138225  0.1312568  -0.09781478  0.12874082  0.03722986 -0.01082433
   0.10047887  0.00800437 -0.04152641 -0.0306553   0.13605222  0.01289378
   0.05760223 -0.19195035 -0.12237714 -0.07873389  0.00380635  0.02028097
   0.02223298  0.06474376  0.10941633 -0.14064552  0.08936299  0.00388683
   0.07314923  0.04556359 -0.02430227  0.11343706  0.00779198 -0.00964779
  -0.08630823  0.12208205 -0.05156697 -0.3145251   0.10649662  0.09643669
   0.05130563 -0.07409885 -0.26777127  0.05541156 -0.00147743  0.11800249
  -0.01156744  0.06501945 -0.00601065  0.085051    0.07932617 -0.08163948
   0.04494501 -0.03012008  0.01525374  0.06678153  0.01800733 -0.0201263
   0.07194183 -0.03790623  0.00289917 -0.0186056  -0.13076556  0.10577566
   0.07499494 -0.11745209  0.09151422  0.00840714 -0.06819624  0.0245196
  -0.01500007 -0.00589196  0.05808676  0.09107708  0.07078542  0.00117582
  -0.04596691  0.12681791 -0.06487873 -0.00370738 -0.02379089 -0.0916268
  -0.07825013  0.06853001 -0.00962984  0.06818784  0.07526734 -0.0005998
  -0.01838216 -0.00074842 -0.08485842  0.10574327 -0.12667136  0.10527002
   0.0553649   0.0220931  -0.04728016  0.01509122  0.01982033  0.10371249
  -0.06818289 -0.1270394 ]] (1, 128)


和相应的数据库值

print("DB ", type(db_enc), db_enc, np.shape(db_enc))

DB  <class 'numpy.ndarray'> [[ 0.1616196  -0.06403983  0.00219853 -0.04197867  0.09346917  0.20562127
   0.12494124  0.02679994 -0.127634   -0.1126218  -0.03107646  0.04363777
   0.14626062  0.03845457  0.07879496 -0.11283173  0.01420811 -0.02750769
  -0.06326418  0.13397539  0.07688539  0.05936693  0.00945647  0.15419547
  -0.08248097 -0.07293285 -0.14824665 -0.15512463 -0.00425251  0.07445055
  -0.03990018  0.0395442  -0.14082795  0.0743586   0.02989104  0.02714477
   0.01490993  0.09542259 -0.03989129 -0.02652638  0.08977504  0.03180737
  -0.10132375 -0.07919461 -0.15734208  0.01482354  0.03443371  0.03917413
  -0.08184692  0.04209467 -0.03919723 -0.11930565  0.08438748  0.07760657
   0.08840926 -0.01585769 -0.01170482  0.17661    -0.04050164 -0.08612555
  -0.03465237  0.15780036  0.07520261 -0.17770033  0.02524511  0.02246985
   0.11478782  0.03914222 -0.09477007 -0.12463204  0.03759931  0.05503495
  -0.00029514 -0.00599168  0.01094351  0.09305067 -0.04536207 -0.10035587
   0.1543854   0.11397277  0.0400256   0.01436814 -0.01748643 -0.08823788
   0.0320038  -0.00023023  0.10030443  0.01394301 -0.07903008  0.03293093
   0.15368395 -0.11213656  0.07401505  0.06123362 -0.1783725  -0.0788461
   0.04645948 -0.00024172  0.03527135  0.02592313  0.11341458  0.07026253
  -0.0544541   0.09657887 -0.17342299  0.0612987  -0.07760912  0.00467722
  -0.10431045  0.03944225  0.10737552  0.07386835 -0.06278969 -0.04545485
  -0.06690471  0.12865026 -0.18798149 -0.01798184 -0.168064    0.05734041
  -0.07086241 -0.08988202 -0.01063311  0.02298436 -0.03099938  0.11156233
  -0.12110745 -0.09365971]] ()


可能的解决方案是什么?

最佳答案

这里的问题是,从数据库中检索到的值是一个字符串。

这是在ndarray

[[ 0.1616196... ]]


但“内部括号”中的内容是字符串。

因此,在重塑后,我能够执行所需的操作。

关于python - numpy.core._exceptions.UFuncTypeError:ufunc'subtract'不包含签名匹配类型的循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59196051/

10-11 16:41