


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、中文自然語言處理一商品評論情感判別1、數(shù)據(jù)集下載fromsklearn.model_selectionimporttrain_test_splitfromgensim.models.word2vecimportWord2Vecimportnumpyasnpimportpandasaspdimportjiebafromsklearn.externalsimportjoblibfromsklearn.svmimportSVC2、載入數(shù)據(jù),做預(yù)處理(分詞),切分訓(xùn)練集與測試集#載入數(shù)據(jù),做預(yù)處理(分詞),切分訓(xùn)練集與測試集defload_file_and_preprocessing():neg=pd
2、.read_excel(chinese_data/neg.xls,header=None,index=None)pos=pd.read_excel(chinese_data/pos.xls,header=None,index=None)cw=lambdax:list(jieba.cut(x)poswords=pos0.apply(cw)negwords=neg0.apply(cw)#use1forpositivesentiment,0fornegativey=np.concatenate(np.ones(len(pos),np.zeros(len(neg)#訓(xùn)練集:測試集=8:2x_train
3、,x_test,y_train,y_test=train_test_split(np.concatenate(poswords,negwords),y,test_size=0.2)#NumPy提供了多種文件操作函數(shù)方便存取數(shù)組內(nèi)容(npy格式以二進(jìn)制存儲數(shù)據(jù)的)np.save(pre_data/y_train.npy,y_train)np.save(pre_data/y_test.npy,y_test)returnx_train,x_test3、計算訓(xùn)練集和測試集每條評論數(shù)據(jù)的向量并存入文件#對每個句子的所有詞向量取均值,來生成一個句子的/ectordefbuild_sentence_vect
4、or(text,size,w2v_model):vec=np.zeros(size).reshape(1,size)count=0forwordintext:try:vec+=w2v_modelword.reshape(1,size)count+=1exceptKeyError:continueifcount!=0:vec/=countreturnvec#計算詞向量defget_train_vecs(x_train,x_test):n_dim=300#詞向量維度#試用Word2Vec建立詞向量模型w2v_model=Word2Vec(size=n_dim,window=5,sg=0,hs=0,
5、negative=5,min_count=10)w2v_model.build_vocab(x_train)#準(zhǔn)備模型詞匯表#在評論訓(xùn)練集上建模w2v_model.train(x_train,total_examples=w2v_model.corpus_count,epochs=w2v_model.iter)#川練詞向量#訓(xùn)練集評論向量集合train_vecs=np.concatenate(build_sentence_vector(z,n_dim,w2v_model)forzinx_train)np.save(pre_data/train_vecs.npy,train_vecs)#將訓(xùn)練集
6、保存到文件中print(train_vecs.shape)#輸出訓(xùn)練集的維度#在測試集上訓(xùn)練w2v_model.train(x_test,total_examples=w2v_model.corpus_count,epochs=w2v_model.iter)w2v_model.save(pre_data/w2v_model/w2v_model.pkl)test_vecs=np.concatenate(build_sentence_vector(z,n_dim,w2v_model)forzinx_test)np.save(pre_data/test_vecs.npy,test_vecs)prin
7、t(test_vecs.shape)4、獲得訓(xùn)練集向量和標(biāo)簽,測試集向量和標(biāo)簽#獲得訓(xùn)練集向量和標(biāo)簽,測試集向量和標(biāo)簽defget_data():train_vecs=np.1oad(pre_data/train_vecs.npy)y_train=np.load(pre_data/y_train.npy)test_vecs=np.1oad(pre_data/test_vecs.npy)y_test=np.load(pre_data/y_test.npy)returntrain_vecs,y_train,test_vecs,y_test5、訓(xùn)練SVM模型#訓(xùn)練SVM模型defsvm_train(
8、train_vecs,y_train,test_vecs,y_test):c1f=SVC(kerne1=rbf,verbose=True)c1f.fit(train_vecs,y_train)#艮據(jù)給定的訓(xùn)練數(shù)據(jù)擬合SVM模型job1ib.dump(c1f,pre_data/svm_mode1/mode1.pk1)#保存訓(xùn)練好的SVM模型print(c1f.score(test_vecs,y_test)#輸出測試數(shù)據(jù)的平均準(zhǔn)確度6、構(gòu)建待遇測句子的向量#構(gòu)建待遇測句子的向量defget_predict_vecs(words):n_dim=300w2v_mode1=Word2Vec.load(p
9、re_data/w2v_mode1/w2v_mode1.pk1)train_vecs=bui1d_sentence_vector(words,n_dim,w2v_mode1)returntrain_vecs7、對單個句子進(jìn)行情感判斷#對單個句子進(jìn)行情感判斷defsvm_predict(string):words=jieba.lcut(string)words_vecs=get_predict_vecs(words)clf=joblib.load(pre_data/svm_model/model.pkl)result=clf.predict(words_vecs)ifint(resultO)=1:print(string,positive)else:print(string,negative)if_name_=_main_:#x_train,x_test=loadile_and_preprocessing()#get_train_vecs(x_train,x_test)#train_vecs,y_train,test_vecs,y_test=get_data()#svm_train(train_vecs,y_train,test_v
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國波紋管帶數(shù)據(jù)監(jiān)測研究報告
- 現(xiàn)代家居裝飾的綠色環(huán)保理念
- 2024年銅仁市人民醫(yī)院同一后勤公司招聘招聘理貨員筆試真題
- 2024年瓊海市考核招聘事業(yè)單位專業(yè)技術(shù)人才考試真題
- 2024年寧波北部綜合保供基地開發(fā)有限公司招聘考試真題
- 2024年河南洛陽偃師區(qū)婦幼保健院招聘筆試真題
- 2024年黑龍江省選調(diào)應(yīng)屆 到基層培養(yǎng)鍛煉工作考試真題
- 技術(shù)合同范本
- 2024年第八師石河子市總醫(yī)院招聘考試真題
- 光學(xué)儀器采購合同
- 駕駛員心理健康與安全駕駛
- 基于強(qiáng)化學(xué)習(xí)的特征選擇技術(shù)
- 隨車起重機(jī)吊裝施工方案
- 《市場營銷》課程標(biāo)準(zhǔn)
- 無違法犯罪記錄證明申請表(個人)
- 聲樂第2版(學(xué)前教育專業(yè))PPT完整全套教學(xué)課件
- 蘇科版六年級下冊《勞動》全一冊全部公開課PPT課件(共9課)
- 小學(xué)英語外研版(三起點(diǎn))四年級下冊全冊課文翻譯(1-10模塊)
- WS 400-2023 血液運(yùn)輸標(biāo)準(zhǔn)
- 銀行業(yè)金融機(jī)構(gòu)監(jiān)管數(shù)據(jù)標(biāo)準(zhǔn)化規(guī)范(2021版)數(shù)據(jù)結(jié)構(gòu)一覽表
- 教育戲?。簩?shí)踐指南與課程計劃
評論
0/150
提交評論