【发布时间】:2018-06-28 00:41:04
【问题描述】:
我有一本格式如下的字典:
{
"sample1": set(["feature1", "feature2", "feature3"]),
"sample2": set(["feature1", "feature4", "feature5"]),
}
我有 20M samples 和 150K 独特功能。
我想把这个转换成csv格式:
sample,feature1,feature2,feature3,feature4,feature5
sample1,1,1,1,0,0
sample2,1,0,0,1,1
到目前为止我做了什么:
ALL_FEATURES = list(set(features))with open("features.csv", "w") as f: f.write("fvecmd5," + ",".join([str(x) for x in ALL_FEATURES]) + "\n") fvecs_lol = list(fvecs.items()) fvecs_keys, fvecs_values = zip(*fvecs_lol) del fvecs_lol tmp = [["1" if feature in featurelist else "0" for feature in ALL_FEATURES] for featurelist in fvecs_values] for i, entry in enumerate(tmp): f.write(fvecs_keys[i] + "," + ",".join(entry) + "\n")
但这运行速度很慢。有更快的方法吗?也许利用 Numpy/Cython?
【问题讨论】:
-
你可以从 tensorflow 或 sklearn 中检查一个热门函数
标签: python python-3.x pandas csv numpy