黑马程序员技术交流社区

标题: 【上海校区】详解使用内置函数创建ndarray [打印本页]

作者: 不二晨    时间: 2018-7-16 10:48
标题: 【上海校区】详解使用内置函数创建ndarray
本笔记整理自 udacity 课程,版权归 udacity 所有, 更多信息请访问 Udacity
引入import numpy as np

在此引入一次,下面直接使用 np

使用 np.zeros(shape) 创建X = np.zeros((3,4))print()print('X = \n', X) print()print('X has dimensions:', X.shape)print('X is an object of type:', type(X))print('The elements in X are of type:', X.dtype)

输出:

X =[[ 0. 0. 0. 0.] [ 0. 0. 0. 0.] [ 0. 0. 0. 0.]]X has dimensions: (3, 4)X is an object of type: class 'numpy.ndarray'The elements in X are of type: float64使用 np.ones(shape) 创建X = np.ones((3,2), dtype = np.int64) # 此处指定类型print()print('X = \n', X)print()print('X has dimensions:', X.shape)print('X is an object of type:', type(X))print('The elements in X are of type:', X.dtype)

输出:

X =  [[1 1] [1 1] [1 1]]X has dimensions: (3, 2)X is an object of type: <class 'numpy.ndarray'>The elements in X are of type: int64使用 np.full(shape, constant value) 创建X = np.full((2,3), 5) print()print('X = \n', X)print()print('X has dimensions:', X.shape)print('X is an object of type:', type(X))print('The elements in X are of type:', X.dtype)

输出:

X =[[5 5 5] [5 5 5]]X has dimensions: (2, 3)X is an object of type: class 'numpy.ndarray'The elements in X are of type: int64使用 np.eye(N) 创建 单位矩阵X = np.eye(5)print()print('X = \n', X)print()print('X has dimensions:', X.shape)print('X is an object of type:', type(X))print('The elements in X are of type:', X.dtype)

输出:

X =[[ 1. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0.] [ 0. 0. 1. 0. 0.] [ 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 1.]]X has dimensions: (5, 5)X is an object of type: class 'numpy.ndarray'The elements in X are of type: float64使用 np.diag() 创建 对角矩阵X = np.diag([10,20,30,50])print()print('X = \n', X)print()

输出为:

X =[[10 0 0 0] [ 0 20 0 0] [ 0 0 30 0] [ 0 0 0 50]]使用 np.arange(start,stop,step) 创建在给定区间内值均匀分布的 ndarrayx = np.arange(1,14,3)print()print('x = ', x)print()print('x has dimensions:', x.shape)print('x is an object of type:', type(x))print('The elements in x are of type:', x.dtype)

输出:

x = [ 1 4 7 10 13]x has dimensions: (5,)x is an object of type: class 'numpy.ndarray'The elements in x are of type: int64使用 np.linspace(start,stop,step) 创建在给定区间内值均匀分布的 ndarrayx = np.linspace(0,25,10)print()print('x = \n', x)print()print('x has dimensions:', x.shape)print('x is an object of type:', type(x))print('The elements in x are of type:', x.dtype)

输出:

x =  [ 0.          2.77777778  5.55555556  8.33333333 11.11111111 13.88888889 16.66666667 19.44444444 22.22222222 25.        ]x has dimensions: (10,)x is an object of type: <class 'numpy.ndarray'>The elements in x are of type: float64使用 np.reshape(ndarray, new_shape) 创建秩为 2 的任何形状 ndarrayx = np.arange(20)print()print('Original x = ', x)print()x = np.reshape(x, (4,5))print()print('Reshaped x = \n', x)print()print('x has dimensions:', x.shape)print('x is an object of type:', type(x))print('The elements in x are of type:', x.dtype)

输出:

Original x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]Reshaped x =[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]]x has dimensions: (4, 5)x is an object of type: class 'numpy.ndarray'The elements in x are of type: int64

同样,我们也可以使用 reshape() 与 np.linspace() 创建秩为 2 的数组。

X = np.linspace(0,50,10, endpoint=False).reshape(5,2)print()print('X = \n', X)print()print('X has dimensions:', X.shape)print('X is an object of type:', type(X))print('The elements in X are of type:', X.dtype)

输出:

X = [[ 0. 5.] [ 10. 15.] [ 20. 25.] [ 30. 35.] [ 40. 45.]]X has dimensions: (5, 2)X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64创建随机 ndarray


【转载】        https://blog.csdn.net/Tyro_java/article/details/81052052



作者: 不二晨    时间: 2018-7-16 11:46
奈斯
作者: wuqiong    时间: 2018-7-16 14:19

作者: 吴琼老师    时间: 2018-7-18 14:31

作者: 梦缠绕的时候    时间: 2018-7-18 15:41





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2