Matplotlib Scatter - ValueError:RGBA序列的长度应为3或4

Tho*_*ott 3 python numpy colors matplotlib scikit-learn

我试图为我的功能绘制图形,我不断收到此错误:

ValueError: RGBA sequence should have length 3 or 4
Run Code Online (Sandbox Code Playgroud)

每当我只有6种类型的形状时,代码就能完美运行,但现在我已将它增加到10它不会起作用?

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

我的代码是:

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()
Run Code Online (Sandbox Code Playgroud)

我的csv文件供参考

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

Jac*_*ern 7

I also ran into this error. In my case, the issue was that I was plot-ing when I meant to scatter. Changing

plt.plot(x1, x2, c=target)
Run Code Online (Sandbox Code Playgroud)

to

plt.scatter(x1, x2, c=target)
Run Code Online (Sandbox Code Playgroud)

fixed it.


Imp*_*est 5

你的y阵列看起来像

 ['3']
 ['9']
 ['0']
 ['5']
 ['5']
 ['Triangle']
 ['7']
 ['9']
 ['0']
 ['0']
...
Run Code Online (Sandbox Code Playgroud)

而实际上它应该是这样的

[3,9,0,5,5,5,7,9,0,0, ...]
Run Code Online (Sandbox Code Playgroud)