深度学习中常用numpy操作(1.创建数组)

# -*- coding:utf-8 -*-

import numpy as np

# array creation
zeros = np.zeros([3, 2])
print("zeros:\n", zeros, '\n')
ones = np.ones((2, 3))
print("ones:\n", ones, '\n')
full = np.full([2, 3], 10)
print("full:\n", full, '\n')
eye = np.eye(3)  # 单位矩阵
print("eye:\n", eye, '\n')
random = np.random.random((2, 2))  # 0-1之间
print("random:\n", random, "\n")


x = np.array([[1, 2.0], [0, 0], (1+1j, 3.)])
x = np.array([[1. + 0.j, 2. + 0.j], [0. + 0.j, 0. + 0.j], [1. + 1.j, 3. + 0.j]])
print("x:\n", x, "\n")  # 这两x结果一样,没想到有啥用处

range_int = np.arange(1, 10, dtype=np.float)
print("range_int:", range_int, '\n')
range_dec = np.arange(1, 2, 0.1)
print("range_dec:", range_dec, '\n')
linspace = np.linspace(3, 5, num=5)  # 均分为num-1份
print("linspace:\n", linspace, '\n')

y = np.arange(20).reshape(5, 4)
row, col = np.indices((2, 3))  # 索引,就是下标
cut_indices = y[row ,col]
print("y:\n", y, '\n')
print("row:\n", row, '\n')
print("col:\n", col, '\n')
print("cut_indices\n", cut_indices, '\n')

输出:

zeros:
 [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]] 

ones:
 [[ 1.  1.  1.]
 [ 1.  1.  1.]] 

full:
 [[10 10 10]
 [10 10 10]] 

eye:
 [[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]] 

random:
 [[ 0.55640391  0.79366123]
 [ 0.35533479  0.23360524]] 

x:
 [[ 1.+0.j  2.+0.j]
 [ 0.+0.j  0.+0.j]
 [ 1.+1.j  3.+0.j]] 

range_int: [ 1.  2.  3.  4.  5.  6.  7.  8.  9.] 

range_dec: [ 1.   1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9] 

linspace:
 [ 3.   3.5  4.   4.5  5. ] 

y:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]] 

row:
 [[0 0 0]
 [1 1 1]] 

col:
 [[0 1 2]
 [0 1 2]] 

cut_indices
 [[0 1 2]
 [4 5 6]] 

Ref:https://docs.scipy.org/doc/numpy-dev/user/basics.creation.html

你可能感兴趣的:(深度学习中常用numpy操作(1.创建数组))