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

本文详细介绍了使用numpy库解决100个数据分析问题的代码实践,涵盖数组创建、操作、统计计算、矩阵乘法、数据处理等多个方面,帮助读者深入理解numpy在Python数据分析中的应用。
摘要由CSDN通过智能技术生成

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 -0.46662041  0.41723656]
 [-1.05768562 -1.02476268 -0.04168865 -0.44227418 -0.17553817]
 [-1.45474513  0.41247651 -0.28695422 -0.09156237  0.00757391]
 [-1.36155231 -0.08620354  0.538985    1.18103188 -0.59021046]
 [ 2.33106887 -0.45919266  1.54971195  1.8721533   0.91698717]]

说明:np.std(x)计算总体标准差。


23.Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
创建一个将颜色描述为(RGBA)四个无符号字节的自定义dtype?

代码如下:

color = np.dtype([("r", np.ubyte, 1),
                 ("g", np.ubyte, 1),
                 ("b", np.ubyte, 1),
                  ("a", np.ubyte, 1)])


24.Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
一个5x3的矩阵与一个3x2的矩阵相乘,实矩阵乘积是什么?

代码如下:

x = np.dot(np.ones((5, 3)), np.ones((3, 2)))
print(x)
或者使用下面代码:
x = np.ones((5, 3)) @ np.ones((3,2))   # python3.5以上版本
print(x)

输出结果如下:

[[3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]]


25.Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
给定一个一维数组,对其在3到8之间的所有元素取反

代码如下:

x = np.arange(11)
x[(3 < x) & (x <= 8)] *= -1
print(x)

输出结果如下:

[ 0  1  2  3 -4 -5 -6 -7 -8  9 10]


26.What is the output of the following script? (★☆☆)
下面脚本运行后的结果是什么?

代码如下:

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))

输出结果如下:

9
10

说明:

  • numpy.sum()签名如下(省略一些参数):numpy.sum(a, axis=None, dtype=None, out=None, …)
  • Python的sum签名:sum(iterable, start=0)
    sum对提供的可迭代对象进行迭代,对值求和,然后加-1(即减1)。 numpy.sum只是将提供的iterable中的所有值求和,并接收一个axis参数1。

27.Consider an integer vector Z, which of these expressions are legal? (★☆☆)
考虑一个整数向量Z,下列表达合法的是哪个

代码如下:

Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

解答:写个例子一个个试试。

Z = np.arange(5)
Z**Z    # legal

Z = np.arange(5)
2 << Z >> 2   # legal


Z = np.arange(5)
Z <- Z    # legal

Z = np.arange(5)
1j*Z      # legal

Z = np.arange(5)
Z/1/1     # legal

Z = np.arange(5)
Z<Z>Z      # false
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


28.What are the result of the following expressions?
下列表达式的结果分别是什么?

代码如下:

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)

输出结果如下:

nan
0
[-2.14748365e+09]

29.How to round away from zero a float array ? (★☆☆)
如何从零位对浮点数组做舍入

代码如下:

x = np.random.uniform(-10, +10, 10)
print(np.copysign(np.ceil(np.abs(x)), x))

输出结果如下:

[  5.   4. -10.   7.  -9.   2.   6.   8.   6.  -1.]

说明:np.copysign(x1, x2)按元素将 x1 的符号更改为 x2 的符号。

30.How to find common values between two arrays? (★☆☆)
如何找到两个数组中的共同元素?

代码如下:

x = np.random.randint(0, 10, 10)
y = np.random.randint(0, 10, 10)
print(np.intersect1d(x, y))

输出结果如下:

[0 3 6 8 9]

说明:numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)[source]
返回两个数组中共同的元素。

  • ar1, ar2 : array_like
    Input arrays. Will be flattened if not already 1D.

  • assume_unique : bool
    If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.
    默认是False,如果是True,假定输入的数组中元素

  • return_indices : bool
    If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple. Default is False.默认是False,如果是True,返回共同元素的索引位置,因为返回共同元素是排序后的,所以索引位置是排序后的元素位置。如果共同元素在一个数组中有多次出现,只返回第一次出现的索引位置


31.How to ignore all numpy warnings (not recommended)? (★☆☆)
如何忽略所有的 numpy 警告(尽管不建议这么做)

代码如下:

defaults = np.seterr(all="ignore")
Z = np.ones(1) / 0


32. Is the following expressions true? (★☆☆)
下面的表达式是正确的吗?

代码如下:

np.sqrt(-1) == np.emath.sqrt(-1)

print(np.sqrt(-1) == np.emath.sqrt(-1))

输出结果如下:

False


33.How to get the dates of yesterday, today and tomorrow? (★☆☆)
如何得到昨天,今天,明天的日期

代码如下:

yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
today = np.datetime64('today', 'D')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
print("yesterday is " + str(yesterday))
print('today is ' + str(today))
print('tomorrow is ' + str(tomorrow))

输出结果如下:

yesterday is 2021-06-25
today is 2021-06-26
tomorrow is 2021-06-27


34.How to get all the dates corresponding to the month of July 2016? (★★☆)
如何得到所有与2016年7月对应的所有日期

代码如下:

x = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
print(x)

输出结果如下:

['2016-07-01' '2016-07-02' '2016-07-03' '2016-07-04' '2016-07-05'
 '2016-07-06' '2016-07-07' '2016-07-08' '2016-07-09' '2016-07-10'
 '2016-07-11' '2016-07-12' '2016-07-13' '2016-07-14' '2016-07-15'
 '2016-07-16' '2016-07-17' '2016-07-18' '2016-07-19' '2016-07-20'
 '2016-07-21' '2016-07-22' '2016-07-23' '2016-07-24' '2016-07-25'
 '2016-07-26' '2016-07-27' '2016-07-28' '2016-07-29' '2016-07-30'
 '2016-07-31']

Process finished with exit code 0


35.How to compute ((A+B)(-A/2)) in place (without copy)? (★★☆)
如何直接在位计算(A+B)
(-A/2)(不建立副本)

代码如下:

A = np.ones(3) * 1
B = np.ones(3) * 2
C = np.ones(3) * 3
np.add
  • 17
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值