Well the error message says it all: you cannot use len on a sparse matrix.
Instead of len(X), use X.shape[0].

Mathieu

On Fri, Jul 12, 2013 at 11:35 PM, Hueseyin Hakan Pekmezci <
pekme...@rhrk.uni-kl.de> wrote:

> Hi scikit-learn members,
>
> 0.13.1 documentation states that individual datasets can
> be loaded in svmlight / libsvm format. So I have fed in
> "iris.scale" libSVM dataset  however some erroneous
> behaviour happens. I am just trying to reproduce
> "plot_iris_exercise.py" with
> iris.scale(
> http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/iris.scale
> ).
>
> The problem in CLI:
>
> $python2.7 linsvm.py iris.scale
> Accuracy 0.973333333333
> Traceback (most recent call last):
>    File "linsvm.py", line 28, in <module>
>      n_sample = len(X)
>    File
> "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py",
> line 175, in __len__
>      raise TypeError("sparse matrix length is ambiguous;
> use getnnz()"
> TypeError: sparse matrix length is ambiguous; use getnnz()
> or shape[0]
>
> The code is very simple below:
>
> import numpy as np
> import pylab as pl
> from sklearn import datasets, svm
> from sklearn.datasets import load_svmlight_file
> import sys
>
> #iris = datasets.load_iris()
> #X = iris.data
> #y = iris.target
>
> ##import iris.scale dataset in LibSVM
> X_in, y_in=load_svmlight_file(sys.argv[1])
>
> X_train=X_in
> y_train=y_in
> X_test=X_in
> y_test=y_in
>
> svc=svm.SVC(kernel='linear')
> clf=svc.fit(X_train, y_train)
> y_pred =clf.predict(X_test)
> print "Accuracy", np.mean(y_pred == y_test)
>
> X = X_in[y_in != 0, :2]
> y = y_in[y_in != 0]
>
> ##The following code is from plot_iris_exercise.py
> n_sample = len(X)
>
> np.random.seed(0)
> order = np.random.permutation(n_sample)
> X = X[order]
> y = y[order].astype(np.float)
>
> X_train = X[:.9 * n_sample]
> y_train = y[:.9 * n_sample]
> X_test = X[.9 * n_sample:]
> y_test = y[.9 * n_sample:]
>
> # fit the model
> for fig_num, kernel in enumerate(('linear', 'rbf',
> 'poly')):
>      clf = svm.SVC(kernel=kernel, gamma=10)
>      clf.fit(X_train, y_train)
>
>      pl.figure(fig_num)
>      pl.clf()
>      pl.scatter(X[:, 0], X[:, 1], c=y, zorder=10,
> cmap=pl.cm.Paired)
>
>      # Circle out the test data
>      pl.scatter(X_test[:, 0], X_test[:, 1], s=80,
> facecolors='none', zorder=10)
>
>      pl.axis('tight')
>      x_min = X[:, 0].min()
>      x_max = X[:, 0].max()
>      y_min = X[:, 1].min()
>      y_max = X[:, 1].max()
>
>      XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
>      Z = clf.decision_function(np.c_[XX.ravel(),
> YY.ravel()])
>
>      # Put the result into a color plot
>      Z = Z.reshape(XX.shape)
>      pl.pcolormesh(XX, YY, Z > 0, cmap=pl.cm.Paired)
>      pl.contour(XX, YY, Z, colors=['k', 'k', 'k'],
> linestyles=['--', '-', '--'],
>                 levels=[-.5, 0, .5])
>
>      pl.title(kernel)
> pl.show()
>
>
> Many thanks,
>
> Hakan
>
>
> ------------------------------------------------------------------------------
> See everything from the browser to the database with AppDynamics
> Get end-to-end visibility with application monitoring from AppDynamics
> Isolate bottlenecks and diagnose root cause in seconds.
> Start your free trial of AppDynamics Pro today!
> http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
> _______________________________________________
> Scikit-learn-general mailing list
> Scikit-learn-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Scikit-learn-general mailing list
Scikit-learn-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general

Reply via email to