python , NumPy : force new arrays to be as type float

标签 python numpy

我时常写这样的代码:

import numpy as np   
a = np.array([1,2,3])
a[1]=3.3
a[2] *= 50
print(a)

这里我没有将 a 初始化为 int,而是初始化为 float,但正如我所说,我忘记了。

现在有没有办法确保此类初始化默认为 float ,除非明确指定 dtype?

最佳答案

不改变来源,不。您的选择是:

养成在数字末尾加点的习惯:

np.array([1.,2.,3.])

明确使用dtype:

np.array([1,2,3], dtype=float)

创建一个新函数:

def ozi_array(*args, **kwargs):
    if 'dtype' not in kwargs:
        return np.array(*args, dtype=float, **kwargs)
    return np.array(*args, **kwargs)

关于 python , NumPy : force new arrays to be as type float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21664922/

相关文章:

python - 如何在 OpenGL 和 pygame 中绘制 3D 网格后的 2D 图像?

python - 如何使用 python 的 telnetlib 在固定时间段内从设备获取数据?

python - 如何使用 CUDA CURAND 保存和恢复随机数生成器的状态?

python - Cython 不够快

python - Python中概率密度函数的更快卷积

python - 如何在 python 代码中指定 “tab”?

python - 在 python 中生成素数 : how is my logic wrong

python - NumPy polydiv,几个变量

python - 如何运行后台进程并且*不*等待?

python - 有与 MATLAB 的 vct2mtx 等效的 Python 吗?