1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

numpyで「ValueError: data type must provide an itemsize」と出たときの対処法

Last updated at Posted at 2020-05-20

numpyにて

今回Bertを使って算出されたベクトルを利用して,Cos類似度を算出していたところ,以下のようなエラーメッセージが.

ValueError: data type must provide an itemsize

簡単にいうと,「与えるデータ型を変更してください」とのこと.

解決法

引数$vector1,vector2$はlist型のベクトルとする

変更前


def cos_sim(v1, v2):
    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))

v1=np.array(vector1)
v2=np.array(vector2)
print(cos_sim(v1,v2))

変更後


def cos_sim(v1, v2):
    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))

v1=np.array(vector1,dtype=float)
v2=np.array(vector2,dtype=float)
print(cos_sim(v1,v2))

つまり,list型からnumpyに変更するとき,第二引数に「dtype=float」を追加しろってこと.

なるほどね.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?