Numpy练习100(1-50)

Numpy练习100(1-50)

导入Numpy

import numpy as np

查看numpy版本

np.__version__

‘1.19.5’

创建一个空的10*10的数组

np.zeros((10, 10))

array([[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., 0., 0., 0., 0., 0., 0., 0.]])

如何查看数组占内存大小

z = np.zeros((10, 10))
print("占内存大小:%d bytes" % (z.size * z.itemsize))

占内存大小:800 bytes

查看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 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.]])

创建一个长度为10的0数组,第5个值为1

arr = np.zeros(10)
arr[4] = 1
arr

array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])

创建一个值从10到49的数组

arr = np.arange(10, 50)
arr

array([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])

反转数组


arr = arr[::-1]
arr

array([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])

创建一个0~8的3*3矩阵

arr = np.arange(0, 9).reshape(3, 3)
arr

array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

生成一个3*3的对角矩阵

np.eye(3)

array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

生成一个333的随机数矩阵

np.random.rand(3, 3, 3)

array([[[0.71611287, 0.88089082, 0.28869726],
        [0.32877681, 0.77498614, 0.507753  ],
        [0.02105801, 0.54824533, 0.67134557]],

       [[0.35160826, 0.03273192, 0.42475066],
        [0.66049115, 0.90992923, 0.75085084],
        [0.98104294, 0.47796444, 0.32495674]],

       [[0.40493365, 0.8437558 , 0.20927506],
        [0.91652788, 0.37966723, 0.93026299],
        [0.91243474, 0.2433465 , 0.35017105]]])

创建一个10*10的随机数组,并找到最大最小值

arr = np.random.rand(10, 10)
arr

array([[0.48183943, 0.18578372, 0.03695515, 0.85103508, 0.85243389,
0.21036799, 0.37215457, 0.26466725, 0.31175872, 0.43338896],
[0.67269127, 0.59882216, 0.58103814, 0.29903043, 0.31209629,
0.20971891, 0.36363015, 0.40661485, 0.47109258, 0.88863397],
[0.53294591, 0.30414622, 0.60783116, 0.50606865, 0.72569013,
0.8007198 , 0.19982655, 0.90804771, 0.1341551 , 0.34878782],
[0.47653939, 0.87917609, 0.65454085, 0.21611637, 0.42571805,
0.98811572, 0.13912827, 0.63946702, 0.35273519, 0.56168437],
[0.15836349, 0.23694106, 0.67034473, 0.78698897, 0.36301913,
0.41072501, 0.25178733, 0.43384988, 0.74404076, 0.59788018],
[0.49906501, 0.23881315, 0.3911549 , 0.10941184, 0.55339091,
0.71927638, 0.24952252, 0.37468966, 0.10035472, 0.32000112],
[0.41608801, 0.5335736 , 0.23237558, 0.33854914, 0.64669652,
0.4690696 , 0.99048624, 0.72516692, 0.44046972, 0.68158182],
[0.46139266, 0.00818548, 0.0366271 , 0.86940488, 0.80598058,
0.3089815 , 0.25287098, 0.28262208, 0.86351969, 0.98244806],
[0.05680224, 0.75212603, 0.4722568 , 0.72653976, 0.07752193,
0.41462455, 0.05934977, 0.11009855, 0.65767248, 0.86084707],
[0.45785832, 0.48162007, 0.0310194 , 0.07191712, 0.85473791,
0.4930891 , 0.82267471, 0.50149885, 0.67369291, 0.34029698]])

arr.max()

0.9904862378468048

arr.min()

0.008185478081292996

创建一个长度为30的随机数组,并找到平均值

arr = np.random.rand(30)
arr

array([0.50106806, 0.2804269 , 0.01966858, 0.13588837, 0.17773934,
0.97859504, 0.35543517, 0.42382569, 0.71094743, 0.04054807,
0.9310036 , 0.66695164, 0.48238948, 0.82834767, 0.68887113,
0.05313706, 0.14636147, 0.95010172, 0.34425971, 0.90465243,
0.64177737, 0.01028769, 0.90087096, 0.38753384, 0.31148752,
0.61962553, 0.65632689, 0.39405704, 0.61052871, 0.15573844])

arr.mean()

0.47694841840852753

创建一个4边为1,中间为0的二维数组

arr = np.ones((10, 10))
arr

array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

