繁体   English   中英

根据规则“安全”,无法将数组数据从dtype('float64')转换为dtype('int64')! int和float之间的astype函数

[英]Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe' ! astype function between int and float

我想用PCA绘制x,y,但是我遇到了转换的问题,尽管我的列表是int并且(y,x)是float的:

    csv = np.genfromtxt ('main_contentieux_IPLSCRS_dataset.csv', 
    delimiter=";")
    y = csv[:,0]
    x = csv[:,1:]


    fig = plt.figure(1, figsize=(8, 6))
    plt.clf()
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

    plt.cla()
    pca = decomposition.PCA(n_components=4)
    pca.fit(x)
    X_fits = pca.transform(x)
    for name, label in [('Aixe en provance', 0), ('Paris', 1), 
      ('Versailles', 2)]:
       ax.text3D(X_fits[y == label, 0].mean(),X_fits[y == label, 
       1].mean()+1.5,X_fits[y == label, 
       2].mean(),name,horizontalalignment='center',bbox=dict(alpha=.8, 
       edgecolor='w', facecolor='w'))

   # Reorder the labels to have colors matching the cluster results

   y = np.choose(y,[0,2,1]).astype(np.float)

错误在于转换:

     y = np.choose(y,[0,2,1]).astype(np.float)

     y is : <class 'numpy.float64'>  and my list [0,2,1] contain int

     ** x and y contains float values,so the problem is in  the casting  when i  try to use (astype) function !  I want to fix it , ** 

np.choose期望将int数组作为第一个参数,但是您似乎正在传递浮点数。 您可以转换第一个参数来减轻这种情况:

y = np.choose(y.astype(int), [0,2,1]).astype(float)

无法从 dtype(&#39;

[英]Cannot cast array data from dtype('<M8[ns]') 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'

暂无
暂无

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

相关问题 根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;int32&#39;) 在 np.array 中查找最频繁的值会返回“TypeError:无法根据安全规则将数组数据从 dtype('float64) 转换为 dtype('int64')” 无法根据“安全”将数组数据从 dtype(&#39;float64&#39;) 转换为 dtype(&#39;int32&#39;) 无法从 dtype(&#39; 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) TypeError:无法从dtype(&#39; 无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) 无法根据规则“安全”将数组数据从dtype(&#39;complex128&#39;)强制转换为dtype(&#39;float64&#39;) odeint:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) 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')>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM