2014-07-04 42 views
1

我有以下为什么numpy形状是空的?

(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, %s' %(test_data.shape[0], test_data[1]) 

print 'saving vectorized instances' 
with open(filename, "wb") as f: 
    np.save(f, training) 
    np.save(f, test_data) 
    np.save(f, target) 
    np.save(f, projectids) 

在这个时间点,我的训练的外形仍然(10, 121)

后来,我只是

with open("../data/f1/training.dat", "rb") as f: 
    training = np.load(f) 
    test_data = np.load(f) 
    target = np.load(f) 
    projectids = np.load(f) 

重新初始化4个变量,但形状不见了。

+0

你必须提供更多的上下文。没有足够的信息来推断发生了什么。至少,显示您编写的用于初始化分类器和训练数据的代码。 – lightalchemist

+0

稀疏矩阵不是NumPy数组。他们甚至不认为arraylikes;大多数NumPy例程不知道如何处理一个例程。看看http://stackoverflow.com/questions/8955448/save-load-scipy-sparse-csr-matrix-in-portable-data-format – user2357112

+0

它可能是有用的在这种情况下,'numpy'确实已知如何处理稀疏矩阵 - 将其包装在对象数组中。如果这有所作为,我使用numpy 1.9dev。 – hpaulj

回答

4

有一个在

array(<418326x223957 sparse matrix of type '<type 'numpy.float64'>' 
    with 165657096 stored elements in Compressed Sparse Row format>, dtype=object) 

形状信息这是一个项目,并且0的尺寸,因此,形状()的阵列。那一项是dtype=object。具体来说,它是一个稀疏阵列 - 尺寸显示在屏幕<418...x22...中。

我打算询问DictVectorizerfit_transform,但那并不重要。这是保存和加载操作来更改值。

我的猜测是你没有加载你刚刚写的文件。


np.save(f,training)与D型object包裹在np.array稀疏矩阵。这就是你看到的负载。

training = training.item() 

将稀疏矩阵从数组包装器中取出。

418326x223957training的形状与完整的数据集,和(10, 121)减少调试集的形状?