arr[1:-1, 1:-1] = 0
arr

array([[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.]])

如何给一个已经存在的数组添加边(填充0)

arr = np.ones((3, 3))
arr

array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])

arr = np.pad(arr, pad_width=1, mode='constant', constant_values=0)
arr

array([[0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0.]])

arr_1 = np.arange(1, 7).reshape(2, 3)
arr_1

array([[1, 2, 3],
[4, 5, 6]])

#np.pad(Matrix,((1,1),(1,2)),‘constant’,constant_values = (0,0)) 
#表示在二维数组Matrix的边缘填充constant_values指定的数值
#(1,1)表示在Matrix的第[0]轴填充(二维数组中,0轴表示行),
#即在0轴前面填充1个宽度的0,
#比如数组Matrix中的1,2,3两个元素前面各填充了一行0,
#在4,5,6下面填充了一行0。
#(1,2)表示在Matrix的第[1]轴填充(二维数组中,1轴表示列),
#即在1轴前面填充1个宽度的0,
#后面填充2个宽度的constant_values表示填充值,
#且(axis0,axis1)的填充值等于(0,0)
​
np.pad(arr_1, (1,1), 'constant', constant_values=0)

array([[0, 0, 0, 0, 0],
[0, 1, 2, 3, 0],
[0, 4, 5, 6, 0],
[0, 0, 0, 0, 0]])

创建一个5*5矩阵,对角线下方值为1,2,3,4

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

array([[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]])

创建一个8*8的矩阵,并用棋盘图案填充

arr = np.zeros((8, 8))
arr

array([[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.]])

arr[1::2,::2] = 1
arr

array([[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.]])

arr[::2,1::2] = 1
arr

array([[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.]])

给定一个678的三维矩阵,求100个元素的索引是什么

#numpy.unravel_index(indices, dims)
#获取一个/组(int)类型的索引值在一个多维数组中的位置。
np.unravel_index(99, (6,7,8))

(1, 5, 3)

使用tile函数创建一个棋盘格8x8矩阵

#复制
import numpy as np
#行1:0 1
#行2:1 0
#复制4次
np.tile([[0, 1], [1, 0]], 4) 

array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]])

#(4, 4)
#x=4:增加列,在同一行复制
#y=4:增加行,将行复制一行
np.tile([[0, 1], [1, 0]], (4, 4))

array([[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]])

创建一个5*5矩阵并标准化处理


arr = np.random.rand(5,5)
arr

array([[0.83156761, 0.57788856, 0.93415224, 0.28334558, 0.5627008 ],
[0.79415727, 0.14410785, 0.77294212, 0.23324915, 0.24357715],
[0.28693338, 0.97402272, 0.76951533, 0.05504568, 0.06098257],
[0.79467114, 0.23862181, 0.44416962, 0.06204065, 0.18040006],
[0.1351451 , 0.78472013, 0.5425988 , 0.40856153, 0.38282963]])

#np.std(arr, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>)
#axis=0为求每一列标准差
#axis=1为求每一行标准差
#ddof=0为计算有偏样本标准差
arr = (arr - np.mean(arr) / (np.std(arr)))
arr

array([[-0.73181899, -0.98549804, -0.62923436, -1.28004102, -1.0006858
],
[-0.76922934, -1.41927875, -0.79044448, -1.33013746, -1.31980945],
[-1.27645322, -0.58936389, -0.79387128, -1.50834092, -1.50240403],
[-0.76871546, -1.32476479, -1.11921699, -1.50134595, -1.38298654],
[-1.4282415 , -0.77866647, -1.0207878 , -1.15482508, -1.18055697]])

创建一个dtype类型用来描述一个颜色(RGBA)

np.dtype([("R", np.ubyte, 1),
             ("G", np.ubyte, 1),
             ("B", np.ubyte, 1), 
             ("A", np.ubyte, 1)])

dtype([(‘R’, ‘u1’), (‘G’, ‘u1’), (‘B’, ‘u1’), (‘A’, ‘u1’)])

一个53的矩阵 * 一个32的矩阵

arr = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9],
               [1, 1, 1],
               [1, 1, 1]])
arr_1 = np.array([[1, 1], 
                 [2, 2], 
                 [1, 1]])
np.dot(arr, arr_1)

array([[ 8, 8],
[20, 20],
[32, 32],
[ 4, 4],
[ 4, 4]])

