引入import numpy as np本笔记整理自 udacity 课程,版权归 udacity 所有, 更多信息请访问 Udacity
在此引入一次,下面直接使用 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输出:
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输出:
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输出:
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输出为:
X =[[10 0 0 0] [ 0 20 0 0] [ 0 0 30 0] [ 0 0 0 50]]输出:
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输出:
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.arange() 函数一样),方法是在 np.linspace() 函数中将关键字 endpoint 设为 False 。
x = np.linspace(0,25,10, endpoint = False)输出:
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使用 np.random.random(shape) 函数创建具有给定形状的 ndarray, 包含位于半开区间 [0.0, 1.0) 内的随机浮点数。
X = np.random.random((3,3))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.12379926 0.52943854 0.3443525 ] [ 0.11169547 0.82123909 0.52864397] [ 0.58244133 0.21980803 0.69026858]]X has dimensions: (3, 3)X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64使用np.random.randint(start, stop, size = shape)创建由特定区间内的随机整数构成的 ndarray, 其中包含在半开区间 [start, stop) 内的随机整数。
X = np.random.randint(4,15,size=(3,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 = [[ 7 11] [ 9 11] [ 6 7]]X has dimensions: (3, 2)X is an object of type: class 'numpy.ndarray' The elements in X are of type: int64使用 np.random.normal(mean, standard deviation, size=shape) 创建由满足特定统计学特性的随机数字组成的 ndarray
X = np.random.normal(0, 0.1, size=(1000,1000))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)print('The elements in X have a mean of:', X.mean())print('The maximum value in X is:', X.max())print('The minimum value in X is:', X.min())print('X has', (X < 0).sum(), 'negative numbers')print('X has', (X > 0).sum(), 'positive numbers')输出:
X =[[ 0.04218614 0.03247225 -0.02936003 ..., 0.01586796 -0.05599115 -0.03630946] [ 0.13879995 -0.01583122 -0.16599967 ..., 0.01859617 -0.08241612 0.09684025] [ 0.14422252 -0.11635985 -0.04550231 ..., -0.09748604 -0.09350044 0.02514799] ..., [-0.10472516 -0.04643974 0.08856722 ..., -0.02096011 -0.02946155 0.12930844] [-0.26596955 0.0829783 0.11032549 ..., -0.14492074 -0.00113646 -0.03566034] [-0.12044482 0.20355356 0.13637195 ..., 0.06047196 -0.04170031 -0.04957684]]X has dimensions: (1000, 1000)X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64The elements in X have a mean of: -0.000121576684405The maximum value in X is: 0.476673923106The minimum value in X is: -0.499114224706 X 具有 500562 个负数 X 具有 499438 个正数说明:
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |