機器學習之路: python 實踐 word2vec 詞向量技術

來源:互聯網
上載者:User

標籤:一個   定製   return   連網   加速   nbsp   學習   ase   函數   

 

git: https://github.com/linyi0604/MachineLearning

詞向量技術 Word2Vec      每個連續詞彙片段都會對後面有一定製約 稱為上下文context          找到句子之間語義層面的聯絡

 

 1 from sklearn.datasets import fetch_20newsgroups 2 from bs4 import BeautifulSoup 3 import nltk, re 4 from gensim.models import word2vec 5  6 # nltk.download(‘punkt‘) 7  8  9 ‘‘‘10 詞向量技術 Word2Vec 11     每個連續詞彙片段都會對後面有一定製約 稱為上下文context12     13     找到句子之間語義層面的聯絡14     15 ‘‘‘16 17 # 連網下載新聞資料18 news = fetch_20newsgroups(subset="all")19 x, y = news.data, news.target20 21 # 定義一個函數 將每條新聞中的句子分離,並返回一個句子的列表22 def news_to_sentences(news):23     news_text = BeautifulSoup(news).get_text()24     tokenizer = nltk.data.load("tokenizers/punkt/english.pickle")25     raw_sentences = tokenizer.tokenize(news_text)26     sentences = []27     for sent in raw_sentences:28         temp = re.sub("[^a-zA-Z]", " ", sent.lower().strip()).split()29         sentences.append(temp)30 31     return sentences32 33 # 將長新聞中的句子剝離出來用於訓練34 sentences = []35 for i in x:36     sentence_list = news_to_sentences(i)37     sentences += sentence_list38 39 40 # 配置詞向量的維度41 num_features = 30042 # 保證被考慮的詞彙的頻度43 min_word_count = 2044 # 並行計算使用cpu核心數量45 num_workers = 246 # 定義訓練詞向量的上下文視窗大小47 context = 548 downsapling = 1e-349 50 # 訓練詞向量模型51 model = word2vec.Word2Vec(sentences,52                           workers=num_workers,53                           size=num_features,54                           min_count=min_word_count,55                           window=context,56                           sample=downsapling)57 # 這個設定代表當前訓練好的詞向量為最終版, 也可以加速模型訓練的速度58 model.init_sims(replace=True)59 60 # 利用訓練好的模型 尋找文本中與college相關的十個詞彙61 print(model.most_similar("college"))62 ‘‘‘63 [(‘wisconsin‘, 0.7664438486099243), 64 (‘osteopathic‘, 0.7474539279937744), 65 (‘madison‘, 0.7433826923370361), 66 (‘univ‘, 0.7296794652938843), 67 (‘melbourne‘, 0.7212647199630737), 68 (‘walla‘, 0.7068545818328857), 69 (‘maryland‘, 0.7038443088531494), 70 (‘carnegie‘, 0.7038302421569824), 71 (‘institute‘, 0.7003713846206665), 72 (‘informatics‘, 0.6968873143196106)]73 ‘‘‘

 

機器學習之路: python 實踐 word2vec 詞向量技術

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.