给定一个一维数组,将3~8取反

arr = np.arange(10)
arr[(3<=arr) & (arr<=8)] *= -1
arr

array([ 0, 1, 2, -3, -4, -5, -6, -7, -8, 9])

看看下边脚本输出啥

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

9
10

给定一个整数数组Z,看看下面哪个表达式是合法的

arr = np.array([[1, 2, 3]])
arr

array([[1, 2, 3]])

#1*1, 2*2, 3*3*3
arr ** arr

array([[ 1, 4, 27]], dtype=int32)

arr < -arr

array([[False, False, False]])

1j * arr

array([[0.+1.j, 0.+2.j, 0.+3.j]])

arr / 1 / 1

array([[1., 2., 3.]])

arr < arr > arr

--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) in ()
----> 1 arr < arr > arr

ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

下面表达式的结果是什么 np.array(0) / np.array(0)
D:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning:
invalid value encountered in true_divide “”"Entry point for
launching an IPython kernel. nan np.array(0) // np.array(0)
D:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning:
divide by zero encountered in floor_divide “”"Entry point for
launching an IPython kernel. 0
np.array([np.nan]).astype(int).astype(float) array([-2.14748365e+09])

如何对数组进行四舍五入操作

#np.random.uniform(low, high, size)
#从(low, high)中随机采样,定义域为左闭右开,包含low,不包含high
#size为输出样本的数目
arr = np.random.uniform(-10, +10, 10)
arr

​ array([-0.6136493 , -8.3777229 , 8.94670211, 6.54870036,
8.80732928,
5.99126026, -7.17393776, -1.31437403, -3.55131662, 2.52798194])

#两种方法
#copysign(x, y)
#x会转换成后边y的格式
#ceil(x)
#x向上取整
#floor(y)
#y向下取整
#np.copysign(np.ceil(np.abs(arr)), arr)
np.ceil(arr)

array([-0., -8., 9., 7., 9., 6., -7., -1., -3., 3.])

如何找出两个数组的共同值

arr = np.random.randint(0, 10, 10)
arr

array([5, 1, 1, 9, 8, 7, 1, 0, 9, 4])

arr_1 = np.random.randint(0, 10, 10)
arr_1

array([2, 0, 6, 0, 7, 3, 0, 4, 7, 3])

#intersect1d中的为1,不是L
np.intersect1d(arr, arr_1)

array([0, 4, 7])

忽略所有numpy警告

defaults = np.seterr(all="ignore")
arr = np.ones(1) / 0
arr = np.seterr(**defaults)
~arr = np.ones(1) / 0

File “”, line 4
~arr = np.ones(1) / 0
^ SyntaxError: can’t assign to operator

下面表达式正确吗

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

False

如何获得昨天今天明天的日期

import numpy as np
from datetime import datetime
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
yesterday
numpy.datetime64('2021-11-23')
today = np.datetime64('today', 'D')
today
numpy.datetime64('2021-11-24')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
tomorrow

numpy.datetime64(‘2021-11-25’)

# 如何获取2016年7月对应的所有日期
arr = np.arange('2016-07', '2016-08', dtype = 'datetime64[D]')
arr

array([‘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’], dtype=‘datetime64[D]’)

如何计算((C+B)*(-A/2))

C = np.ones(3)*1
D = np.ones(3)*2
np.add(C, D)

array([3., 3., 3.])

np.divide(C, 2, out=C)

array([0.5, 0.5, 0.5])

np.negative(C, out=C)

array([-0.5, -0.5, -0.5])

np.multiply(C, D, out=C)

array([-1., -1., -1.])

提取随机数列整数部分的五种方法

arr = np.random.uniform(0, 10, 10)
arr

array([2.81559748, 0.03019882, 1.51206861, 7.99610918, 1.81493326,
4.16075382, 4.75941272, 2.12098681, 1.51858654, 2.14791856])

arr = arr - arr%1
arr

array([2., 0., 1., 7., 1., 4., 4., 2., 1., 2.])

np.floor(arr)

array([2., 0., 1., 7., 1., 4., 4., 2., 1., 2.])

np.ceil(arr)

array([2., 0., 1., 7., 1., 4., 4., 2., 1., 2.])

arr.astype(int)

array([2, 0, 1, 7, 1, 4, 4, 2, 1, 2])

