numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types

Question:

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

I am trying to perform the following operation:

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

where encoding is a 128 dimensional vector of a face sent via a POST request and db_enc is another 128 dimensional vector that I am fetching from Sqlite3 database. I am getting the above error on performing this step. Here is the encoding in detail,

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)

and the corresponding database value

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]] ()

What could be the possible solution?

Asked By: Abhijith E

||

Answers:

The problem here was that the value when retrieved from database was a string.

This was in ndarray

[[ 0.1616196... ]]

but the content in Inner Brackets was a string.

So after reshaping I was able to perform the desired operation.

Answered By: Abhijith E
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.