【发布时间】:2020-10-22 21:23:25
【问题描述】:
我正在尝试使用 word2vec 和 tfidf-score 对包含 1,600 万条推文的数据集进行一项基本的推文情绪分析,但我的 6 GB Gforce-Nvidia 未能这样做。因为这是我第一个与机器学习相关的实践项目,所以我想知道我做错了什么,因为数据集都是文本,它不应该占用这么多内存,这会使我的笔记本电脑在 tweet2vec 函数中冻结或在缩放部分出现内存错误。下面是我的代码的一部分,一切都崩溃了。 最后一件事是我尝试了多达 1M 的数据并且它有效!所以我很好奇是什么导致了这个问题
# --------------- calculating word weight for using later in word2vec model & bringing words together ---------------
def word_weight(data):
vectorizer = TfidfVectorizer(sublinear_tf=True, use_idf=True)
d = dict()
for index in tqdm(data, total=len(data), desc='Assigning weight to words'):
# --------- try except caches the empty indexes ----------
try:
matrix = vectorizer.fit_transform([w for w in index])
tfidf = dict(zip(vectorizer.get_feature_names(), vectorizer.idf_))
d.update(tfidf)
except ValueError:
continue
print("every word has weight now\n"
"--------------------------------------")
return d
# ------------------- bringing tokens with weight to recreate tweets ----------------
def tweet2vec(tokens, size, tfidf):
count = 0
for index in tqdm(tokens, total=len(tokens), desc='creating sentence vectors'):
# ---------- size is the dimension of word2vec model (200) ---------------
vec = np.zeros(size)
for word in index:
try:
vec += model[word] * tfidf[word]
except KeyError:
continue
tokens[count] = vec.tolist()
count += 1
print("tweet vectors are ready for scaling for ML algorithm\n"
"-------------------------------------------------")
return tokens
dataset = read_dataset('training.csv', ['target', 't_id', 'created_at', 'query', 'user', 'text'])
dataset = delete_unwanted_col(dataset, ['t_id', 'created_at', 'query', 'user'])
dataset_token = [pre_process(t) for t in tqdm(map(lambda t: t, dataset['text']),
desc='cleaning text', total=len(dataset['text']))]
print('pre_process completed, list of tweet tokens is returned\n'
'--------------------------------------------------------')
X = np.array(tweet2vec(dataset_token, 200, word_weight(dataset_token)))
print('scaling vectors ...')
X_scaled = scale(X)
print('features scaled!')
给 word_weight 函数的数据是一个 (1599999, 200) 形状的列表,每个索引由预处理的推文标记组成。 感谢您的时间和提前回答,当然我很高兴听到处理大型数据集的更好方法
【问题讨论】:
标签: python machine-learning bigdata sentiment-analysis