简体   繁体   English

TypeError:无法从dtype('

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

When trying to plot the outliers using different colours on a scatterplot I came across this error: 当试图在散点图上使用不同的颜色绘制离群值时,我遇到了此错误:

TypeError: Cannot cast array data from dtype('U1') to dtype('float64') according to the rule 'safe' TypeError:根据规则“安全”,无法将数组数据从dtype('U1')转换为dtype('float64')

My code: 我的代码:

import statsmodels.api as sm
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

CRP = pd.read_csv('CarsProp.csv')

priceMean = CRP.price.mean()
priceStd = CRP.price.std()   

CRP['isOutlierPrice'] = np.nan
testColumn1 = abs(CRP.price - priceMean) > 2*priceStd

for i, value in enumerate(testColumn1):
    if value == True:
        CRP['isOutlierPrice'][i] = 1

mileageMean = CRP.mileage.mean()
mileageStd = CRP.mileage.mean()

CRP['isOutlierMileage'] = np.nan
testColumn2 = abs(CRP.mileage - mileageMean) > 2*priceStd

for i, value in enumerate(testColumn2):
    if value == True:
        CRP['isOutlierMileage'][i] = 1

outlierPmsJoint = ((CRP['isOutlierPrice'] == 1) | (CRP['isOutlierMileage'] == 1))

colorChoiceDict = {True: (1.0, 0.55, 0.0, 1.0), 
              False: (0.11, 0.65, 0.72, 0.1)}

colorCol = [colorChoiceDict[val] for val in outlierPmsJoint]

PriceFloat = [float(val) for val in CRP.price]
MileageFloat = [float(val) for val in CRP.mileage]

plt.figure()
plt.scatter(PriceFloat, MileageFloat, c = colorCol, linewidth='0')
plt.set_title('Price vs. Mileage with outliers')  

Does anyone know where the problem is and how to solve it? 有谁知道问题在哪里以及如何解决? Thank you, 谢谢,

The problem was in the scatter plot line. 问题出在散布图线上。 It should be: 它应该是:

plt.scatter(PriceFloat, MileageFloat, c = colorCol, linewidth=0)

I was putting the line width arg in inverted comas, which did not yield an output. 我将线宽arg置于倒置昏迷状态,这没有产生输出。 However, the error message was very misleading. 但是,错误消息是非常令人误解的。 Next time I probably will pay less attention to the error message when debugging. 下次在调试时,我可能会较少关注该错误消息。

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.

相关问题 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 Matplotlib错误TypeError:无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39; - Matplotlib Error TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe' 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 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 根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;int32&#39;) - Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe' 无法从 dtype(&#39; - Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe' Numpy.dot TypeError:根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;S32&#39;) - Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe' 无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 无法根据规则“安全”将数组数据从dtype(&#39;complex128&#39;)强制转换为dtype(&#39;float64&#39;) - Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM