[英]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.
下次在调试时,我可能会较少关注该错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.