我有一个csv文件,其中包含大约3000个用户(行)的56124项(列)的用户评级。额定值是小于128的整数。我有这个功能:
def sparse_to_npz(file, npz):
print("Reading " + file + " ...")
data_items = pd.read_csv(file)
# Create a new dataframe without the user ids.
data_items = data_items.drop('u', 1)
# As a first step we normalize the user vectors to unit vectors.
# magnitude = sqrt(x2 + y2 + z2 + ...)
magnitude = np.sqrt(np.square(data_items).sum(axis=1))
# unitvector = (x / magnitude, y / magnitude, z / magnitude, ...)
data_items = data_items.divide(magnitude, axis='index')
del magnitude
print("Saving to " + npz)
data_sparse = sparse.csr_matrix(data_items)
del data_items
sparse.save_npz(npz, data_sparse)
#np.save("columns", data_items.columns.values)
其中传递了两个文件:输入csv文件(稀疏,每个用户都有所有项目的评分),并应输出npz文件以节省内存。在使用pandas读取文件并将其存储在data_items
之后,我们需要计算震级并将data_items
除以它,最后保存npz文件。问题是我在计算mag的步骤中得到了错误。在内存为12gb的机器上使用np.sqrt(np.square(...
。我该怎么做?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