task2

本文详细介绍了numpy库的基本操作,包括创建向量、矩阵,获取内存大小,查看函数文档,以及各种数学运算如求平均值、最大值、最小值、归一化、矩阵乘法、相乘、取反、排序等。同时,还涉及到数组的索引、切片、单位矩阵、随机数生成、特殊矩阵构造和数组类型转换等内容,是numpy入门与进阶的实用教程。
摘要由CSDN通过智能技术生成
  1. 导入numpy库并简写为 np
import numpy as np
  1. 打印numpy的版本和配置说明
print(np.__version__)
np.show_config()
1.15.4
mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/anaconda3/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/anaconda3/include']
blas_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/anaconda3/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/anaconda3/include']
blas_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/anaconda3/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/anaconda3/include']
lapack_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/anaconda3/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/anaconda3/include']
lapack_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/anaconda3/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/anaconda3/include']
  1. 创建一个长度为10的空向量
Z=np.zeros(10)
print (Z)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
  1. 如何找到任何一个数组的内存大小?
Z=np.zeros((10,10))
print ("%d bytes" %(Z.size*Z.itemsize))

800 bytes
  1. 如何从命令行得到numpy中add函数的说明文档?
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 may be the shape of one or
    the other).
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
    Values of True indicate to calculate the ufunc at that position, values
    of False indicate to leave the value in the output alone.
**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.]])
  1. 创建一个长度为10并且除了第五个值为1的空向量 (
Z = np.zeros(10)
Z[4] = 1#array[n]
print(Z)
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
  1. 创建一个值域范围从10到49的向量
z=np.arange(10,50)
print (z)
[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]
  1. 反转一个向量(第一个元素变为最后一个
z=np.arange(10,50)
a=z[::-1]
print (a)
[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]
  1. 创建一个 3x3 并且值从0到8的矩阵
z=np.arange(9).reshape(3,3)
print (z)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
  1. 找到数组[1,2,0,0,4,0]中非0元素的位置索引
nz=np.nonzero([1,2,0,0,4,0])#nonzero找出非零元素的索引
print (nz)
(array([0, 1, 4]),)
  1. 创建一个 3x3 的单位矩阵
n=np.eye(3,3)
print (n)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  1. 创建一个 3x3x3的随机数组
z=np.random.random((3,3,3))
print (z)
[[[0.38555942 0.03967291 0.11691279]
  [0.14450181 0.84619474 0.96687301]
  [0.84301805 0.32825705 0.91466083]]

 [[0.62835269 0.1170878  0.54229367]
  [0.87376554 0.41814426 0.76473372]
  [0.18220092 0.61691598 0.58157413]]

 [[0.60500251 0.55338976 0.19846201]
  [0.95039281 0.7587364  0.51526201]
  [0.710684   0.33470073 0.38118964]]]
  1. 创建一个 10x10 的随机数组并找到它的最大值和最小值
z=np.random.random((10,10))
print (z.min(),z.max())
0.015826515392604934 0.9991998936904795
  1. 创建一个长度为30的随机向量并找到它的平均值
z=np.random.random(30)
m=z.mean()
print (m)
0.47166627486012536
  1. 创建一个二维数组,其中边界值为1,其余值为0
z=np.zeros((10,10))
z[0,0]=1
print (z)

[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值