簡體   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