Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to plot a wonderful PCA using mlpy, and the c argument to plt.scatter is causing an exception.

dataset: http://mlpy.sourceforge.net/docs/3.5/_downloads/iris.csv

import mlpy
from matplotlib.mlab import PCA
import matplotlib.pyplot as plt

def classification():
    # x: (observations x attributes) matrix
    x = np.array(iris)[:, :4]
    # y: classes (1: setosa, 2: versicolor, 3: virginica)
    outputs_file="/Users/vasques/Desktop/classification.csv"
    y = np.loadtxt(outputs_file, delimiter=',')[:, 4].astype(np.int)

    pca = mlpy.PCA() # new PCA instance
    pca.learn(x) # learn from data
    z = pca.transform(x, k=2) # embed x into the k=2 dimensional subspace

    plt.set_cmap(plt.cm.Paired)
    fig1 = plt.figure(1)
    title = plt.title("PCA")
    plot = plt.scatter(z[:, 0], z[:, 1], c=y)
    labx = plt.xlabel("First component")
    laby = plt.ylabel("Second component")
    plt.show()

and I have the following error:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 1050, in draw
    func(*args)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axes/_base.py", line 2075, in draw
    a.draw(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/collections.py", line 729, in draw
    Collection.draw(self, renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/collections.py", line 301, in draw
    self._offset_position)
  File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.py", line 79, in draw_path_collection
    all_transforms = [t.get_matrix() for t in all_transforms]
AttributeError: 'numpy.ndarray' object has no attribute 'get_matrix'

When I delete c=y in plt.scatter it runs but I have a single color that does not allow me to recognize the groups.

share|improve this question
    
and this is exactly the same code as here: mlpy.sourceforge.net/docs/3.5/… –  user3091978 Dec 11 '13 at 16:43
    
Might want to clean up the source code a bit first. For example you define data=np.zeros(shape=(4,4)) and then immediately redefine it to data=np.array(iris); makes it hard to determine what the intent is. –  Iguananaut Dec 11 '13 at 17:10
    
@user3091978 Can you make a simpler example that is copy-pastable? –  tcaswell Jan 20 at 3:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.