18
loading...
This website collects cookies to deliver better user experience
numpy.concatenate()
function.# import numpy
import numpy
# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])
# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate(ar1, ar2)
print(ar3)
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 9, in <module>
ar3 = numpy.concatenate(ar1, ar1)
File "< __array_function__ internals>", line 5, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index
numpy.concatenate()
method. All you need to do is enclose array 1 and array 2 inside the square brackets.# import numpy
import numpy
# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])
# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate([ar1, ar2])
print(ar3)
# Output
['Apple' 'Orange' 'Banana' 'Pineapple' 'Grapes' 'Onion' 'Potato']
numpy.concatenate()
method.# import numpy
import numpy
# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])
# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate((ar1, ar2))
print(ar3)
# Output
['Apple' 'Orange' 'Banana' 'Pineapple' 'Grapes' 'Onion' 'Potato']
import numpy as np
somelist = list(range(1000))
indices = np.random.choice(range(len(somelist)), replace=False, size=500)
print(somelist[indices.astype(int)])
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 4, in <module>
print(somelist[indices.astype(int)])
TypeError: only integer scalar arrays can be converted to a scalar index
import numpy as np
somelist = list(range(1000))
indices = np.random.choice(range(len(somelist)), replace=False, size=500)
print(np.array(somelist)[indices.astype(int)])