这不像好奇心那么严重。
在我的 64 位 linux 解释器中我可以执行
In [10]: np.int64 == np.int64
Out[10]: True
In [11]: np.int64 is np.int64
Out[11]: True
太好了,正是我所期望的。 但是我发现了 numpy.core.numeric 模块的这个奇怪的属性
In [19]: from numpy.core.numeric import _typelessdata
In [20]: _typelessdata
Out[20]: [numpy.int64, numpy.float64, numpy.complex128, numpy.int64]
奇怪为什么 numpy.int64 在那里两次?让我们调查一下。
In [23]: _typelessdata[0] is _typelessdata[-1]
Out[23]: False
In [24]: _typelessdata[0] == _typelessdata[-1]
Out[24]: False
In [25]: id(_typelessdata[-1])
Out[25]: 139990931572128
In [26]: id(_typelessdata[0])
Out[26]: 139990931572544
In [27]: _typelessdata[-1]
Out[27]: numpy.int64
In [28]: _typelessdata[0]
Out[28]: numpy.int64
哇,他们不一样。这里发生了什么?为什么有两个 np.int64?
最佳答案
Here是 _typelessdata 在 numeric.py 中构造的行:
_typelessdata = [int_, float_, complex_]
if issubclass(intc, int):
_typelessdata.append(intc)
if issubclass(longlong, int):
_typelessdata.append(longlong)
intc 是 C 兼容(32 位)有符号整数,int 是原生 Python
整数,可以是 32 位或 64 位,具体取决于平台。
在 32 位系统上,原生 Python int 类型也是 32 位,所以
issubclass(intc, int) 返回 True 并且 intc 被附加到 _typelessdata,
最终看起来像这样:
[numpy.int32, numpy.float64, numpy.complex128, numpy.int32]
请注意,_typelessdata[-1] 是 numpy.intc,而不是 numpy.int32。
在 64 位系统上,int 是 64 位的,因此 issubclass(longlong, int) 返回 True 和一个 longlong 被附加到 _typelessdata,导致:
[numpy.int64, numpy.float64, numpy.complex128, numpy.int64]
在这种情况下,正如 Joe 指出的那样,(_typelessdata[-1] is numpy.longlong) == True。
更大的问题是为什么 _typelessdata 的内容是这样设置的。
我在 numpy 源代码中唯一能找到 _typelessdata 的地方
实际使用的是this line在 np.array_repr 的定义中
在同一个文件中:
skipdtype = (arr.dtype.type in _typelessdata) and arr.size > 0
_typelessdata 的目的是确保 np.array_repr 正确打印其 dtype 恰好与(平台相关的) native Python 整数类型。
例如,在 32 位系统上,其中 int 为 32 位:
In [1]: np.array_repr(np.intc([1]))
Out[1]: 'array([1])'
In [2]: np.array_repr(np.longlong([1]))
Out[2]: 'array([1], dtype=int64)'
而在 64 位系统上,int 是 64 位:
In [1]: np.array_repr(np.intc([1]))
Out[1]: 'array([1], dtype=int32)'
In [2]: np.array_repr(np.longlong([1]))
Out[2]: 'array([1])'
arr.dtype.type in _typelessdata 检查上面的行确保打印 dtype 被跳过适当的平台相关的 native 整数 dtypes.
关于python - 为什么numpy.core.numeric._typelessdata中有两个np.int64(为什么numpy.int64不是numpy.int64?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28455982/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串