繁体   English   中英

根据规则'safe',无法将数组数据从dtype('float64')转换为dtype('int32')

[英]Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'

我有一个像numpy数组

result = np.array([[[289, 354, 331],
                    [291, 206,  66],
                    [242,  70, 256]],

                   [[210, 389, 342],
                    [273, 454, 218],
                    [255,  87, 256]],

                   [[127, 342, 173],
                    [450, 395, 147],
                    [223, 228, 401]]])

我试图掩盖数组,如果一个元素大于255.即我假设它是0-1024范围并将我的值除以4

result = np.putmask(result, result > 255, result/4)

注意:结果是以前的3D数组。我收到此错误

TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'

我究竟做错了什么? 提前致谢

错误说明:

这说明了numpy数组的一个有趣属性:numpy数组的所有元素必须是相同的类型

例如,如果您有以下数组:

>>> array1 = np.array([[23, 632, 634],[23.5, 67, 123.6]])
>>> array1
array([[  23. ,  632. ,  634. ],
   [  23.5,   67. ,  123.6]])
>>> type(array1[0][0])
<class 'numpy.float64'>

我们注意到,即使列表[ 23,632,634 ]中的所有元素都是int类型(特别是'numpy.int64' ), array1中的所有元素都被转换为浮点数,因为元素123.6在第二行(注意数组中的小数点打印出来)。

类似地,如果我们在数组中的任何位置包含一个字符串,则数组的所有元素都将转换为字符串:

>>> array2 = np.array([[23, 632, 'foo'],[23.5, 67, 123.6]])
>>> type(array2[0][0])
<class 'numpy.str_'>

结论:

原始结果数组包含'numpy.int64'类型元素,但result/4操作返回类型为'numpy.float64'的元素数组(因为82/4 = 20.5等)。 因此,当您尝试并替换结果中的值时,它不是“安全”,因为您无意中尝试将浮动放入一个int数组中。

问题是当你除以4时,你正在创建浮点值,它们不想进入int数组。

