14.Numpy之数组创建方法

# NumPy之 数组创建

import numpy as np


def print_array(a):
    print(a)
    print('array dimensions is %d' % (a.ndim))
    print('array shape is', a.shape)
    print('array size is %d' % (a.size))
    print('Data type of array is %s ' % (str(a.dtype)))


# ===================================================
# 创建一维数组 by 列表(list)
print('create array by list:')
a_list = [1, 2, 3]
a_list_arr = np.array(a_list)
print_array(a_list_arr)
# output:
# create array by list:
# [1 2 3]
# array dimensions is 1
# array shape is (3,)
# array size is 3
# Data type of array is int32

# 创建一维数组 by 元组(tuple)
print('create array by tuple:')
a_tuple = (4, 5, 6)
a_tuple_arr = np.array(a_tuple)
print_array(a_tuple_arr)
# output:
# create array by tuple:
# [4 5 6]
# array dimensions is 1
# array shape is (3,)
# array size is 3
# Data type of array is int32

# ===================================================
# 不要用set创建数组,得不到想要的数组
print('Don\'t create array by set,you will not get what you want:')
a_set = {4, 5, 6}
a_set_arr = np.array(a_set)
print_array(a_set_arr)
# output:
# Don't create array by set,you will not get what you want:
# {4, 5, 6}
# array dimensions is 0
# array shape is ()
# array size is 1
# Data type of array is object


# ===================================================
# 创建数组时,可以显示指定数组数据类型

a_list_of_list = [[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]]]
complex_array = np.array(a_list_of_list, dtype=complex)
print_array(complex_array)
# output:
# [[[  0.+0.j   1.+0.j]
#   [  2.+0.j   3.+0.j]]
#
#  [[  4.+0.j   5.+0.j]
#   [  6.+0.j   7.+0.j]]
#
#  [[  8.+0.j   9.+0.j]
#   [ 10.+0.j  11.+0.j]]]
# array dimensions is 3
# array shape is (3, 2, 2)
# array size is 12
# Data type of array is complex128

# ===================================================
# 有些情况不知道数组元素的值,但是知道元素的数据类型,可以提前创建带有初始值的数组
# 创建方式如下:
# 生成全1初始值数组
one_array = np.ones((3, 2, 2))
print_array(one_array)
# output:
# [[[ 1.  1.]
#   [ 1.  1.]]
#
#  [[ 1.  1.]
#   [ 1.  1.]]
#
#  [[ 1.  1.]
#   [ 1.  1.]]]
# array dimensions is 3
# array shape is (3, 2, 2)
# array size is 12
# Data type of array is float64

# 生成带初始值的数组时,同样可以指定数据类型,默认是float
# 生成全零初始值数组
zero_array = np.zeros((3, 3), dtype=int)
print_array(zero_array)
# output:
# [[0 0 0]
#  [0 0 0]
#  [0 0 0]]
# array dimensions is 2
# array shape is (3, 3)
# array size is 9
# Data type of array is int32

# 生成随机初始值的数组
empty_array = np.empty((3, 3))
print_array(empty_array)

# output:
# [[  0.00000000e+000   0.00000000e+000   0.00000000e+000]
#  [  0.00000000e+000   0.00000000e+000   2.31222722e-321]
#  [  0.00000000e+000   0.00000000e+000   0.00000000e+000]]
# array dimensions is 2
# array shape is (3, 3)
# array size is 9
# Data type of array is float64


# ===================================================
# 生成连续数组
arange_arr = np.arange(0, 100, 5)
print(arange_arr)  # [ 0  5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]
arange_arr = arange_arr.reshape(2, 2, 5)
print(arange_arr)
# output:
# [[[ 0  5 10 15 20]
#   [25 30 35 40 45]]
#
#  [[50 55 60 65 70]
#   [75 80 85 90 95]]]

# 生成数为浮点数时,推荐使用下面生成方式
linspace_arr = np.linspace(0, 2, 9)
print(linspace_arr)  # [ 0.    0.25  0.5   0.75  1.    1.25  1.5   1.75  2.  ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值