you may want to start with V=np.eye(X.shape[1]) instead of a random one.
Michael
On Thursday, July 10, 2014, Danny Sullivan <[email protected]> wrote:
> Hi Sheila,
>
> The mahalanobis metric is a special case that needs a covariance matrix to
> be passed in. You can generate one by doing:
>
> import numpy as np
>
> rng = np.random.RandomState(0)
>
> V = rng.rand(X.shape[1],X.shape[1])
>
>
> clf = KNeighborsClassifier(n_neighbors=5, metric="mahalanobis", V =
> np.dot(V, V.T))
>
>
> clf.fit(X,y)
>
> print(clf.predict(X))
>
>
> That should work for getting the fit and the predict to work, but I'm not
> the most experienced on the malalanobis metric so someone else may want to
> back me up on this one.
>
>
> -Danny
>
> ------------------------------
> Date: Thu, 10 Jul 2014 14:12:06 +0200
> From: [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> To: [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> Subject: Re: [Scikit-learn-general] metric in neighbors classifier
>
> X.shape
> (150, 2)
>
> X.dtype
> dtype('float64')
>
> Here is how I get the data
>
> from sklearn import datasets
>
> from sklearn.neighbors import KNeighborsClassifier
>
>
> iris = datasets.load_iris()
> X = iris.data[:, :2]
> y = iris.target
>
> clf = KNeighborsClassifier(n_neighbors=5, metric="mahalanobis")
> clf.fit(X,y)
>
>
>
>
>
> On 10 July 2014 14:01, Kyle Kastner <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
> OK - what is the result of X.shape and X.dtype? What is X?
>
>
> On Thu, Jul 10, 2014 at 1:55 PM, Sheila the angel <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
> Yes, the error is in fit(X,y)
>
> clf.fit(X,y)
>
> ---------------------------------------------------------------------------
> Traceback (most recent call last):
>
> File "<ipython-input-77-12b5b6bd5106>", line 1, in <module>
> clf.fit(X,y)
>
> File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.py",
> line 630, in fit
> return self._fit(X)
>
> File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.py",
> line 216, in _fit
> **self.effective_metric_kwds_)
>
> File "binary_tree.pxi", line 1062, in
> sklearn.neighbors.ball_tree.BinaryTree.__init__
> (sklearn/neighbors/ball_tree.c:8037)
>
> File "dist_metrics.pyx", line 280, in
> sklearn.neighbors.dist_metrics.DistanceMetric.get_metric
> (sklearn/neighbors/dist_metrics.c:4066)
>
> File "dist_metrics.pyx", line 636, in
> sklearn.neighbors.dist_metrics.MahalanobisDistance.__init__
> (sklearn/neighbors/dist_metrics.c:7162)
>
> File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line
> 510, in inv
> _assertRankAtLeast2(a)
>
> File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line
> 202, in _assertRankAtLeast2
> 'at least two-dimensional' % len(a.shape))
>
> LinAlgError: 0-dimensional array given. Array must be at least
> two-dimensional
>
>
>
>
> On 10 July 2014 13:36, Danny Sullivan <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
> Hi Sheila,
>
> That looks right, I'm guessing you're getting an error when performing the
> fit, not when setting the metric. What's the error you're getting?
>
>
> On 7/10/14, 11:45 AM, Sheila the angel wrote:
>
> What is the correct way to use different metric in KNeighborsClassifier ?
>
> I tried this
>
> clf = KNeighborsClassifier(metric="mahalanobis").fit(X, y)
>
> which give me error.
>
>
> Thanks
> --
> Sheila
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awardshttp://p.sf.net/sfu/Bonitasoft
>
>
>
> _______________________________________________
> Scikit-learn-general mailing [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
>
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> Scikit-learn-general mailing list
> [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
>
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> Scikit-learn-general mailing list
> [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
>
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> Scikit-learn-general mailing list
> [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows Winner
> of BOSSIE, CODIE, OW2 and Gartner awards http://p.sf.net/sfu/Bonitasoft
> _______________________________________________ Scikit-learn-general
> mailing list [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Scikit-learn-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general