1

I have some features that look like this

[array([-725.91003  ,   51.094467 ,   14.295977 , ...,   -1.7903049,
          -1.7903049,   -1.7903049], dtype=float32),
 array([-7.5625812e+02,  7.8090454e+01, -2.7161818e+00, ...,
        -4.3733236e-01, -4.3733236e-01, -4.3733236e-01], dtype=float32),
 array([-748.3388   ,   73.042336 ,    3.2872062, ...,    0.7631229,
           0.7631229,    0.7631229], dtype=float32),
 array([-727.7924   ,  104.6542   ,    5.8776445, ...,   -1.7250983,
          -1.7250983,   -1.7250983], dtype=float32),
 array([-626.4832   ,   49.174232 ,  -17.049093 , ...,   -1.8033456,
          -1.8033456,   -1.8033456], dtype=float32),
 array([-7.2422760e+02,  1.0093343e+02,  1.8611973e+01, ...,
         5.7193387e-01,  5.7193387e-01,  5.7193387e-01], dtype=float32),
 array([-7.6023083e+02,  7.8462936e+01, -7.6474414e+00, ...,
         2.0393424e-01,  2.0393424e-01,  2.0393424e-01], dtype=float32),
 array([-732.217    ,  111.95129  ,    7.088293 , ...,   -0.8722699,
          -0.8722699,   -0.8722699], dtype=float32),
 array([-7.2999091e+02,  5.5315689e+01,  6.6589708e+00, ...,
        -5.7534605e-01, -5.7534605e-01, -5.7534605e-01], dtype=float32),
 array([-7.5546600e+02,  7.5549866e+01, -1.7497752e+00, ...,
         1.6348878e-01,  1.6348878e-01,  1.6348878e-01], dtype=float32),
 array([-735.93207  ,   67.90759  ,    5.9061728, ...,   -0.7860311,
          -0.7860311,   -0.7860311], dtype=float32),
 array([-7.5995685e+02,  6.4485733e+01,  5.4719698e-01, ...,
        -4.0942365e-01, -4.0942365e-01, -4.0942365e-01], dtype=float32),
 array([-7.2625726e+02,  7.6061371e+01,  2.1122944e+01, ...,
         6.2402117e-01,  6.2402117e-01,  6.2402117e-01], dtype=float32)]

How can I put them in a correct sequence. I want to have one outer np.array, and all the arrays that are inside must become lists. I want to put this np.array to a PCA. Generally speaking I want them in np.array format, but with the correct way.

Then I want to do something like that

pca = PCA(n_components=2)
f2d= pca.fit_transform(features)

If I do that with my initial format I take an error message

ValueError: setting an array element with a sequence.
CyberMathIdiot
  • 193
  • 1
  • 2
  • 12
  • At the moment you have a list of array and you want an array of lists? Why not a 2D array? Why not a 1D array where the first X elements are the features of your first array, etc? Why don't you show us the classifier so we can understand better what the *correct way* is? You're listing both python 2 and 3. If you aren't targetting a specific version, just keep the python tag. And if this is about machine learning, add any relevant tags. – Reti43 Dec 05 '20 at 14:53
  • I want a np.array something like that `np.array([....],[.....],[....])` – CyberMathIdiot Dec 05 '20 at 14:58
  • Yeah, you said as much in your question. But I don't understand why you'd want to do something like that. Both your construct in your question and your desired result can be iterated/accessed similarly. – Reti43 Dec 05 '20 at 15:02
  • Ok basically I want to do a PCA first (and then I will break it to train and test to use the classifier). So I want to do something like that `pca = PCA(n_components=2)` `f2d= pca.fit_transform(features)` – CyberMathIdiot Dec 05 '20 at 15:02
  • I fixed the main text, ta make clearer what I want. – CyberMathIdiot Dec 05 '20 at 15:13
  • Does this answer your question? [ValueError: setting an array element with a sequence](https://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence) – Reti43 Dec 05 '20 at 15:17
  • I think that it doesn't answer my question. – CyberMathIdiot Dec 05 '20 at 15:38

2 Answers2

0

According to the documentation fit_transform(X, y=None) wants:

X: Xarray-like, shape (n_samples, n_features) Training data, where n_samples is the number of samples and n_features is the number of features.

So in your case you want to turn your list of 1D ndarray into a 2D ndarray by:

features = np.array(features)

You don't need to turn it into 1D ndarray of lists.

Lior Cohen
  • 5,570
  • 2
  • 14
  • 30
0

Basically, I found which was the problem. The problem is that each array of features has different length. Therefore, if I do np.array(feautures), this is not exactly a "matrix", so PCA can't understand what this object is.

I think that I must change some of the previous parts of my algorithm, so as each array of feautures to has the same length.

CyberMathIdiot
  • 193
  • 1
  • 2
  • 12