____tz_zs
numpy 提供了多了函数来快速创建不同需求的数组。对于普通的 ndarray 的创建、切片、复制、改变维度和数据类型等操作,见我另一篇博客:https://blog.csdn.net/tz_zs/article/details/73929778
np.empty 构造一个长度为 shape 的未初始化数组,这个数组的元素可能是内存位置上存在的任何数值。
empty(shape, dtype=float, order='C')
# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
# 构造一个长度为 shape 的未初始化数组,这个数组的元素可能是内存位置上存在的任何数值
ndarray = np.empty(shape=3)
print(ndarray)
"""
[ 4.94065646e-324 4.94065646e-324 4.94065646e-324]
"""
ndarray = np.empty(shape=(2, 3))
print(ndarray)
"""
[[ 6.89911851e-310 6.89911851e-310 6.89911705e-310]
[ 2.41806952e-316 6.89911705e-310 6.89911705e-310]]
"""
ndarray = np.empty(shape=(2, 3), dtype=int)
print(ndarray)
"""
[[139639713229752 139639713229752 139639683772968]
[ 48942272 139639683773136 139639683772968]]
"""
ndarray = np.empty(shape=(2, 3), order="F")
print(ndarray)
"""
[[ 6.89911851e-310 6.89911705e-310 6.89911705e-310]
[ 6.89911851e-310 2.41806952e-316 6.89911705e-310]]
"""
ndarray = np.empty(shape=(2, 3), dtype=int, order="F")
print(ndarray)
"""
[[139639713229752 139639683773136 48942272]
[139639683772968 139639713229752 139639683772968]]
"""
np.ones / np.zeros 创建一个全0/1的数组。
numpy.ones(shape, dtype=None, order='C')
numpy.zeros(shape, dtype=float, order='C')
返回给定大小和类型的新数组。shape代表返回的数组的大小(几行几列),dtype是数据类型,order指存储的规则是“c”还是“f”,即行在前和列在前。
官方文档: numpy.ones | numpy.zeros
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[ 1.],
[ 1.]])
>>> np.ones((2,2))
array([[ 1., 1.],
[ 1., 1.]])
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
>>> np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])
>>> np.zeros((2, 1))
array([[ 0.],
[ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
dtype=[('x', '
·
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
a = np.zeros((7, 7), dtype=int)
a[:2, :3] = 1
a[2:4, 2:4] = 2
a[4, 4] = 3
a[:, 5] = 4
a[[0, 2, 4, 6], 6] = 5
print(a)
"""
[[1 1 1 0 0 4 5]
[1 1 1 0 0 4 0]
[0 0 2 2 0 4 5]
[0 0 2 2 0 4 0]
[0 0 0 0 3 4 5]
[0 0 0 0 0 4 0]
[0 0 0 0 0 4 5]]
"""
np.full 构造一个数组,用指定值填充其元素
full(shape, fill_value, dtype=None, order='C')
# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
# 构造一个2x3的数组,其中元素全部都为 7
ndarray = np.full((2, 3), 7)
print(ndarray)
"""
[[7 7 7]
[7 7 7]]
"""
np.titl() 扩展
np.tile(A, reps)
# -*- coding: utf-8 -*-
"""
@author: tz_zs
np.titl() 扩展
"""
import numpy as np
n1 = np.arange(0, 40, 10)
print(n1) # [ 0 10 20 30]
n2 = np.tile(n1, (3, 5)) # 扩展
print(n2)
'''
[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
'''
np.r_
官方文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html
#普通的拼接数组
X = np.r_[[1,2,3],[4,5,6],[7,8,9]]
print X
#第一个字符串控制组合
。。。
np.linspace 主要用于间隔采样,在指定的间隔内返回均匀间隔的数字
官方文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
n1 = np.linspace(-5, 5, 5)
print(n1)
# [-5. - 2.5 0. 2.5 5.]
n2 = np.linspace(-5, 5, 5, False)
print(n2)
# [-5. - 3. - 1. 1. 3.]
n1 = np.linspace(start=-5, stop=5, num=5, endpoint=True, retstep=True)
print(n1)
# (array([-5. , -2.5, 0. , 2.5, 5. ]), 2.5)
n2 = np.linspace(start=-5, stop=5, num=5, endpoint=True, retstep=True, dtype=int)
print(n2)
# (array([-5, -2, 0, 2, 5]), 2.5)
end