This article mainly introduces the use of the gensim library word2vec in Python. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
pip install gensim After installing the library , you can import and use:
1. Training model definition
##
from gensim.models import Word2Vec model = Word2Vec(sentences, sg=1, size=100, window=5, min_count=5, negative=3, sample=0.001, hs=1, workers=4)
Parameter explanation:
1.sg=1 is the skip-gram algorithm, which is sensitive to low-frequency words; the default sg=0 is the CBOW algorithm. 2.size is the dimension of the output word vector. If the value is too small, the word mapping will affect the results due to conflicts. If the value is too large, it will consume memory and slow down the algorithm calculation. Generally, the value is 100 to between 200. 3.window is the maximum distance between the current word and the target word in the sentence. 3 means looking at 3-b words before the target word and b words after it (b is random between 0-3 ). 4.min_count is used to filter words. Words with a frequency less than min-count will be ignored. The default value is 5. 5. Negative and sample can be fine-tuned based on the training results. Sample indicates that higher frequency words are randomly downsampled to the set threshold. The default value is 1e-3. 6.hs=1 means hierarchical softmax will be used. By default hs=0 and negative is not 0, negative sampling will be selected. 7. Workers control the parallelism of training. This parameter is only valid after Cpython is installed, otherwise only a single core can be used.For detailed parameter description, please view the word2vec source code.
2. Saving and loading the model after training
model.save(fname) model = Word2Vec.load(fname)
3. Model use (word similarity calculation, etc.)
model.most_similar(positive=['woman', 'king'], negative=['man']) #输出[('queen', 0.50882536), ...] model.doesnt_match("breakfast cereal dinner lunch".split()) #输出'cereal' model.similarity('woman', 'man') #输出0.73723527 model['computer'] # raw numpy vector of a word #输出array([-0.00449447, -0.00310097, 0.02421786, ...], dtype=float32)Other contents will not be described in detail, please refer to the details The official description of gensim's word2vec is very detailed.
The above is the detailed content of Use of gensim library word2vec in Python. For more information, please follow other related articles on the PHP Chinese website!

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),