gpt4 book ai didi

python - 使用 scikit linear_kernel 进行 TF-IDF 计算时结果太大

转载 作者:行者123 更新时间:2023-12-04 10:27:36 25 4
gpt4 key购买 nike

我是 Python 新手,目前正在尝试开发基于内容的推荐系统。我正在读取一个包含大约 60,000 行的 csv 文件,并使用 TfidfVectorizer.fit_transform 生成矩阵大小 (63098, 9081),然后应用 linear_kernel,然后崩溃并返回错误 RuntimeError: nnz of the result is too large。

tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=100, stop_words='english')
tfidf_matrix = tf.fit_transform(ds['description'])
result = linear_kernel(tfidf_matrix, tfidf_matrix)

日志显示假设的结果大小为 20gb。有没有办法对数据进行分块以及如何将其应用到我当前的结构中?提前致谢。

最佳答案

这是因为您使用数据中的所有唯一词来创建 TfidfVectorizer您的矩阵的形状将是 No. of documents * No. of unique words ,您系统的当前内存可能不支持此大小,为了避免内存错误,您可以使用 max_features 限制矩阵大小。 docs中提到的参数
5000的尺寸或 10000通常使用,但你可以尝试不同的值,看看什么适合你。

关于python - 使用 scikit linear_kernel 进行 TF-IDF 计算时结果太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60565605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com