np.trunc(arr)

array([2., 0., 1., 7., 1., 4., 4., 2., 1., 2.])

创建一个5*5的矩阵,每一行值为1~4

arr = np.zeros((5, 5))
arr += np.arange(5)
arr

array([[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.]])

给定一个生成器函数,可以生成10个整数,使用它来创建一个数组

def generate():
    for x in range(10):
        yield x
np.fromiter(generate(), dtype=int, count=-1)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

创建一个长度为10的数组,值为0~1之间,不包含首尾

arr = np.random.uniform(0, 1, 10)
arr

array([0.54055566, 0.95606238, 0.66590546, 0.85242485, 0.59385307,
0.76815627, 0.18383446, 0.58069445, 0.32744785, 0.21595318])

#numpy.linspace(start, stop, num=50, endpoint=True, 
#retstep=False, dtype=None, axis=0)
#endpoint:True则包含stop,False不包含stop
np.linspace(0, 1, 11, endpoint = False)[1:]

array([0.09090909, 0.18181818, 0.27272727, 0.36363636, 0.45454545,
0.54545455, 0.63636364, 0.72727273, 0.81818182, 0.90909091])

创建一个长尾10的数组,并做排序操作

arr = np.random.rand(10)
arr

array([0.14974594, 0.36396232, 0.13667815, 0.9140814 , 0.02026494,
0.71054348, 0.84811872, 0.67113335, 0.84269459, 0.45853275])

arr.sort()
arr

array([0.02026494, 0.13667815, 0.14974594, 0.36396232, 0.45853275,
0.67113335, 0.71054348, 0.84269459, 0.84811872, 0.9140814 ])

如何对一个数组进行相加操作,并且速度快于np.sum

arr = np.arange(10)
arr

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.add.reduce(arr)

45

a = np.random.rand(2, 3)
a

array([[0.6353138 , 0.84637329, 0.40215851],
[0.68735282, 0.66543 , 0.32781727]])

b = np.random.rand(2, 3)
b

array([[0.08301729, 0.83742605, 0.03815493],
[0.21875107, 0.66645686, 0.57591683]])

equal = np.allclose(a, b)
equal

False

equal_1 = np.array_equal(a, b)
equal_1

False

使一个数组不变(只读)

arr = np.array([[1, 2, 3],
               [4, 5, 6]])
arr

array([[1, 2, 3],
[4, 5, 6]])

arr.flags.writeable = False
arr[0, 0] = 2

--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) in ()
1 arr.flags.writeable = False
----> 2 arr[0, 0] = 2

ValueError: assignment destination is read-only

给定一个笛卡尔坐标 的一个10*2的随机矩阵,将其转换为极坐标

arr = np.random.rand(10, 2)
arr

array([[0.17413758, 0.64583282],
[0.96465674, 0.31675587],
[0.14983688, 0.38644617],
[0.85289215, 0.79389811],
[0.63071575, 0.40532534],
[0.56286767, 0.8490789 ],
[0.57373708, 0.69153604],
[0.79373757, 0.66538194],
[0.33418371, 0.47014435],
[0.07866323, 0.58631301]])

x = arr[:,0]
x

array([0.17413758, 0.96465674, 0.14983688, 0.85289215, 0.63071575,
0.56286767, 0.57373708, 0.79373757, 0.33418371, 0.07866323])

y = arr[:, 1]
y

array([0.64583282, 0.31675587, 0.38644617, 0.79389811, 0.40532534,
0.8490789 , 0.69153604, 0.66538194, 0.47014435, 0.58631301])

r = np.sqrt(x**2 + y**2)
r

array([0.66889754, 1.01533094, 0.41447767, 1.16520351, 0.74972727,
1.0187026 , 0.89855235, 1.03573764, 0.57681406, 0.59156644])

t = np.arctan2(y, x)
t

array([1.30742696, 0.317269 , 1.20091184, 0.74958983, 0.57118626,
0.9853951 , 0.87823216, 0.69765616, 0.95285154, 1.43742683])

创建一个长度为10的随机矩阵,并将最大值替换为0

arr = np.random.rand(10)
arr

array([0.06678863, 0.90337092, 0.941385 , 0.72378383, 0.2967094 ,
0.70132596, 0.26173271, 0.03405047, 0.76676091, 0.59013508])

