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:
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!
原文:https://stackoverflow.com/questions/51127657
最满意答案
哦,伙计,我发现了问题!
在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 ...
相关问答
更多-
我用一些技巧修复它=) [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:@{UITextAttributeFont: [UIFont systemFontOfSize:0.1]} forSt ...
-
testflightapp和iOS 5.1(testflightapp and iOS 5.1)[2022-09-06]
安装iOS 5.1后,我突然遇到了一些类似的问题。 偶尔Mobile Safari的缓存和cookie将被破坏。 清除它们应该可以解决问题。 After installing iOS 5.1 I suddenly had similar problems with some of our testers. Occasionally Mobile Safari's cache and cookies will become corrupted. Clearing them should resolve the ... -
我实现了一个构建在UINavigationController之上的。 为了显示navigationController后面的菜单,您需要将菜单添加到应用程序的主窗口,而不是将其添加为导航控制器的子视图。 您似乎正在移动导航控制器内的viewController,而您需要移动导航控制器的该视图,这样可以移动包括导航栏在内的所有内容。 https://github.com/aryaxt/iOS-Slide-Menu I implemented one which is built on top of a UI ...
-
尝试按顶部工具栏的数量减去框的大小。 当使用self.frame.size.height时,它将使用整个高度作为视图上的位置,因此当通过工具栏/导航栏进行减去时,它会将其提升到足以使整个框适合视图 //clearly not objc but its just to show you how to implement it boxLocation at (self.frame.size.height - (amount of toolbar/nav bar)) 希望这可以帮助! try subtr ...
-
最后我找到了解决方案, 我认为问题可能是由于拆分视图在我们尝试在其上呈现视图控制器时无法关闭根视图控制器。 我改变了上面这个功能 -(void)displayFileInReader:(NSURL *)fileURL { SPDocumentReader *objiPadDocumentReader = [[SPDocumentReader alloc] init]; objiPadDocumentReader.readerType = ReaderTypeLocalCachedDocume ...
-
问题结果是contentSizeForViewInPopover 。 我们的UINavigationController子类实现了这个方法来告诉包含UIPopoverController应该有多大。 在7之前的所有版本的iOS中都记录了绝对工作。它不仅在7中被弃用,它还会导致上述问题。 解决方案是在iOS 7中返回popoverController.contentViewController.view.bounds.size并重新执行所有视图,使它们适合弹出窗口,而不是调整窗口大小来处理视图。 另一个解决方 ...
-
您正在推送的视图控制器具有nil背景颜色,默认情况下在iOS 7上清除。只需将其设置为白色或其他任何颜色即可。 The view controller you're pushing has an nil background colour, which defaults to clear on iOS 7. Just set it to white or whatever and it'll work.
-
哦,伙计,我发现了问题! 在tableViewController的viewDidLoad中我设置了navigationBarStyle,如下所示: [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent]; 删除此行后,一切正常。 实际上它根本不做任何事情,因为无论如何barStyle都采用那种特殊的popover风格。 但是因为我正在为iPhone使用相同的tableViewController,所 ...
-
也许在你的情况下有所帮助:在AIR 3.5中处理视频时我经常崩溃。 将呈现模式设置为direct可帮助解决AIR运行时的此错误。 你试过吗? Maybe helpful in your situation either: I had constant crashes when working with video in AIR 3.5. Setting the render mode to direct helped to workaround this bug of the AIR runtime. Ha ...
-
您根本不需要下载SDK 5.1。 SDK 6.0包括SDK 5.1包含的每个库和标头,以及iOS6添加的库和标头。 请务必了解SDK版本,应用程序兼容的iOS版本以及构建应用程序的体系结构之间的区别。 使用SDK 6.0进行编译并不意味着您的应用只能在iOS6上运行; 这意味着您的应用程序将能够在iOS上从您在IOS_DEPLOYMENT_TARGET设置的版本运行到与SDK对应的版本,即6.0。 因此,如果您希望您的应用程序与iOS 5.1及更高版本兼容,您仍然可以使用Base SDK 6.0进行编译,而 ...