首页 \ 问答 \ Numpy [...,无](Numpy […,None])

Numpy [...,无](Numpy […,None])

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

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

建立

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

从统计学习的要素

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

它看起来像这样:

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

我认为我知道的

当我将[...,None]与我离开时相比时,我看到输出的差异。 这里是:

在此处输入图像描述

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

主要问题

虽然我看到[...,None]的输出给了我想要的东西,但我很难找到关于它实际上应该做什么的任何信息。 任何人都可以告诉我这个代码实际意味着什么, None参数是做什么的,等等?

谢谢!


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:

From Elements of Statistical Learning

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:

enter image description here

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!


原文:https://stackoverflow.com/questions/51127657
更新时间:2023-10-21 09:10

最满意答案

哦,伙计,我发现了问题!

在tableViewController的viewDidLoad中我设置了navigationBarStyle,如下所示:

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

删除此行后,一切正常。 实际上它根本不做任何事情,因为无论如何barStyle都采用那种特殊的popover风格。 但是因为我正在为iPhone使用相同的tableViewController,所以我设置了barStyle。

我做了另一个简单的测试项目,并能够重现这个bug并通过删除barStyle设置来解决它。

我将提交一份bug报告......


Oh man, I found the problem!

In the tableViewController's viewDidLoad I set the navigationBarStyle like this:

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

When you remove this line, everything works fine. Actually it doesn't do anything at all, because the barStyle is in that special popover style anyway. But because I'm using the same tableViewController for the iPhone, I set the barStyle.

I made another simple test project and was able to reproduce this bug and solve it by removing the barStyle setting.

I'll file a bugreport ...

相关问答

更多

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)