danielhomola/mifs

ValueError: Expected 2D array, got 1D array instead: ...

Closed this issue · 2 comments

kxxwz commented

Hi @danielhomola ,
Thanks for your code! When I was trying to use 'JMI' with 'categorical=False', I got the following error:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
1 start = time.time()
2 feat_selector = mifs.MutualInformationFeatureSelector(categorical=False, verbose=2, method='JMI')
----> 3 feat_selector.fit(X
, y_.reshape(-1,1))
4 print("{0:.3f} s".format(time.time()-start))

D:\Anaconda3\envs\Py2\lib\site-packages\mifs-0.0.1.dev0-py2.7.egg\mifs\mifs.pyc in fit(self, X, y)
147 self.n_jobs = NUM_CORES - self.n_jobs
148
--> 149 return self._fit(X, y)
150
151

D:\Anaconda3\envs\Py2\lib\site-packages\mifs-0.0.1.dev0-py2.7.egg\mifs\mifs.pyc in _fit(self, X, y)
191
192 def _fit(self, X, y):
--> 193 self.X, y = self._check_params(X, y)
194 n, p = X.shape
195 self.y = y.reshape((n, 1))

D:\Anaconda3\envs\Py2\lib\site-packages\mifs-0.0.1.dev0-py2.7.egg\mifs\mifs.pyc in _check_params(self, X, y)
294 ss = StandardScaler()
295 X = ss.fit_transform(X)
--> 296 y = ss.fit_transform(y)
297
298 # sanity checks

D:\Anaconda3\envs\Py2\lib\site-packages\sklearn\base.pyc in fit_transform(self, X, y, **fit_params)
516 if y is None:
517 # fit method of arity 1 (unsupervised transformation)
--> 518 return self.fit(X, **fit_params).transform(X)
519 else:
520 # fit method of arity 2 (supervised transformation)

D:\Anaconda3\envs\Py2\lib\site-packages\sklearn\preprocessing\data.pyc in fit(self, X, y)
588 # Reset internal state before fitting
589 self._reset()
--> 590 return self.partial_fit(X, y)
591
592 def partial_fit(self, X, y=None):

D:\Anaconda3\envs\Py2\lib\site-packages\sklearn\preprocessing\data.pyc in partial_fit(self, X, y)
610 """
611 X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
--> 612 warn_on_dtype=True, estimator=self, dtype=FLOAT_DTYPES)
613
614 # Even in the case of with_mean=False, we update the mean anyway

D:\Anaconda3\envs\Py2\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
408 "Reshape your data either using array.reshape(-1, 1) if "
409 "your data has a single feature or array.reshape(1, -1) "
--> 410 "if it contains a single sample.".format(array))
411 array = np.atleast_2d(array)
412 # To ensure that array flags are maintained

ValueError: Expected 2D array, got 1D array instead:
array=[ 0.8762763 0.98324755 0.69945964 0.93662919 0.02916694 ... 0.53569545 ]
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample._

I believe the array I input to the function is 2D, but it still outputs such error. If I change 'categorical=False' to 'categorical=True', then everything works well. Can you give me some help?
Thanks a lot!

I think you're using a new scikit-learn version and it's throwing an error because in the new version everything has to be a 2d matrix, even a single column or row.

It even says: Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Have you tried this on your y column?

Yes, the example provided fails in this way and the coercion to 1D appears to be done here: https://github.com/danielhomola/mifs/blob/master/mifs/mifs.py#L291 by calling check_X_y().

Do you have any plan to make this package compatible with the current sklearn?

See also #14