Numpy [...,None] [英] Numpy [...,None]

查看:67
本文介绍了Numpy [...,None]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己需要向现有的 numpy 数组添加功能,这导致了以下代码的最后一部分实际上在做什么的问题:

 np.ones(shape=feature_set.shape)[...,None]

设置

举个例子,假设我希望通过使用 numpy 和求解来求解线性回归参数估计:

假设我有一个形状为 (50,1) 的特征集,一个形状为 (50,) 的目标变量,并且我希望使用目标变量的形状为截距值添加一列.

它看起来像这样:

# 创建随机目标 &功能集y_train = np.random.randint(0,100, size = (50,))特征集 = np.random.randint(0,100,size=(50,1))# 在目标变量的形状之后构建一组 1sint_train = np.ones(shape=y_train.shape)[...,None]# 然后可以将 int_train 添加到特征集X = np.concatenate((int_train, feature_set),1)

我所知道的

当我包含 [...,None] 时,我看到输出的差异与我不使用它时的差异.这是:

第二个版本围绕需要相同维数的输入数组返回错误,最终我偶然发现了使用 [...,None] 的解决方案.

主要问题

虽然我看到 [...,None] 的输出给了我我想要的东西,但我正在努力寻找关于什么它实际上应该做什么的任何信息.任何人都可以带我了解这段代码的实际含义,None 参数在做什么,等等?

谢谢!

解决方案

[..., None] 的切片由两个快捷方式"组成:

省略号文字组件:

<块引用>

点 (...) 代表生成完整索引元组所需的尽可能多的冒号.例如,如果 x 是一个秩为 5 的数组(即它有 5 个轴),则

  • x[1,2,...] 等价于 x[1,2,:,:,:],
  • x[...,3]x[:,:,:,:,3]
  • x[4,...,5,:]x[4,:,:,5,:].

(来源)

None 组件:

<块引用>

numpy.newaxis

newaxis 对象可用于所有切片操作以创建长度为 1 的轴.newaxis 是None"的别名,可以使用None"代替它,结果相同.

(来源)

因此,arr[..., None] 接受一个维度为 N 的数组并添加"最后"的维度对于维度 N+1 的结果数组.

示例:

将 numpy 导入为 npx = np.array([[1,2,3],[4,5,6]])打印(x.shape)#(2, 3)y = x[...,无]打印(y.shape) # (2, 3, 1)z = x[:,:,np.newaxis]打印(z.shape)#(2, 3, 1)a = np.expand_dims(x,axis=-1)打印(a.shape) # (2, 3, 1)打印((y == z).all()) #真print((y == a).all()) # 真

I have found myself needing to add features to existing numpy arrays which has led to a question around what the last portion of the following code is actually doing:

   np.ones(shape=feature_set.shape)[...,None]

Set-up

As an example, let's say I wish to solve for linear regression parameter estimates by using numpy and solving:

Assume I have a feature set shape (50,1), a target variable of shape (50,), and I wish to use the shape of my target variable to add a column for intercept values.

It would look something like this:

# Create random target & feature set
y_train = np.random.randint(0,100, size = (50,))
feature_set = np.random.randint(0,100,size=(50,1))

# Build a set of 1s after shape of target variable
int_train = np.ones(shape=y_train.shape)[...,None]

# Able to then add int_train to feature set 
X = np.concatenate((int_train, feature_set),1)

What I Think I Know

I see the difference in output when I include [...,None] vs when I leave it off. Here it is:

The second version returns an error around input arrays needing the same number of dimensions, and eventually I stumbled on the solution to use [...,None].

Main Question

While I see the output of [...,None] gives me what I want, I am struggling to find any information on what it is actually supposed to do. Can anybody walk me through what this code actually means, what the None argument is doing, etc?

Thank you!

解决方案

The slice of [..., None] consists of two "shortcuts":

The ellipsis literal component:

The dots (...) represent as many colons as needed to produce a complete indexing tuple. For example, if x is a rank 5 array (i.e., it has 5 axes), then

  • x[1,2,...] is equivalent to x[1,2,:,:,:],
  • x[...,3] to x[:,:,:,:,3] and
  • x[4,...,5,:] to x[4,:,:,5,:].

(Source)

The None component:

numpy.newaxis

The newaxis object can be used in all slicing operations to create an axis of length one. newaxis is an alias for ‘None’, and ‘None’ can be used in place of this with the same result.

(Source)

So, arr[..., None] takes an array of dimension N and "adds" a dimension "at the end" for a resulting array of dimension N+1.

Example:

import numpy as np

x = np.array([[1,2,3],[4,5,6]])
print(x.shape)          # (2, 3)

y = x[...,None]
print(y.shape)          # (2, 3, 1)

z = x[:,:,np.newaxis]
print(z.shape)          # (2, 3, 1)

a = np.expand_dims(x, axis=-1)
print(a.shape)          # (2, 3, 1)

print((y == z).all())   # True
print((y == a).all())   # True

这篇关于Numpy [...,None]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