diff --git a/scikits/learn/lda.py b/scikits/learn/lda.py index 01caf0e1bea59dc9e6d911a063222cf4cce5b7db..ebd189a3f8cc5241d9cbd9558b8829f5b832625c 100644 --- a/scikits/learn/lda.py +++ b/scikits/learn/lda.py @@ -84,10 +84,14 @@ class LDA(BaseEstimator, ClassifierMixin): self._set_params(**params) X = np.asanyarray(X) y = np.asanyarray(y) - if y.dtype.char.lower() not in ('i', 'l'): + if y.dtype.char.lower() not in ('b', 'h', 'i'): # We need integer values to be able to use - # ndimage.measurements and np.bincount on numpy > 2.0 - y = y.astype(np.int) + # ndimage.measurements and np.bincount on numpy >= 2.0. + # We currently support (u)int8, (u)int16 and (u)int32. + # Note that versions of scipy >= 0.8 can also accept + # (u)int64. We however don't support it for backwards + # compatibility. + y = y.astype(np.int32) if X.ndim != 2: raise ValueError('X must be a 2D array') if X.shape[0] != y.shape[0]: @@ -102,8 +106,8 @@ class LDA(BaseEstimator, ClassifierMixin): raise ValueError('y has less than 2 classes') classes_indices = [(y == c).ravel() for c in classes] if self.priors is None: - counts = np.array(ndimage.measurements.sum(np.ones(len(y)), - y, index=classes)) + counts = np.array(ndimage.measurements.sum( + np.ones(n_samples, dtype=y.dtype), y, index=classes)) self.priors_ = counts / float(n_samples) else: self.priors_ = self.priors diff --git a/scikits/learn/qda.py b/scikits/learn/qda.py index 5cbeeb6537785ea75eb6717feba4b26d694254d8..40bdfe70e6bb4e7be4f4e6d507d9f0bb206da4ee 100644 --- a/scikits/learn/qda.py +++ b/scikits/learn/qda.py @@ -96,10 +96,14 @@ class QDA(BaseEstimator, ClassifierMixin): raise ValueError( 'Incompatible shapes: X has %s samples, while y ' 'has %s' % (X.shape[0], y.shape[0])) - if y.dtype.char.lower() not in ('i', 'l'): + if y.dtype.char.lower() not in ('b', 'h', 'i'): # We need integer values to be able to use - # ndimage.measurements and np.bincount on numpy > 2.0 - y = y.astype(np.int) + # ndimage.measurements and np.bincount on numpy >= 2.0. + # We currently support (u)int8, (u)int16 and (u)int32. + # Note that versions of scipy >= 0.8 can also accept + # (u)int64. We however don't support it for backwards + # compatibility. + y = y.astype(np.int32) n_samples, n_features = X.shape classes = np.unique(y) n_classes = classes.size @@ -107,8 +111,8 @@ class QDA(BaseEstimator, ClassifierMixin): raise exceptions.ValueError('y has less than 2 classes') classes_indices = [(y == c).ravel() for c in classes] if self.priors is None: - counts = np.array(ndimage.measurements.sum(np.ones(len(y)), - y, index=classes)) + counts = np.array(ndimage.measurements.sum( + np.ones(n_samples, dtype=y.dtype), y, index=classes)) self.priors_ = counts / float(n_samples) else: self.priors_ = self.priors