如果你想使用putmask ,并避免尝试转换为float的问题,那么你可以使用floor division( // )来将你的值更改为int

np.putmask(result, result>255, result//4)

>>> result
array([[[ 72,  88,  82],
        [ 72, 206,  66],
        [242,  70,  64]],

       [[210,  97,  85],
        [ 68, 113, 218],
        [255,  87,  64]],

       [[127,  85, 173],
        [112,  98, 147],
        [223, 228, 100]]])

备选方案#1:

result数组转换为float dtype ,并使用原始putmask

result = result.astype(float)

np.putmask(result, result > 255, result/4)

>>> result
array([[[ 72.25,  88.5 ,  82.75],
        [ 72.75, 206.  ,  66.  ],
        [242.  ,  70.  ,  64.  ]],

       [[210.  ,  97.25,  85.5 ],
        [ 68.25, 113.5 , 218.  ],
        [255.  ,  87.  ,  64.  ]],

       [[127.  ,  85.5 , 173.  ],
        [112.5 ,  98.75, 147.  ],
        [223.  , 228.  , 100.25]]])

如果需要,您甚至可以转换回int:

result = result.astype(int)

array([[[ 72,  88,  82],
        [ 72, 206,  66],
        [242,  70,  64]],

       [[210,  97,  85],
        [ 68, 113, 218],
        [255,  87,  64]],

       [[127,  85, 173],
        [112,  98, 147],
        [223, 228, 100]]])

备选方案#2:

完全putmask ,你可以得到你想要的结果:

result[result > 255] = result[result > 255] / 4

>>> result
array([[[ 72,  88,  82],
        [ 72, 206,  66],
        [242,  70,  64]],

       [[210,  97,  85],
        [ 68, 113, 218],
        [255,  87,  64]],

       [[127,  85, 173],
        [112,  98, 147],
        [223, 228, 100]]])

TypeError:无法将数组数据从 dtype('float64) 转换为 dtype(' <u32') according to safe rule< div><div id="text_translate"><pre> class SigmoidNeuron: def __init__(self): self.w=None self.b=None def perceptron(self,x): return np.dot(x,self.wT)+self.b def sigmoid(self,x): return 1.0/(1.0+np.exp(-x)) def grad_w(self,x,y): y_pred = self.sigmoid(self.perceptron(x)) return (y_pred-y)*y_pred*(1-y_pred)*x def grad_b(self,x,y): y_pred = self.sigmoid(self.perceptron(x)) return (y_pred-y)*y_pred*(1-y_pred) def fit(self,x,y,epochs=1,learning_rate=1,initialise=True): #initialise w,b if initialise: self.w=np.random.randn(1,X.shape[1]) self.b=0 for i in range(epochs): dw=0 db=0 for x,y in zip(X,Y): dw+=self.grad_w(x,y) db+=self.grad_b(x,y) self.w -= learning_rate*dw self.b -= learning_rate*db</pre><pre> `</pre><p> 我正在运行一个 sigmoid 神经网络代码,并且在使用数据运行此 class 时出现错误</p><pre>X_scaled_train.astype(float) array([[ 1.29929126, -0.90185206, 0.03173306, ..., -0.14142136, -0.15523011, 0.21232515], [-1.16225208, -0.86697607, 1.03451971, ..., -0.14142136, -0.15523011, 0.21232515], [ 1.77523922, 0.65594214, 0.03173306, ..., -0.14142136, -0.15523011, 0.21232515], ..., [ 1.44058831, -0.58796815, -0.66464655, ..., -0.14142136, -0.15523011, 0.21232515], [-1.42253612, 0.50481285, 1.54984063, ..., -0.14142136, -0.15523011, 0.21232515], [ 1.06875397, 0.6791928, 0.97880934, ..., -0.14142136, -0.15523011, 0.21232515]])</pre><pre> Y_scaled_train.astype(float) array([[0.68], [0.72], [0.72], [0.6 ], [0.8 ], [0.64], [0.68],</pre><p> 这些是我在运行这条线时训练集的数据 sn.fit(X_scaled_train,Y_scaled_train,epochs=10,learning_rate=0.2) 我收到了那个类型错误我应该怎么做才能删除它</p><p>错误显示</p><pre>TypeError Traceback (most recent call last) &lt;ipython-input-167-51016d58d1f5&gt; in &lt;module&gt;() ----&gt; 1 sn.fit(X_scaled_train,Y_scaled_train,epochs=10,learning_rate=0.2) 2 frames &lt;ipython-input-25-2e09637c6d09&gt; in perceptron(self, x) 4 self.b=None 5 def perceptron(self,x): ----&gt; 6 return np.dot(x,self.wT)+self.b 7 def sigmoid(self,x): 8 return 1.0/(1.0+np.exp(-x)) &lt;__array_function__ internals&gt; in dot(*args, **kwargs) TypeError: Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre></div></u32')>

[英]TypeError: cannot cast array data from dtype('float64) to dtype('<U32') according to safe rule

cut function:无法将数组数据从 dtype('float64') 转换为 dtype(' <u32') according to the rule 'safe'< div><div id="text_translate"><p> 我想将 Dataframe 的列中的内容更改为“好”或“坏”。 该列填充了从 1 到 10 的数字。1-5 是坏的,6-10 是好的。 为此,我想使用 cut 方法。</p><pre> bins = (1, 5.5, 10) rating = ['bad', 'good'] game['useropinion'] = pd.cut(rating, bins)</pre><p> 运行后的结果:</p><pre> Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><p> 怎么了? 我如何解决它?</p></div></u32')>

[英]cut function : Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

无法从 dtype(&#39;

[英]Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法根据“安全”将数组数据从 dtype(&#39;float64&#39;) 转换为 dtype(&#39;int32&#39;) Numpy.dot TypeError:根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;S32&#39;) TypeError:无法将数组数据从 dtype('float64) 转换为 dtype(' <u32') according to safe rule< div><div id="text_translate"><pre> class SigmoidNeuron: def __init__(self): self.w=None self.b=None def perceptron(self,x): return np.dot(x,self.wT)+self.b def sigmoid(self,x): return 1.0/(1.0+np.exp(-x)) def grad_w(self,x,y): y_pred = self.sigmoid(self.perceptron(x)) return (y_pred-y)*y_pred*(1-y_pred)*x def grad_b(self,x,y): y_pred = self.sigmoid(self.perceptron(x)) return (y_pred-y)*y_pred*(1-y_pred) def fit(self,x,y,epochs=1,learning_rate=1,initialise=True): #initialise w,b if initialise: self.w=np.random.randn(1,X.shape[1]) self.b=0 for i in range(epochs): dw=0 db=0 for x,y in zip(X,Y): dw+=self.grad_w(x,y) db+=self.grad_b(x,y) self.w -= learning_rate*dw self.b -= learning_rate*db</pre><pre> `</pre><p> 我正在运行一个 sigmoid 神经网络代码,并且在使用数据运行此 class 时出现错误</p><pre>X_scaled_train.astype(float) array([[ 1.29929126, -0.90185206, 0.03173306, ..., -0.14142136, -0.15523011, 0.21232515], [-1.16225208, -0.86697607, 1.03451971, ..., -0.14142136, -0.15523011, 0.21232515], [ 1.77523922, 0.65594214, 0.03173306, ..., -0.14142136, -0.15523011, 0.21232515], ..., [ 1.44058831, -0.58796815, -0.66464655, ..., -0.14142136, -0.15523011, 0.21232515], [-1.42253612, 0.50481285, 1.54984063, ..., -0.14142136, -0.15523011, 0.21232515], [ 1.06875397, 0.6791928, 0.97880934, ..., -0.14142136, -0.15523011, 0.21232515]])</pre><pre> Y_scaled_train.astype(float) array([[0.68], [0.72], [0.72], [0.6 ], [0.8 ], [0.64], [0.68],</pre><p> 这些是我在运行这条线时训练集的数据 sn.fit(X_scaled_train,Y_scaled_train,epochs=10,learning_rate=0.2) 我收到了那个类型错误我应该怎么做才能删除它</p><p>错误显示</p><pre>TypeError Traceback (most recent call last) &lt;ipython-input-167-51016d58d1f5&gt; in &lt;module&gt;() ----&gt; 1 sn.fit(X_scaled_train,Y_scaled_train,epochs=10,learning_rate=0.2) 2 frames &lt;ipython-input-25-2e09637c6d09&gt; in perceptron(self, x) 4 self.b=None 5 def perceptron(self,x): ----&gt; 6 return np.dot(x,self.wT)+self.b 7 def sigmoid(self,x): 8 return 1.0/(1.0+np.exp(-x)) &lt;__array_function__ internals&gt; in dot(*args, **kwargs) TypeError: Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre></div></u32')> cut function:无法将数组数据从 dtype('float64') 转换为 dtype(' <u32') according to the rule 'safe'< div><div id="text_translate"><p> 我想将 Dataframe 的列中的内容更改为“好”或“坏”。 该列填充了从 1 到 10 的数字。1-5 是坏的,6-10 是好的。 为此,我想使用 cut 方法。</p><pre> bins = (1, 5.5, 10) rating = ['bad', 'good'] game['useropinion'] = pd.cut(rating, bins)</pre><p> 运行后的结果:</p><pre> Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><p> 怎么了? 我如何解决它?</p></div></u32')> TypeError:无法从dtype(&#39; 无法从 dtype(&#39; 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) 类型错误:无法根据 seaborn 中的“安全”规则将数组数据从 dtype(&#39;int64&#39;) 转换为 dtype(&#39;int32&#39;) Seaborn TypeError:无法根据规则“安全”将数组数据从 dtype('int64') 转换为 dtype('int32') 无法根据规则“安全”问题将数组数据从 dtype('int64') 转换为 dtype('int32')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM