This content originally appeared on CodeSource.io and was authored by Deven
If you are getting TypeError: only length-1 arrays can be converted to Python scalars error, this article will help you fix the issue.
Consider the following example which throws you the same error
import numpy as np
array1 = np.array(['apple', 'mango', 'orange', 'grapes'])
array2 = np.array(['potato', 'Tomato'])
array3 = np.concatenate(array1, array1)
print(array3)
Output of the above the Code.
Traceback (most recent call last):
File "main.py", line 7, in <module>
arraythree = np.concatenate(arrayone, arrayone)
TypeError: only length-1 arrays can be converted to Python scalars
To fix TypeError: only length-1 arrays can be converted to Python scalars issue you need to convert array 1 and array 2 into tuple or list.
Consider the example below:
# import numpy
import numpy
# Create array
array1 = numpy.array(['apple', 'mango', 'orange', 'grapes'])
array2 = numpy.array(['potato', 'Tomato'])
# Concatenate array arrray1& array2 by Tuple
array3 = numpy.concatenate((array1, array1))
print(array3)
output of the above code:
['apple' 'mango' 'orange' 'grapes' 'apple' 'mango' 'orange' 'grapes']
The post Fix – TypeError: only length-1 arrays can be converted to Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-03-07T05:29:55+00:00) Fix – TypeError: only length-1 arrays can be converted to Python. Retrieved from https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.