Python数据分析numpy入门-------numpy100题练习

Posted Geek_bao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python数据分析numpy入门-------numpy100题练习相关的知识,希望对你有一定的参考价值。

Python数据分析基础

二、numpy100题练习

1.Import the numpy package under the name np (★☆☆)。
导入numpy库并简写为 np

代码如下:

import numpy as np  


2. Print the numpy version and the configuration (★☆☆)
打印numpy的版本和配置说明

代码如下:

import numpy as np  
print(np.__version__)
print(np.show_config())

输出结果如下:

1.19.1
blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_info']
    libraries = ['openblas_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_info']
    libraries = ['openblas_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_lapack_info']
    libraries = ['openblas_lapack_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_lapack_info']
    libraries = ['openblas_lapack_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
None

Process finished with exit code 0


3.Create a null vector of size 10 (★☆☆)
创建一个长度为10的空向量

代码如下:

Z = np.zeros(10)
print(Z)

输出结果如下:

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]


4. How to find the memory size of any array (★☆☆)
如何找到任何一个数组的内存大小

代码如下:

Z = np.zeros((10, 10))
print("%d bytes" % (Z.size * Z.itemsize))

输出结果如下:

800 bytes


5. How to get the documentation of the numpy add function from the command line? (★☆☆)
如何从命令行得到numpy中add函数的说明文档?

代码如下:

cmd命令行用如下命令:
python -c "import numpy; numpy.info(numpy.add)"
python程序中使用如下代码:
print(np.info(np.add))

输出结果如下:

add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Add arguments element-wise.

Parameters
----------
x1, x2 : array_like
    The arrays to be added.
    If ``x1.shape != x2.shape``, they must be broadcastable to a common
    shape (which becomes the shape of the output).
out : ndarray, None, or tuple of ndarray and None, optional
    A location into which the result is stored. If provided, it must have
    a shape that the inputs broadcast to. If not provided or None,
    a freshly-allocated array is returned. A tuple (possible only as a
    keyword argument) must have length equal to the number of outputs.
where : array_like, optional
    This condition is broadcast over the input. At locations where the
    condition is True, the `out` array will be set to the ufunc result.
    Elsewhere, the `out` array will retain its original value.
    Note that if an uninitialized `out` array is created via the default
    ``out=None``, locations within it where the condition is False will
    remain uninitialized.
**kwargs
    For other keyword-only arguments, see the
    :ref:`ufunc docs <ufuncs.kwargs>`.

Returns
-------
add : ndarray or scalar
    The sum of `x1` and `x2`, element-wise.
    This is a scalar if both `x1` and `x2` are scalars.

Notes
-----
Equivalent to `x1` + `x2` in terms of array broadcasting.

Examples
--------
>>> np.add(1.0, 4.0)
5.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.add(x1, x2)
array([[  0.,   2.,   4.],
       [  3.,   5.,   7.],
       [  6.,   8.,  10.]])


6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
创建一个长度为10并且除了第五个值为1的空向量

代码如下:

x = np.zeros(10)
x[4] = 1
print(x)

输出结果如下:

