繁体   English   中英

Numpy.dot TypeError:根据规则'safe',无法将数组数据从dtype('float64')转换为dtype('S32')

[英]Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'

使用np.dot(a,bT)时为什么会出现此错误:

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

a和b的类型为numpy.ndarray 我的NumPy版本是1.11.0。

只需从BrenBarn和Warren Weckesser那里获取输入,就可以提供一个应该运行的代码片段(通过将你的字符串转换为float):

a = map(lambda x: float(x),a)
b = map(lambda x: float(x),b)
np.dot(a,b.T)

或者@JLT建议的更简单

a = map(float,a)
b = map(float,b)
np.dot(a,b.T)

但正如Warren Weckesser所说,你应该检查数组的类型,很可能已经包含了浮点数。

尝试将整个numpy数组转换为float示例:

train = train.astype(float)
train_target = train_target.astype(float)

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'

(Python) Pivot 表 - 无法将数组数据从 dtype('float64) 转换为 dtype(' <u32') according to safe rule< div><div id="text_translate"><p> 在制作 pivot 表时,我遇到了转换数组数据错误的问题。 我已经彻底检查了所有组件都是float64。 但是错误仍然存在。 我的代码片段转载如下:</p><pre> bins_array=np.asarray(bins_list,dtype = np.float64) Genesis_file['PASS_DATE']=np.round(((date_rp-Genesis_file['VALUE_DATE']).dt.days)/365,1) temp_file=Genesis_file.pivot_table(values ='ACY_CURR_BALANCE_2021.03.19',index = pd.cut('CM_MIS_Y',bins = bins_array), columns = pd.cut('PASS_DATE',bins = bins_array),fill_value = 0.0, aggfunc = 'sum', dropna = False) &gt;&gt;&gt;TypeError: Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><pre> Genesis_file['ACY_CURR_BALANCE_2021.03.19'].dtypes &gt;&gt;&gt;dtype('float64') Genesis_file['CM_MIS_Y'].dtypes &gt;&gt;&gt;dtype('float64') Genesis_file['PASS_DATE'].dtypes &gt;&gt;&gt;dtype('float64')</pre><p> 所有列都是float64; 但是 ['ACY_CURR_BALANCE_2021.03.19'] 列没有四舍五入; ['CM_MIS_Y'] 和 ['PASS_DATE'] 舍入到小数点后 1 位(希望信息有所帮助!)</p><p> 非常感谢!</p></div></u32')>

[英](Python) Pivot table- cannot cast array data from dtype('float64) to dtype('<U32') according to safe rule

暂无
暂无

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

相关问题 Python将CSV导入到数组TypeError中:无法根据规则“安全”将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;S32&#39;) 使用matplotlib绘制3d线图。 TypeError:根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;S32&#39;) 根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;int32&#39;) TypeError:无法从dtype(&#39; 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) Matplotlib错误TypeError:无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#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')> (Python) Pivot 表 - 无法将数组数据从 dtype('float64) 转换为 dtype(' <u32') according to safe rule< div><div id="text_translate"><p> 在制作 pivot 表时,我遇到了转换数组数据错误的问题。 我已经彻底检查了所有组件都是float64。 但是错误仍然存在。 我的代码片段转载如下:</p><pre> bins_array=np.asarray(bins_list,dtype = np.float64) Genesis_file['PASS_DATE']=np.round(((date_rp-Genesis_file['VALUE_DATE']).dt.days)/365,1) temp_file=Genesis_file.pivot_table(values ='ACY_CURR_BALANCE_2021.03.19',index = pd.cut('CM_MIS_Y',bins = bins_array), columns = pd.cut('PASS_DATE',bins = bins_array),fill_value = 0.0, aggfunc = 'sum', dropna = False) &gt;&gt;&gt;TypeError: Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><pre> Genesis_file['ACY_CURR_BALANCE_2021.03.19'].dtypes &gt;&gt;&gt;dtype('float64') Genesis_file['CM_MIS_Y'].dtypes &gt;&gt;&gt;dtype('float64') Genesis_file['PASS_DATE'].dtypes &gt;&gt;&gt;dtype('float64')</pre><p> 所有列都是float64; 但是 ['ACY_CURR_BALANCE_2021.03.19'] 列没有四舍五入; ['CM_MIS_Y'] 和 ['PASS_DATE'] 舍入到小数点后 1 位(希望信息有所帮助!)</p><p> 非常感谢!</p></div></u32')> 无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM