我正在尝试加载我之前保存的稀疏数组.保存稀疏数组很容易.试图阅读它是一种痛苦.scipy.load在我的稀疏数组周围返回一个0d数组.
import scipy as sp
A = sp.load("my_array"); A
array(<325729x325729 sparse matrix of type '<type 'numpy.int8'>'
with 1497134 stored elements in Compressed Sparse Row format>, dtype=object)
为了获得稀疏矩阵,我必须展平0d数组,或者使用sp.asarray(A).这似乎是一种非常难以做到的事情.Scipy是否足够聪明才能理解它已经加载了一个稀疏数组?有没有更好的方法来加载稀疏数组?
我在文本文件中有一个1.2GB的边缘列表.我的ubuntu PC有8GB的RAM.输入中的每一行都是如此
287111206 357850135
我想将其转换为稀疏邻接矩阵并将其输出到文件.
我的数据的一些统计数据:
Number of edges: around 62500000
Number of vertices: around 31250000
我之前在/sf/answers/2706735111/问了很多相同的问题并得到了很好的答案.问题是我无法让它发挥作用.
我首先尝试使用np.loadtxt加载文件,但它非常慢并且使用了大量内存.所以相反我转移到pandas.read_csv这是非常快,但这导致它自己的问题.这是我目前的代码:
import pandas
import numpy as np
from scipy import sparse
data = pandas.read_csv("edges.txt", sep=" ", header= None, dtype=np.uint32)
A = data.as_matrix()
print type(A)
k1,k2,k3=np.unique(A,return_inverse=True,return_index=True)
rows,cols=k3.reshape(A.shape).T
M=sparse.coo_matrix((np.ones(rows.shape,int),(rows,cols)))
print type(M)
问题是pandas数据框data很大,我在A中有效地复制了一个低效的副本.然而,随着代码崩溃,事情变得更糟
<type 'instancemethod'>
Traceback (most recent call last):
  File "make-sparse-matrix.py", line 13, in <module>
    rows,cols=k3.reshape(A.shape).T
AttributeError: 'function' object has no attribute 'shape'
raph@raph-desktop:~/python$ python make-sparse-matrix.py 
<type 'numpy.ndarray'>
Traceback (most recent …倾倒大矩阵(170000*20000)如下
 cPickle.dump(train_set,gzip.open('train.pickle.gz','wb'), cPickle.HIGHEST_PROTOCOL)
我收到以下错误:
SystemError: error return without exception set
在这种情况下我该如何处理?
我正在将numpy稀疏数组(已删除)保存到csv中.结果是我有一个3GB的csv.问题是95%的细胞是0.0000.我用过fmt='%5.4f'.如何格式化和保存,使零保存为0,非零浮点数以'%5.4f'格式保存?如果我能做到这一点,我相信我可以将3GB降至300MB.
我在用
np.savetxt('foo.csv', arrayDense, fmt='%5.4f', delimiter = ',')
感谢和问候
我想对一些打包为 a 的数据运行sklearn's RandomForestClassifier,而这些数据numpy.ndarray恰好是稀疏的。打电话fit给ValueError: setting an array element with a sequence.。从其他帖子我了解到随机森林无法处理稀疏数据。
我希望该对象有一个todense方法,但它没有。
>>> X_train
array(<1443899x1936774 sparse matrix of type '<class 'numpy.float64'>'
    with 141256894 stored elements in Compressed Sparse Row format>,
      dtype=object)
>>> type(X_train)
<class 'numpy.ndarray'>
我尝试用 SciPy 包装它,csr_matrix但这也会产生错误。
有没有办法让随机森林接受这些数据?(不确定密集实际上是否适合内存,但那是另一回事......)
编辑 1
产生错误的代码是这样的:
X_train = np.load('train.npy') # this returns a ndarray
train_gt = pd.read_csv('train_gt.csv')
model = RandomForestClassifier()
model.fit(X_train, train_gt.target)
至于使用的建议toarray(),ndarray 没有这样的方法。
AttributeError: 'numpy.ndarray' object has no …
我有以下内容
(Pdb) training
array(<418326x223957 sparse matrix of type '<type 'numpy.float64'>'
    with 165657096 stored elements in Compressed Sparse Row format>, dtype=object)
(Pdb) training.shape
()
为什么没有形状信息?
编辑:这就是我所做的:
training, target, test, projectids = generate_features(outcomes, projects, resources)
target = np.array([1. if i == 't' else 0. for i in target])
projectids = np.array([i for i in projectids])
print 'vectorizing training features'
d = DictVectorizer(sparse=True)
training = d.fit_transform(training[:10].T.to_dict().values())
#test_data = d.fit_transform(training.T.to_dict().values())
test_data = d.transform(test[:10].T.to_dict().values())
print 'training shape: %s, %s' %(training.shape[0], training[1])
print 'test shape: %s, …python ×5
numpy ×4
pandas ×2
scikit-learn ×2
scipy ×2
gzip ×1
optimization ×1
pickle ×1
sparse-array ×1
system-error ×1
ubuntu ×1