This is the record of the 83rd "Measurement of word / context frequency" of Language processing 100 knock 2015. It takes time (about 7 minutes) because it is a process for a file of about 800 MB. I thought that a memory error would occur if I read it all at once, and when I tried to survive using the chunksize option of pandas, I had a hard time because I could not do it at all. After all, it could be read all at once and there was no particular problem.
Link | Remarks |
---|---|
083.Word / context frequency measurement.ipynb | Answer program GitHub link |
100 amateur language processing knocks:83 | I am always indebted to you by knocking 100 language processing |
100 language processing knock 2015 version(83,84) | I referred to it in Chapter 9. |
How to use Pandas groupby | Easy to understand how to use pandas group by |
to_pickle function | to_pickle functionの公式ヘルプ |
type | version | Contents |
---|---|---|
OS | Ubuntu18.04.01 LTS | It is running virtually |
pyenv | 1.2.15 | I use pyenv because I sometimes use multiple Python environments |
Python | 3.6.9 | python3 on pyenv.6.I'm using 9 3.7 or 3.There is no deep reason not to use 8 series Packages are managed using venv |
In the above environment, I am using the following additional Python packages. Just install with regular pip.
type | version |
---|---|
pandas | 0.25.3 |
enwiki-20150112-400-r10-105752.txt.bz2 Is the text of 105,752 articles randomly sampled 1/10 from the English Wikipedia articles as of January 12, 2015, which consist of more than 400 words, compressed in bzip2 format. is there. Using this text as a corpus, I want to learn a vector (distributed expression) that expresses the meaning of a word. In the first half of Chapter 9, principal component analysis is applied to the word context co-occurrence matrix created from the corpus, and the process of learning word vectors is implemented by dividing it into several processes. In the latter half of Chapter 9, the word vector (300 dimensions) obtained by learning is used to calculate the similarity of words and perform analogy.
Note that if problem 83 is implemented obediently, a large amount (about 7GB) of main memory is required. If you run out of memory, devise a process or 1/100 sampling corpus enwiki-20150112-400-r100-10576.txt.bz2 Use /nlp100/data/enwiki-20150112-400-r100-10576.txt.bz2).
This time * "1/100 sampling corpus [enwiki-20150112-400-r100-10576.txt.bz2](http://www.cl.ecei.tohoku.ac.jp/nlp100/data/enwiki-20150112-" 400-r100-10576.txt.bz2) ”* is used.
Use the output of> 82 to find the following appearance distribution and constants.
-$ f (t, c) $: Co-occurrence of the word $ t $ and the context word $ c $ -$ f (t, ∗) $: Number of occurrences of word $ t $ -$ f (∗, c) $: Number of occurrences of context word $ c $ -$ N $: Total number of occurrences of word / context word pairs
"Word $ t $" is [previous article](https://qiita.com/FukuharaYohei/items/64e20ced30cba76383dc#%E6%96%87%E8%84%88%E8%AA%9E%E3%81% It is ** "Target Word" ** written in A8% E3% 81% AF). Suppose the output of 82 is as follows.
t(Target word) | c(Contextual words) |
---|---|
t1 | c1 |
t1 | c2 |
t2 | c1 |
t1 | c1 |
Then, the following is output. So-called SQL Group By.
** $ f (t, c) $: Co-occurrence of word $ t $ and contextual word $ c $ **
t(Target word) | c(Contextual words) | Number of co-occurrence |
---|---|---|
t1 | c1 | 2 |
t1 | c2 | 1 |
t2 | c1 | 1 |
** $ f (t, ∗) $: Number of occurrences of word $ t $ **
t(Target word) | Number of appearances |
---|---|
t1 | 3 |
t2 | 1 |
** $ f (∗, c) $: Number of occurrences of context word $ c $ **
c(Contextual words) | Number of appearances |
---|---|
c1 | 3 |
c2 | 1 |
** $ N $: Total number of occurrences of word / context word pairs ** 4 (because the whole is 4 lines)
import sys
import pandas as pd
df = pd.read_table('./082.context.txt', header=None, names=['t', 'c'])
print(df.info())
def to_pickle_file(grouped, path):
print('length:', grouped.size)
grouped.to_pickle(path)
to_pickle_file(df.groupby(['t','c'])['c'].agg('count'), './083_group_tc.zip')
to_pickle_file(df.groupby('t')['c'].agg('count'), './083_group_t.zip')
to_pickle_file(df.groupby('c')['c'].agg('count'), './083_group_c.zip')
I'm using pandas to load a c
file with the column name t
.
df = pd.read_table('./082.context.txt', header=None, names=['t', 'c'])
print(df.info())
As a result of df.info ()
, the following is output, and you can see that the result of * "$ N $: total number of occurrences of word / context word pairs" * is 6800317. You can also see that it uses about 1GB of memory. By the way, it took about 1.5 minutes for the read part.
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 68000317 entries, 0 to 68000316
Data columns (total 2 columns):
t object
c object
dtypes: object(2)
memory usage: 1.0+ GB
None
Here, the count of the groupby
result of pandas is saved by pickle
. If you set the file extension to zip, it will be compressed automatically, which is convenient. If you load the file saved here, you can restore it as the same pandas Series object as when you saved it.
def to_pickle_file(grouped, path):
print('length:', grouped.size)
grouped.to_pickle(path)
This is the main part of this time. Grouping is done using groupby
of pandas and the result is counted.
to_pickle_file(df.groupby(['t','c'])['c'].agg('count'), './083_group_tc.zip')
to_pickle_file(df.groupby('t')['c'].agg('count'), './083_group_t.zip')
to_pickle_file(df.groupby('c')['c'].agg('count'), './083_group_c.zip')
By the way, the following is the information of each process.
Number of lines | processing time | file size | |
---|---|---|---|
$ f(t,c) $ | 21,327,Line 945 | 4min 38s | 103.7MB |
$ f(t,*) $ | 388,Line 836 | 34.7s | 2.8MB |
$ f(*,c) $ | 388,Line 836 | 24.2s | 2.8MB |
Since the target file size is large (about 800MB), trial and error was very difficult. So, at first, I created and coded a file that covered only the first 100,000 lines.
cat 082.context.txt | head -n 100000 >> 082.context_mini.txt
When I got an error on line XX in DataFrame
, I combined head
and tail
to see the contents of the file. Normally, I simply open the file, but in the case of a large file, it takes time to open it, so I did this.
The following command displays lines 12198 to 3 of the file. By the way, this error is a failure related to tokenization of the sentence written in "Spilling story" of the previous article.
$ cat 082.context.txt | head -n 124150 | tail -n 3
"b")("s" "c
− "b")("s"
− "c
Recommended Posts