-1

Imagine that you have created an array with 100 dimensions and then you calculate something and fill this array. for whatever reason, you have not created 2d array, what is wrong with this question that you want to assign another dimension to this data, with this justification that for example 250 samples should have this calculated data?!!

I have searched this but I could not find any solution. Maybe I am not searching with correct keyword!

Actually I want to reshape a numpy array of (100,) to (250,100). I have read this link and a couple of other links but did not help me.

I have also tried this way:

numpyarray = (100,)
transformed_numpyarray = np.reshape(numpyarray,(100,-1)).T

which gives me this output:

(1, 100)

but I really do not want 1 as the first item of 2d array. what Im trying to do is to either convert to (,100) or at least something like this (250,100). "250" is a constant number I know already so I want to say for example for 250 samples with 100 dimension.

Thanks.

sariii
  • 2,020
  • 6
  • 29
  • 57
  • 3
    `(100,)` is standard python notation for a 1 element tuple. `(,100)` is not valid python syntax. – hpaulj Nov 21 '18 at 01:54
  • `np.reshape(numpyarray,(100,-1))` gives an error, `cannot reshape array of size 1 into shape (100,newaxis)`. Your description confuses arrays and their shapes. – hpaulj Nov 21 '18 at 02:42
  • Your link is to a `keras` question. there `(None, 100)` is a valid shape. There isn't a `numpy` equivalent. – hpaulj Nov 21 '18 at 02:43
  • @hpaulj thank you so much for your explanations. though (,100) may not be a valid but I need it to be in this way. Or Im gonna to update my question to another way. I will be ok to update (100,) to (250,100). 250 is something constant. is in this way is doable? – sariii Nov 21 '18 at 03:30
  • You still seem to be confused about dimensions, shape and reshaping. I'd suggest working with simple examples like `np.arange(24).reshape(4,6)`. – hpaulj Nov 21 '18 at 05:39
  • If you trying to build a 2d array from an unknown number of 1d arrays, we usually recommend collecting them in a list, and make the array with one call. List appends are efficient. Just make sure all 1d arrays have the same size. – hpaulj Nov 21 '18 at 06:35
  • 1
    Question has nothing to do with `machine-learning` or `scikit-learn` - kindly do not spam the tags (removed). – desertnaut Nov 21 '18 at 12:14
  • @desertnaut you are right, as it was part of my experience toward machine learning project I tagged them. but thanks for updating that :) – sariii Nov 21 '18 at 15:53
  • @hpaulj you are right. actually I have one dimension array with some calculated data, then I want to convert it to two dimension array. to be honest i did not know how can I ask my question, like it should be called reshaping or other way saying. till now I found that I can do that by for loop, creating an empty 2d array and then re-fill it with my data but Im not sure its an ellegant way! – sariii Nov 21 '18 at 15:57
  • Do want a 2d array with the same data in each row? If so,why? What do you intend to do with it? – hpaulj Nov 21 '18 at 16:00
  • @hpaulj exactly I want an 2d array with the same data. like Im imagining all the samples should have that calculated data.its a research based experiment that I need to do this in part of my problem(the reason why I do not start from a 2d array from the scratch has a reason, that Im not able to do) thats why I need to later convert it to 2d array :) – sariii Nov 22 '18 at 16:51

2 Answers2

1

numpy's arrays are static sized, you can't have an array with a variable shape. If you don't know beforehand how many samples you will have you can gradually add them with vstack:

In [4]: numpyarray.shape                                                        
Out[4]: (3, 4)

In [5]: new_sample.shape                                                        
Out[5]: (4,)

In [6]: numpyarray = np.vstack([numpyarray, new_sample])                        

In [7]: numpyarray.shape                                                       
Out[7]: (4, 4)

you can also first define the size by creating an array full of zeros and then progressively fill it with samples.

numpyarray = np.zeros((250,100))
...
numpyarray[i] = new_sample
vlizana
  • 2,962
  • 1
  • 16
  • 26
  • thanks for the answer, but I got this error after running your example ValueError: all the input array dimensions except for the concatenation axis must match exactly. do you have any idea of this? – sariii Nov 21 '18 at 03:25
  • I also updated my question. in case it will be easier to convert to a constant number like 250 – sariii Nov 21 '18 at 03:34
  • 1
    The first error means that the length of the samples must be the same as the `numpyarray` concatenation axis, in this case, all of your samples must have shape `(100,)` or `(1,100)` and `numpyarray` must be `(n,100)`. About the update on the question, when you reshape an array the resulting array must have the same number of cells, in this case the resulting array would have a lot more. I edited my answer about how this can be done. – vlizana Nov 21 '18 at 03:45
  • 1
    thank you so much. for the explanations of the error, I followed exactly like your example but got that error. your second answer seems doable but I think I need to for loop through the data?. I'm not at work right now. I will try on that and then get back to mark as answer. again thanks for taking the time :) – sariii Nov 21 '18 at 04:08
1

I'm still confused about what you are trying to do. So far I can picture two alternatives - reshape and repeat. To illustrate:

In [148]: x = np.arange(16)
In [149]: x
Out[149]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

In [150]: x.reshape(4,4)
Out[150]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [151]: np.repeat(x[None,:], 4, axis=0)
Out[151]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353