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

时间:2018-12-14 11:51:35

标签: python numpy pca

我想用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 , ** 

1 个答案:

答案 0 :(得分:0)

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

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