小编dba*_*ron的帖子

Python - 从文件中获取列迭代器(不读取整个文件)

我的主要目标是从浮动的巨大矩阵计算中位数(按列).例:

a = numpy.array(([1,1,3,2,7],[4,5,8,2,3],[1,6,9,3,2]))

numpy.median(a, axis=0)

Out[38]: array([ 1.,  5.,  8.,  2.,  3.])
Run Code Online (Sandbox Code Playgroud)

矩阵太大而不适合Python内存(~5 TB),所以我将它保存在csv文件中.所以我想遍历每一列并计算中位数.

有没有办法让我在不读取整个文件的情况下获取列迭代器?

关于计算矩阵中值的任何其他想法也会很好.谢谢!

python numpy median

5
推荐指数
1
解决办法
913
查看次数

如何访问Spark RandomForest中的各个预测?

我希望使用pyspark.mllib.tree.RandomForest模块来获取观察的邻近矩阵.

到目前为止,我的数据足够小,可以直接加载到内存中.因此,我使用sklearn.ensemble.RandomForestClassifier以下列方式获取邻近矩阵:假设X是包含要素的矩阵,Y是包含标签的向量.我训练随机森林来区分标签为"0"和标签为"1"的对象.拥有经过训练的随机森林,我想通过计算两个观测结果得到相同的最终节点(=叶子)的决策树数量来获得我的数据集中每对观测值之间的接近程度.因此,对于100个决策树,两个观测值之间的接近度量可以是0(从不落在同一个最终叶子中)和100(在所有决策树中已落到相同的最终叶子).这个python的实现:

import numpy
from sklearn import ensemble

## data
print X.shape, Y.shape # X is a matrix that holds the 4281 features and contains 8562 observations and Y contains 8562 labels
>> (8562, 4281) (8562,)

## train the tree
n_trees = 100
rand_tree = sklearn.ensemble.RandomForestClassifier(n_estimators=n_tress)
rand_tree.fit(X, Y)

## get proximity matrix
apply_mat = rand_tree.apply(X)
obs_num = len(apply_mat)
sim_mat = numpy.eye(obs_num) * len(apply_mat[0]) # max values that they can be similar at = N estimators

for i in xrange(obs_num):
    for …
Run Code Online (Sandbox Code Playgroud)

python random-forest apache-spark pyspark apache-spark-mllib

5
推荐指数
1
解决办法
1757
查看次数