【发布时间】:2014-03-06 07:45:49
【问题描述】:
我有一个从 csv 文件保存/读取的 DataFrame,我想从中创建一个 Term Density Matrix DataFrame。按照 herrfz 的建议 here,我使用了 sklearn 的 CounVectorizer。我将该代码包装在一个函数中
from sklearn.feature_extraction.text import CountVectorizer
countvec = CountVectorizer()
from scipy.sparse import coo_matrix, csc_matrix, hstack
def df2tdm(df,titleColumn,placementColumn):
'''
Takes in a DataFrame with at least two columns, and returns a dataframe with the term density matrix
of the words appearing in the titleColumn
Inputs: df, a DataFrame containing titleColumn, placementColumn among other columns
Outputs: tdm_df, a DataFrame containing placementColumn and columns with all the words appearrig in df.titleColumn
Credits:
https://stackoverflow.com/questions/22205845/efficient-way-to-create-term-density-matrix-from-pandas-dataframe
'''
tdm_df = pd.DataFrame(countvec.fit_transform(df[titleColumn]).toarray(), columns=countvec.get_feature_names())
tdm_df = tdm_df.join(pd.DataFrame(df[placementColumn]))
return tdm_df
将 TDM 作为 DataFrame 返回,例如:
df = pd.DataFrame({'title':['Delicious boiled egg','Fried egg ', 'Potato salad', 'Split orange','Something else'], 'page':[1, 1, 2, 3, 4]})
print df.head()
tdm_df = df2tdm(df,'title','page')
tdm_df.head()
boiled delicious egg else fried orange potato salad something \
0 1 1 1 0 0 0 0 0 0
1 0 0 1 0 1 0 0 0 0
2 0 0 0 0 0 0 1 1 0
3 0 0 0 0 0 1 0 0 0
4 0 0 0 1 0 0 0 0 1
split page
0 0 1
1 0 1
2 0 2
3 1 3
4 0 4
此实现存在内存扩展不佳的问题:当我使用一个占用 190 kB 并保存为 utf8 的 DataFrame 时,该函数使用 ~200 MB 来创建 TDM 数据帧。当 csv 文件为 600 kB 时,该函数使用 700 MB,而当 csv 为 3.8 MB 时,该函数耗尽了我所有的内存和交换文件(8 GB)并崩溃。
我还使用稀疏矩阵和稀疏数据帧(下)做了一个实现,但是内存使用几乎相同,只是速度要慢得多
def df2tdm_sparse(df,titleColumn,placementColumn):
'''
Takes in a DataFrame with at least two columns, and returns a dataframe with the term density matrix
of the words appearing in the titleColumn. This implementation uses sparse DataFrames.
Inputs: df, a DataFrame containing titleColumn, placementColumn among other columns
Outputs: tdm_df, a DataFrame containing placementColumn and columns with all the words appearrig in df.titleColumn
Credits:
https://stackoverflow.com/questions/22205845/efficient-way-to-create-term-density-matrix-from-pandas-dataframe
https://stackoverflow.com/questions/17818783/populate-a-pandas-sparsedataframe-from-a-scipy-sparse-matrix
https://stackoverflow.com/questions/6844998/is-there-an-efficient-way-of-concatenating-scipy-sparse-matrices
'''
pm = df[[placementColumn]].values
tm = countvec.fit_transform(df[titleColumn])#.toarray()
m = csc_matrix(hstack([pm,tm]))
dfout = pd.SparseDataFrame([ pd.SparseSeries(m[i].toarray().ravel()) for i in np.arange(m.shape[0]) ])
dfout.columns = [placementColumn]+countvec.get_feature_names()
return dfout
关于如何提高内存使用率的任何建议?我想知道这是否与 scikit 的内存问题有关,例如here
【问题讨论】:
-
你的稀疏转换没有做任何事情;你需要先用 nan 表示 0。但更大的问题是为什么你需要一个框架呢? scipy sparse repr 或 scikit-kearn repr 在这里完成工作。 (并且更容易找出内存问题所在)
标签: python memory pandas scikit-learn