In [404]: RV = np.array([[0.23, 2.5 , 5. , 7.1],['a', 'b'],['a1', 'a2']])
In [405]: RV
Out[405]:
array([list([0.23, 2.5, 5.0, 7.1]), list(['a', 'b']), list(['a1', 'a2'])],
dtype=object)
RV is a object dtype array because the lists vary in size. It is essentially a list. In fact, give how you use it, you might as well leave it as a list.
In [406]: def combination(RV):
...: r = np.array(np.meshgrid(*RV)).T.reshape(-1,len(RV))
...: return r
In [407]: r = combination(RV)
In [408]: r
Out[408]:
array([['0.23', 'a', 'a1'],
['0.23', 'b', 'a1'],
['2.5', 'a', 'a1'],
['2.5', 'b', 'a1'],
['5.0', 'a', 'a1'],
['5.0', 'b', 'a1'],
['7.1', 'a', 'a1'],
['7.1', 'b', 'a1'],
['0.23', 'a', 'a2'],
['0.23', 'b', 'a2'],
['2.5', 'a', 'a2'],
['2.5', 'b', 'a2'],
['5.0', 'a', 'a2'],
['5.0', 'b', 'a2'],
['7.1', 'a', 'a2'],
['7.1', 'b', 'a2']], dtype='<U32')
r is a string dtype - all of it. You can convert a column, but you can't put the float values back into r (without them being converted back to strings).
In [409]: r[:,0].astype(float)
Out[409]:
array([0.23, 0.23, 2.5 , 2.5 , 5. , 5. , 7.1 , 7.1 , 0.23, 0.23, 2.5 ,
2.5 , 5. , 5. , 7.1 , 7.1 ])
meshgrid preserves the dtype when creating the list of arrays:
In [410]: np.meshgrid(*RV)
Out[410]:
[array([[[0.23, 0.23],
[2.5 , 2.5 ],
[5. , 5. ],
[7.1 , 7.1 ]],
[[0.23, 0.23],
[2.5 , 2.5 ],
[5. , 5. ],
[7.1 , 7.1 ]]]), array([[['a', 'a'],
['a', 'a'],
['a', 'a'],
['a', 'a']],
[['b', 'b'],
['b', 'b'],
['b', 'b'],
['b', 'b']]], dtype='<U1'), array([[['a1', 'a2'],
['a1', 'a2'],
['a1', 'a2'],
['a1', 'a2']],
[['a1', 'a2'],
['a1', 'a2'],
['a1', 'a2'],
['a1', 'a2']]], dtype='<U2')]
But when you wrap them in np.array it uses the common compatible dtype, string. You can individually reshape an element of that meshgrid list:
In [411]: _[0].ravel()
Out[411]:
array([0.23, 0.23, 2.5 , 2.5 , 5. , 5. , 7.1 , 7.1 , 0.23, 0.23, 2.5 ,
2.5 , 5. , 5. , 7.1 , 7.1 ])
Are you fully aware of consequences of making object dtype arrays?
By the way, look at this alternative RV:
In [416]: np.array([[0.23, 2.5],['a', 'b'],['a1', 'a2']])
Out[416]:
array([['0.23', '2.5'],
['a', 'b'],
['a1', 'a2']], dtype='<U32')
In [417]: np.array([[0.23, 2.5],['a', 'b'],['a1', 'a2']],object)
Out[417]:
array([[0.23, 2.5],
['a', 'b'],
['a1', 'a2']], dtype=object)
Reliably creating an object dtype array with a given shape is not a trivial task.
r