Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to plot a graph for my features and I keep getting this error:

ValueError: RGBA sequence should have length 3 or 4

The code worked perfectly whenever I only had 6 types of shapes but now that i've increased it to 10 it wont work?

If I make c=c with c = np.random.random((100, 4)) instead of c=y it works but then every data point has a different colour.

My code is:

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

As far as I can tell the problem is to do with c=y, cmap=plt.cm.Paired) but I cant be sure and I cant find a solution.

Not remove, add! I cannot run the code because there are undefined variables. Also, the code isn't minimal because of all the svm stuff. – ImportanceOfBeingErnest Mar 29, 2017 at 17:40 @ImportanceOfBeingErnest I have added in features and features1, I just completely missed them. I wasnt sure whether to include the svm in case it was relevant. – Thom Elliott Mar 29, 2017 at 17:52

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)
plt.scatter(x1, x2, c=target)

fixed it.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.