1

I'm having a hard time trying to concatenate two arrays using numpy. One of the arrays have text (string) and the other array have numbers (int64).

How do I do that?

Using np.concatenate() set all the values as string and need both.

I'm running a for loop to determinate the hyperparameters of a RandomForestClassifier... when the loop goes to the numbers, gives an error since is expecting the numbers and get the string '1' or '2'.

I'm using

np.concatenate((['auto'], np.arange(20, 120, 20)), axis=0, out=None)

and getting

array(['auto', '20', '40', '60', '80', '100'], dtype='<U11')

However, I need

array(['auto', 20, 40, 60, 80, 100])
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50

2 Answers2

1

One of the arrays you're concatenating should have object dtype in order to get a final array with object type which can hold items with heterogeneous data types:

In [7]: np.concatenate((['auto'], np.arange(20, 120, 20).astype(object)), axis=0, out=None)
Out[7]: array(['auto', 20, 40, 60, 80, 100], dtype=object)

And if you're wondering how does Numpy determine the array types you can read How does numpy determin the object-array's dtype and what it means?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • With `object` dtype you loose a lot of the speed and convenience of working with numeric dtypes. Such an array is basically a list. In fact iteration on an equivalent list is faster. – hpaulj Apr 14 '19 at 15:58
0

While maybe not being the best solution, this will work:

np.asarray(['auto'] + list(np.arange(20, 120, 20)), dtype=object)

Result:

array(['auto', 20, 40, 60, 80, 100], dtype=object)

The problem lies in the fact that you are combining different types, and so you need to tell numpy that all objects are allowed as they are, without conversion.

Zionsof
  • 1,196
  • 11
  • 23