#arr.argmax()针对的是非整数的数组进行的最大值检测
arr[arr.argmax()] = 0
arr

array([0.06678863, 0.90337092, 0. , 0.72378383, 0.2967094 ,
0.70132596, 0.26173271, 0.03405047, 0.76676091, 0.59013508])

创建具有x和y坐标的结构化数组,它覆盖(0, 1)X(0, 1)区域

arr = np.zeros((3, 3),[('x', float), ('y', float)])
arr

array([[(0., 0.), (0., 0.), (0., 0.)],
[(0., 0.), (0., 0.), (0., 0.)],
[(0., 0.), (0., 0.), (0., 0.)]], dtype=[(‘x’, ‘<f8’), (‘y’, ‘<f8’)])

#meshgrid(x, y)
arr['x'],arr['y'] = np.meshgrid(np.linspace(0, 1, 3), np.linspace(0, 1, 3))
arr

array([[(0. , 0. ), (0.5, 0. ), (1. , 0. )],
[(0. , 0.5), (0.5, 0.5), (1. , 0.5)],
[(0. , 1. ), (0.5, 1. ), (1. , 1. )]],
dtype=[(‘x’, ‘<f8’), (‘y’, ‘<f8’)])

给定两个数组x和y,构造柯西矩阵

C(Cij = 1 / (xi - yj))
x = np.arange(3)
y = x + 0.5
z = np.subtract.outer(x, y)
z

array([[-0.5, -1.5, -2.5],
[ 0.5, -0.5, -1.5],
[ 1.5, 0.5, -0.5]])

d = 1.0 / z
d

array([[-2. , -0.66666667, -0.4 ],
[ 2. , -2. , -0.66666667],
[ 0.66666667, 2. , -2. ]])

C = np.linalg.det(d)
C

-15.17037037037037

打印每种numpy标量类型的最小和最大可表示值

for dtype in [np.int8, np.int32, np.int64]:
    np.iinfo(dtype).min
    np.iinfo(dtype).max

如何打印出数组中所有的值

np.set_printoptions(threshold = np.inf)
arr = np.random.randint(0, 5, (10, 3))
arr

array([[0, 3, 2],
[1, 0, 3],
[3, 4, 1],
[1, 3, 1],
[0, 4, 4],
[2, 1, 0],
[1, 4, 3],
[4, 1, 0],
[4, 1, 2],
[1, 2, 3]])

如何在数组中找到最接近给定值的值

a = np.arange(100)
b = np.random.uniform(0, 100)
x = np.abs(a-b)
x

array([56.57592546, 55.57592546, 54.57592546, 53.57592546,
52.57592546,
51.57592546, 50.57592546, 49.57592546, 48.57592546, 47.57592546,
46.57592546, 45.57592546, 44.57592546, 43.57592546, 42.57592546,
41.57592546, 40.57592546, 39.57592546, 38.57592546, 37.57592546,
36.57592546, 35.57592546, 34.57592546, 33.57592546, 32.57592546,
31.57592546, 30.57592546, 29.57592546, 28.57592546, 27.57592546,
26.57592546, 25.57592546, 24.57592546, 23.57592546, 22.57592546,
21.57592546, 20.57592546, 19.57592546, 18.57592546, 17.57592546,
16.57592546, 15.57592546, 14.57592546, 13.57592546, 12.57592546,
11.57592546, 10.57592546, 9.57592546, 8.57592546, 7.57592546,
6.57592546, 5.57592546, 4.57592546, 3.57592546, 2.57592546,
1.57592546, 0.57592546, 0.42407454, 1.42407454, 2.42407454,
3.42407454, 4.42407454, 5.42407454, 6.42407454, 7.42407454,
8.42407454, 9.42407454, 10.42407454, 11.42407454, 12.42407454,
13.42407454, 14.42407454, 15.42407454, 16.42407454, 17.42407454,
18.42407454, 19.42407454, 20.42407454, 21.42407454, 22.42407454,
23.42407454, 24.42407454, 25.42407454, 26.42407454, 27.42407454,
28.42407454, 29.42407454, 30.42407454, 31.42407454, 32.42407454,
33.42407454, 34.42407454, 35.42407454, 36.42407454, 37.42407454,
38.42407454, 39.42407454, 40.42407454, 41.42407454, 42.42407454])

index = x.argmin()
index

57

z[index]

57

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyyxbdmrrc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值