[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

7.Create a vector with values ranging from 10 to 49 (★☆☆)
创建一个值域范围从10到49的向量

代码如下:

x = np.arange(10, 50)
print(x)

输出结果如下:

[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]


8. Reverse a vector (first element becomes last) (★☆☆)
反转一个向量(第一个元素变为最后一个)

代码如下:

x = np.arange(50)
x = x[::-1]  # 当步长为 -1 即反向走时,应该从下标大的走向下标小的,开始反转
print(x)

输出结果如下:

[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2
  1  0]


9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
创建一个 3x3 并且值从0到8的矩阵

代码如下:

x = np.arange(9).reshape(3, 3)
print(x)

输出结果如下:

[[0 1 2]
 [3 4 5]
 [6 7 8]]


10.Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
找到数组[1,2,0,0,4,0]中非0元素的位置索引

代码如下:

x = np.nonzero([1, 2, 0, 0, 4, 0])
print(x)

输出结果如下:

(array([0, 1, 4], dtype=int64),)


11.Create a 3x3 identity matrix (★☆☆)
​创建3x3的对角矩阵

代码如下:

x = np.eye(3)
print(x)

输出结果如下:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

12.Create a 3x3x3 array with random values (★☆☆)
​创建一个 3x3x3的随机数组

代码如下:

x = np.random.random((3, 3, 3))
print(x)

输出结果如下:

[[[0.90645099 0.39055025 0.13326805]
  [0.82727578 0.80306489 0.73626431]
  [0.86937515 0.93136459 0.05317126]]

 [[0.57875347 0.6657227  0.24202423]
  [0.41064389 0.66225554 0.32342978]
  [0.22953023 0.25789695 0.3861879 ]]

 [[0.7715045  0.80144285 0.09692252]
  [0.64913309 0.21699249 0.42576076]
  [0.19170728 0.40928135 0.68654943]]]

Process finished with exit code 0

13.Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
创建一个 10x10 的随机数组并找到它的最大值和最小值

代码如下:

import numpy as np
x = np.random.random((10, 10))
print(x.min())
print(x.max())

输出结果如下:

0.004546019288869885
0.9988575624248186

14.Create a random vector of size 30 and find the mean value (★☆☆)
​创建一个长度为30的随机向量并找到它的平均值

代码如下:

x = np.random.random(30)
print(x.mean())

输出结果如下:

0.5570963332977725

15.Create a 2d array with 1 on the border and 0 inside (★☆☆)
​创建一个二维数组,其中边界值为1,其余值为0

代码如下:

x = np.ones((10, 10))
x[1:-1, 1:-1] = 0
print(x)

输出结果如下:

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

Process finished with exit code 0


16.How to add a border (filled with 0’s) around an existing array? (★☆☆)
​对于一个已知数组,如何添加一个用0填充的边界

代码如下:

x = np.ones((5, 5))
x = np.pad(x, pad_width=1, mode='constant', constant_values=0)
print(x)

输出结果如下:

[[0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

说明:np.pad(array, pad_width, mode, kwargs)
顾名思义,用于给数组array扩充新的行或者列。
参数意义:
array:要填充的对象
pad_width: 各个方向上填充的维度
pad_width = ((1,2), (2,2)) 指第一维(此时为行)上面填充一位、下面填充两位;第二维(此时为列)左边填充两位、右边填充两位。
mode:用于指定填充内容。

17.What is the result of the following expression? (★☆☆)
以下表达式运行的结果分别是什么

代码如下:

print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)

输出结果如下:

nan
False
False
nan
True
False

说明:
nan与任何值进行运算都是 nan;
nan:not a number 表示不是一个数字,属于浮点类;
inf:np.inf 表示正无穷,-np.inf表示负无穷,属于浮点类;
两个nan是不相等的

18.Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
​创建一个 5x5的矩阵,并设置值1,2,3,4落在其对角线下方位置

代码如下:

x = np.diag(1 + np.arange(4), k=-1)
print(x)

输出结果如下:

[[0 0 0 0 0]
 [1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]]

说明:
numpy.diag(v,k=0)。以一维数组的形式返回方阵的对角线(或非对角线)元素,或将一维数组转换成方阵(非对角线元素为0).两种功能角色转变取决于输入的v。
参数详解:
v : array_like
如果v是2D数组,返回k位置的对角线。
如果v是1D数组,返回一个v作为k位置对角线的2维数组。
k : int, optional
对角线的位置,大于零位于对角线上面,小于零则在下面。

19.Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
​创建一个8x8 的矩阵,并且设置成棋盘样式

代码如下:

x = np.zeros((8, 8), dtype=int)
x[1::2, ::2] = 1
x[::2, 1::2] = 1
print(x)

输出结果如下:

[[0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]]

Process finished with exit code 0


说明:x[1::2, ::2] = 1。第一个切片表示行的数据变换,第二个表示第几行数据变换。1::2表示行的变化,从索引1开始,步长为2,赋值为1。后面::2,表示从第0行开始,步长为2,执行此赋值。
注意:格式b = a[i:j:s]
这里的s表示步进,缺省为1.(-1时即翻转读取)。
所以a[i:j:1]相当于a[i:j]。当s<0时,i缺省时,默认为-1. j缺省时,默认为-len(a)-1。所以a[::-1]相当于 a[-1:-len(a)-1:-1],也就是从最后一个元素到第一个元素复制一遍。

20.Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
考虑一个 (6,7,8) 形状的数组,其第100个元素的索引(x,y,z)是什么?

代码如下:

print(np.unravel_index(100, (6, 7, 8)))

输出结果如下:

(1, 5, 4)

说明:np.unravel_index(indices, shape, order = ‘C’)
一句话概括:求出数组某元素(或某组元素)拉成一维后的索引值在原本维度(或指定新维度)中对应的索引。
参数说明:
indices: 整数构成的数组, 其中元素是索引值(integer array whose elements are indices into flattened version of array)
shape: tuple of ints, 一般是原本数组的维度,也可以给定的新维度。

21.Create a checkerboard 8x8 matrix using the tile function (★☆☆)
用tile函数去创建一个 8x8的棋盘样式矩阵

代码如下:

x = np.tile(np.array([[0, 1], [1, 0]]), (4, 4))
print(x)

输出结果如下:

[[0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]]

Process finished with exit code 0

说明:np.tile(a,(2,1)),tile有平铺的意思,顾名思义。第一个参数为Y轴(纵向)扩大倍数,第二个为X轴(横向)扩大倍数。本例中X轴扩大一倍便为不复制。

22.Normalize a 5x5 random matrix (★☆☆)
对一个5x5的随机矩阵做归一化

代码如下:

x = np.random.random((5, 5))
x = (x-np.mean(x))/(np.std(x))
print(x)

输出结果如下:

[[ 0.59892254 -0.60850328 -1.67865401 -科学计算工具Numpy

python中的numpy运算

Python数据分析numpy入门

Python numpy 入门系列 12 算术函数

python数据分析与展示Numpy入门

Python数据分析numpy入门-------numpy100题练习