0
使用循環工作逐行執行此函數。使用pandas.DataFrame.apply執行相同的函數返回ValueError:操作數不能與形狀一起廣播。 pandas.DataFrame.apply應該工作嗎?如果這是不容易解釋的事情之一,有關如何加速處理(除了多處理)的任何想法?pandas.DataFrame.apply ValueError:操作數無法與形狀一起廣播
#python 3.6
import pandas as pd # version 0.19.2
import numpy as np #
#gensim version 1.0.1
from gensim import models #https://radimrehurek.com/gensim/models/word2vec.html
df=pd.DataFrame({"q1":[['how', 'I', 'from', 'iPhone', 'keep', 'them', 'my', 'but', 'delete', 'iCloud', 'photos', 'in', 'can'],
['use', 'are', 'radio', 'What', 'commercial', 'cognitive', 'technology', 'in'],
['how', 'I', 'razor', 'prevent', 'burns', 'the', 'stomach', 'on', 'can']],
"q2":[['Can', 'remove', 'from', 'I', 'iPhone', 'removing', 'them', 'my', 'storage', 'photos', 'iCloud', 'without'],
['radio', 'from', 'Where', 'do', 'come', 'cognitive', 'distinction'],
['how', 'I', 'razor', 'prevent', 'can', 'burn']]})
#using pretrained model https://code.google.com/archive/p/word2vec/
w2v = models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
#This works
df['w2v_sim']=np.nan
for i in range(len(df)):
df['w2v_sim'].ix[i]=w2v.n_similarity(df['q1'].ix[i],df['q2'].ix[i])
print(str(df['w2v_sim'].ix[i]))
#this doesn't work
df['w2v_sim']=np.nan
df['w2v_sim']=df.apply(w2v.n_similarity(df['q1'],df['q2']),axis=1)
ValueError異常:操作數無法與形狀(13300)一起廣播(8300)
謝謝
沒有錯誤(它的工作)。非常感謝。 –