gpt4 book ai didi

python - 在python中更改数组的数据类型

转载 作者:行者123 更新时间:2023-11-28 20:05:34 25 4
gpt4 key购买 nike

我通过生成一组随机数并将它们转换为 int 类型来创建一个数组。但是,我不认为我下面的方法是有效的。是否有更改数组中数据类型的最佳方法?

# standard normal distributed random numbers
c=random.randn(5,5)
c

array([[-0.37644781, -0.81347483, -0.36895952, -2.68702544, -0.96780752],
[ 0.05293328, 1.65260753, 0.55586611, -0.5786392 , 0.50865003],
[ 1.25508358, 0.51783276, 2.36435212, -0.23484705, -1.20999296],
[ 2.07552172, 0.65506648, 0.10231436, -0.26046045, 0.40148111],
[ 0.24864496, -1.8852587 , -2.51091886, 1.01106003, 1.53118353]])

d=array([[-0.37644781, -0.81347483, -0.36895952, -2.68702544, -0.96780752],
[ 0.05293328, 1.65260753, 0.55586611, -0.5786392 , 0.50865003],
[ 1.25508358, 0.51783276, 2.36435212, -0.23484705, -1.20999296],
[ 2.07552172, 0.65506648, 0.10231436, -0.26046045, 0.40148111],
[ 0.24864496, -1.8852587 , -2.51091886, 1.01106003, 1.53118353]],dtype=int)

d
array([[ 0, 0, 0, -2, 0],
[ 0, 1, 0, 0, 0],
[ 1, 0, 2, 0, -1],
[ 2, 0, 0, 0, 0],
[ 0, -1, -2, 1, 1]])

最佳答案

要么找到一种方法来获得具有正确类型的输出,要么使用 astype,请参阅 docs , 为了改变数组的类型

在您的情况下,以下示例为您提供类型为 np.int

的数组
c=random.randn(5,5).astype(np.int)

关于python - 在python中更改数组的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com