Python 在 numpy 中,[:,None] 的选择有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37867354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In numpy, what does selection by [:,None] do?
提问by Huey
I'm taking the Udacity course on deep learning and I came across the following code:
我正在学习关于深度学习的 Udacity 课程,我遇到了以下代码:
def reformat(dataset, labels):
dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
# Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return dataset, labels
What does labels[:,None]
actually do here?
什么是labels[:,None]
真正在这里做?
采纳答案by hpaulj
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
numpy.newaxis
The newaxis object can be used in all slicing operations to create an axis of length one. :const: newaxis is an alias for ‘None', and ‘None' can be used in place of this with the same result.
numpy.newaxis
newaxis 对象可用于所有切片操作以创建长度为 1 的轴。:const: newaxis 是 'None' 的别名,可以使用 'None' 代替它,结果相同。
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.expand_dims.html
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.expand_dims.html
Demonstrating with part of your code
使用您的部分代码进行演示
In [154]: labels=np.array([1,3,5])
In [155]: labels[:,None]
Out[155]:
array([[1],
[3],
[5]])
In [157]: np.arange(8)==labels[:,None]
Out[157]:
array([[False, True, False, False, False, False, False, False],
[False, False, False, True, False, False, False, False],
[False, False, False, False, False, True, False, False]], dtype=bool)
In [158]: (np.arange(8)==labels[:,None]).astype(int)
Out[158]:
array([[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0]])
回答by GWW
None
is an alias for NP.newaxis. It creates an axis with length 1. This can be useful for matrix multiplcation etc.
None
是NP.newaxis的别名。它创建一个长度为 1 的轴。这对于矩阵乘法等很有用。
>>>> import numpy as NP
>>>> a = NP.arange(1,5)
>>>> print a
[1 2 3 4]
>>>> print a.shape
(4,)
>>>> print a[:,None].shape
(4, 1)
>>>> print a[:,None]
[[1]
[2]
[3]
[4]]
回答by john ktejik
to explain it in plain english, it allows operations between two arrays of different number of dimensions.
用简单的英语解释它,它允许在不同维数的两个数组之间进行操作。
It does this by adding a new, empty dimension which will automagically fit the size of the other array.
它通过添加一个新的空维度来实现这一点,该维度将自动适应另一个数组的大小。
So basically if:
所以基本上如果:
Array1 = shape[100] and Array2 = shape[10,100]
Array1 = shape[100] 和 Array2 = shape[10,100]
Array1 * Array2
will normally give an error.
Array1 * Array2
一般会报错。
Array1[:,None] * Array2
will work.
Array1[:,None] * Array2
将工作。
回答by CodingYourLife
I came here after having the exact same problem doing the same Udacity course. What I wanted to do is transpose the one dimensional numpy series/array which does not work with numpy.transpose([1, 2, 3]). So I wanted to add you can transpose like this (source):
我在完成相同的 Udacity 课程后遇到了完全相同的问题后来到这里。我想要做的是转置不适用于 numpy.transpose([1, 2, 3]) 的一维 numpy 系列/数组。所以我想补充你可以像这样转置(来源):
numpy.matrix([1, 2, 3]).T
It results in:
结果是:
matrix([[1],
[2],
[3]])
which is pretty much identical (type is different) to:
这与以下几乎相同(类型不同):
x=np.array([1, 2, 3])
x[:,None]
But I think it's easier to remember...
但我认为它更容易记住......
回答by Nischal Lal Shrestha
If you see code from experienced NumPy users, you will often see them use a special slicing syntax instead of calling reshape.
如果你看到有经验的 NumPy 用户的代码,你会经常看到他们使用特殊的切片语法而不是调用 reshape。
x = v[None, :]
or
或者
x = v[:, None]
Those lines create a slice that looks at all of the items of v but asks NumPy to add a new dimension of size 1 for the associated axis.
这些行创建了一个切片,查看 v 的所有项目,但要求 NumPy 为关联的轴添加大小为 1 的新维度。