我有以下np.chararray
array = np.chararray(shape=(5))
initialize_array(array)
# Gives out the following array of type S1
# [b'1' b'0' b'1' b'0' b'1']
如何将这个数组转换为nd数组?我希望有一个数组,如
[ 1 0 1 0 1 ] # dtype = int
是否可以通过我不知道的某些功能?还是应该“手工”完成?
使用astype像:
new_ndarray = array.astype("int")
引发ValueError:
ValueError:只能从字符串数据创建字符数组。
您可以做:
import numpy as np
array = np.chararray(shape=(5))
array[:] = [b'1', b'0', b'1', b'0', b'1']
array_int = np.array(array, dtype=np.int32)
print(array_int)
# [1 0 1 0 1]