Issue
I have the following array x
:
x = np.array([[ 0. , 1. , 0.47351264, 2. ],
[ 2. , 3. , 0.65425345, 2. ],
[20. , 21. , 0.71297456, 2. ],
[22. , 23. , 0.6863126 , 2. ],
[24. , 25. , 0.67509844, 2. ],
[26. , 27. , 0.68440122, 2. ],
[28. , 29. , 0.68737942, 2. ],
[30. , 31. , 0.71718599, 2. ],
[32. , 33. , 0.73359071, 2. ],
[34. , 35. , 0.6795543 , 2. ],
[ 4. , 5. , 0.64574043, 2. ],
[ 6. , 7. , 0.63806522, 2. ],
[ 8. , 9. , 0.69928704, 2. ],
[10. , 11. , 0.6190996 , 2. ],
[12. , 13. , 0.68331497, 2. ],
[14. , 15. , 0.65097253, 2. ],
[16. , 17. , 0.6896147 , 2. ],
[18. , 19. , 0.61292212, 2. ],
[37. , 38. , 0.58519669, 4. ],
[47. , 48. , 0.65853573, 4. ],
[49. , 50. , 0.63965727, 4. ],
[51. , 52. , 0.64220952, 4. ],
[53. , 54. , 0.65616207, 4. ],
[39. , 40. , 0.70860266, 4. ],
[41. , 42. , 0.68387971, 4. ],
[43. , 44. , 0.69205266, 4. ],
[45. , 46. , 0.73869603, 4. ],
[55. , 56. , 0.64534955, 8. ],
[57. , 58. , 0.65458359, 8. ],
[59. , 60. , 0.70113174, 8. ],
[61. , 62. , 0.68490694, 8. ],
[63. , 36. , 0.66786758, 5. ],
[64. , 65. , 0.67181672, 16. ],
[66. , 67. , 0.68897615, 16. ],
[69. , 70. , 0.68773403, 32. ],
[71. , 68. , 0.6894432 , 37. ]])
And I tried to execute below:
np.array(map(tuple,x),dtype=[('i0',int),('i1',int), ('dist',float),('num',int)])
I am not entirely sure what is the above code trying to achieve. Can someone explain?
And I got the following error when I tried to execute the code:
*** TypeError: int() argument must be a string, a bytes-like object or a real number, not 'map'
Solution
The code you provided seems to be an attempt to convert the NumPy array x into an array of tuples with specific data types for each element within the tuple.
Breaking down the code:
map(tuple, x)
is attempting to convert each sub-array within the NumPy array x into a tuple. This creates an iterator of tuples.
np.array(..., dtype=[('i0', int), ('i1', int), ('dist', float), ('num', int)])
aims to convert this iterator of tuples into a NumPy array with specific data types assigned to each element within the tuple.
The error you're encountering (TypeError: int() argument must be a string, a bytes-like object or a real number, not 'map') is due to the fact that map(tuple, x) returns an iterator object, and NumPy is not able to convert this directly into an array with specified data types.
To fix this, you should convert the map result explicitly into a list of tuples before creating the NumPy array:
import numpy as np
x = np.array([[0. , 1. , 0.47351264, 2. ],
# ... (your provided array continues here)
[71. , 68. , 0.6894432 , 37. ]])
# Convert the array 'x' into a list of tuples and then create a structured NumPy array
converted_list = list(map(tuple, x))
result_array = np.array(converted_list, dtype=[('i0', int), ('i1', int), ('dist', float), ('num', int)])
print(result_array)
This code first converts the array x into a list of tuples using list(map(tuple, x)), and then converts this list of tuples into a structured NumPy array with specified data types for each element within the tuple.
Answered By - ShadowGunn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.