【问题标题】:Matplotlib Scatter - ValueError: RGBA sequence should have length 3 or 4Matplotlib Scatter - ValueError:RGBA 序列的长度应为 3 或 4
【发布时间】:2017-08-23 06:45:31
【问题描述】:

我正在尝试为我的功能绘制图表,但我不断收到此错误:

ValueError: RGBA sequence should have length 3 or 4

只要我只有 6 种形状,代码就可以完美运行,但现在我将它增加到 10 种,它就不能工作了?

如果我用c = np.random.random((100, 4)) 而不是c=y 制作c=c,它可以工作,但是每个数据点都有不同的颜色。

我的代码是:

features = ["Number of Sides", "Standard Deviation of Number of Sides/Perimeter",
      "Standard Deviation of the Angles", "Largest Angle"]

features1 = ["Label"]

def Build_Data_Set():

    data_df = pd.DataFrame.from_csv("AllMixedShapes2.csv")

    #This line randomly shuffles the data so that the different types of training data get
    #mixed up randomly to prevent the data being skewed
    data_df = data_df.reindex(np.random.permutation(data_df.index))
    X = np.array(data_df[features].values)

    data_df2 = pd.DataFrame.from_csv("AllMixedShapes2.csv")
    y = np.array(data_df2[features1].replace("Circle",0).replace("Equilateral Triangle",1)
             .replace("Right Angle Triangle",2).replace("Acute Triangle",3)
             .replace("Obtuse Triangle",4).replace("Square",5)
             .replace("Parallelogram",6).replace("Rectangle",7)
             .replace("Pentagon",8).replace("Seal",9).values.tolist())
return X,y

def SVC_Analysis():

    test_size = 300
    X,y = Build_Data_Set()

    clf = svm.SVC(kernel = 'rbf', C = 1.0)
    clf.fit(X[:test_size],y[:test_size])

    correct_count = 0

    for x in range(1, test_size+1):
            if clf.predict(X[-x])[0] == y[-x]:
                correct_count += 1

    print("Accuracy:", (correct_count/test_size) * 100.00)


    data_df = pd.DataFrame.from_csv("AllMixedShapes2.csv")
    X1 = np.array(data_df[features2].values)
    y1 = np.array(data_df[features3].values)
    #w = clf.coef_[0]
    #a = -w[0] / w[1]
    xx = np.linspace(0,5)
    yy = np.linspace(0,185)

    h0 = plt.plot(xx,yy, "k-", label="non weighted")

    plt.scatter(X1[:, 0],y1, c=y, cmap=plt.cm.Paired)
    plt.ylabel("Maximum Angle (Degrees)")
    plt.xlabel("Number Of Sides")
    plt.title('Shapes')
    plt.legend()
    plt.show()

SVC_Analysis()

My csv file for reference looks like

据我所知,问题与c=y, cmap=plt.cm.Paired) 有关,但我不能确定,也找不到解决方案。

【问题讨论】:

  • @ImportanceOfBeingErnest 您希望我删除什么?或者你有答案吗?
  • 不删除,添加!我无法运行代码,因为有未定义的变量。此外,由于所有 svm 的东西,代码并不是最少的。
  • @ImportanceOfBeingErnest 我已经添加了featuresfeatures1,我只是完全错过了它们。我不确定是否包含 svm 以防万一。

标签: python numpy matplotlib colors scikit-learn


【解决方案1】:

我也遇到了这个错误。就我而言,问题是当我打算 scatter 时,我是 plot-ing。改变

plt.plot(x1, x2, c=target)

plt.scatter(x1, x2, c=target)

修复它。

【讨论】:

    【解决方案2】:

    你的 y 数组看起来像

     ['3']
     ['9']
     ['0']
     ['5']
     ['5']
     ['Triangle']
     ['7']
     ['9']
     ['0']
     ['0']
    ...
    

    实际上应该是这样的

    [3,9,0,5,5,5,7,9,0,0, ...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多