【问题标题】:why is numpy shape empty?为什么numpy的形状是空的?
【发布时间】:2014-08-25 07:09:39
【问题描述】:

我有以下

(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)

稍后,我只是通过

重新初始化 4 个变量
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)

但形状不见了。

【问题讨论】:

  • 您必须提供更多上下文。没有足够的信息来推断发生了什么。至少,展示你为初始化分类器和训练数据而编写的代码。
  • 稀疏矩阵不是 NumPy 数组。他们甚至不被认为是类似数组的。大多数 NumPy 例程都不知道如何处理一个。看看stackoverflow.com/questions/8955448/… 可能会有用
  • 在这种情况下,numpy 确实知道如何处理sparse 矩阵 - 将其包装在对象数组中。如果有区别,我正在使用 numpy 1.9dev。
  • user2357112 是正确的。我已经修改并使用了泡菜

标签: python numpy pandas scikit-learn


【解决方案1】:

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

这是一个包含一项的数组,0 维,因此形状为()。那一项是dtype=object。具体来说,它是一个稀疏数组 - 尺寸显示在 &lt;418...x22... 中。

我想问一下DictVectorizerfit_transform,但这没关系。改变值的是保存和加载操作。

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


您的 np.save(f,training) 将稀疏矩阵包装在具有 dtype objectnp.array 中。这就是您在加载时看到的内容。

training = training.item()

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

418326x223957 是具有完整数据集的training 的形状,而(10, 121) 是精简调试集的形状吗?

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2020-07-28
    • 2018-05-13
    • 2017-09-02
    相关资源
    最近更新 更多