numpy学习之创建数组

1.使用array函数创建数组

import numpy as np
ndarray1 = np.array([1, 2, 3])
array([1, 2, 3])
ndarray2 = np.array(list('abcd'))
array(['a', 'b', 'c', 'd'],
      dtype='<U1')
ndarray3 = np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

2.zeros和zeros_like创建数组

用于创建数组,数组元素默认值是0. 注意:zeros_like函数只是根据传入的ndarray数组的shape来创建所有元素为0的数组,并不是拷贝源数组中的数据

ndarray1 = np.zeros(6)
ndarray2 = np.zeros((2, 3))
ndarray3 = np.zeros_like(ndarray2)  # 按照 ndarray2 的shape创建数组
print("数组类型:")
print('ndarray1:', type(ndarray1))
print('ndarray2:', type(ndarray2))
print('ndarray3:', type(ndarray3))print("数组元素类型:")
print('ndarray1:', ndarray1.dtype)
print('ndarray2:', ndarray2.dtype)
print('ndarray3:', ndarray3.dtype)print("数组形状:")
print('ndarray1:', ndarray1.shape)
print('ndarray2:', ndarray2.shape)
print('ndarray3:', ndarray3.shape)

输出结果:
数组类型:
ndarray1: <class 'numpy.ndarray'>
ndarray2: <class 'numpy.ndarray'>
ndarray3: <class 'numpy.ndarray'>
数组元素类型:
ndarray1: float64
ndarray2: float64
ndarray3: float64
数组形状:
ndarray1: (6,)
ndarray2: (2, 3)
ndarray3: (2, 3)

3.ones和ones_like创建数组

与zero类似

# 创建数组,元素默认值是0
ndarray1 = np.ones(7)
ndarray2 = np.ones((2, 3))
# 修改元素的值
ndarray2[0][1] = 4
ndarray3 = np.ones_like(ndarray2)  # 按照 ndarray2 的shape创建数组
# 打印数组元素类型
print("数组类型:")
print('ndarray1:', type(ndarray1))
print('ndarray2:', type(ndarray2))
print('ndarray3:', type(ndarray3))print("数组元素类型:")
print('ndarray1:', ndarray1.dtype)
print('ndarray2:', ndarray2.dtype)
print('ndarray3:', ndarray3.dtype)print("数组形状:")
print('ndarray1:', ndarray1.shape)
print('ndarray2:', ndarray2.shape)
print('ndarray3:', ndarray3.shape)

输出结果:
数组类型:
ndarray1: <class 'numpy.ndarray'>
ndarray2: <class 'numpy.ndarray'>
ndarray3: <class 'numpy.ndarray'>
数组元素类型:
ndarray1: float64
ndarray2: float64
ndarray3: float64
数组形状:
ndarray1: (7,)
ndarray2: (2, 3)
ndarray3: (2, 3)

4.empty和empty_like创建数组

用于创建空数组,空数据中的值并不为0,而是未初始化的随机值.

ndarray1 = np.empty(5)
ndarray2 = np.empty((2, 3))
ndarray3 = np.empty_like(ndarray1)
# 打印数组元素类型
print("数组类型:")
print('ndarray1:', type(ndarray1))
print('ndarray2:', type(ndarray2))
print('ndarray3:', type(ndarray3))print("数组元素类型:")
print('ndarray1:', ndarray1.dtype)
print('ndarray2:', ndarray2.dtype)
print('ndarray3:', ndarray3.dtype)print("数组形状:")
print('ndarray1:', ndarray1.shape)
print('ndarray2:', ndarray2.shape)
print('ndarray3:', ndarray3.shape)

输出结果:
数组类型:
ndarray1: <class 'numpy.ndarray'>
ndarray2: <class 'numpy.ndarray'>
ndarray3: <class 'numpy.ndarray'>
数组元素类型:
ndarray1: float64
ndarray2: float64
ndarray3: float64
数组形状:
ndarray1: (5,)
ndarray2: (2, 3)
ndarray3: (5,)

5.arange函数创建数组

arange函数是python内置函数range函数的数组版本

ndarray1 = np.arange(10)
print("ndarray1:",ndarray1)
ndarray2 = np.arange(10, 20)
print("ndarray2:",ndarray2)
ndarray3 = np.arange(10, 20, 2)
print("ndarray3:",ndarray3)

输出结果:
ndarray1: [0 1 2 3 4 5 6 7 8 9]
ndarray2: [10 11 12 13 14 15 16 17 18 19]
ndarray3: [10 12 14 16 18]

6.eye创建对角矩阵数组

该函数用于创建一个N*N的矩阵,对角线为1,其余为0.

ndarray1 = np.eye(3)
ndarray1
输出结果:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

 

posted @ 2019-02-13 16:28  GreatAnt  阅读(6486)  评论(0编辑  收藏  举报