python中的[1:], [::-1], [:,m:n]
1 | b = a[i:j] # 表示复制a[i]到a[j - 1],以生成新的list对象 |
X[:, 0]
是numpy中数组的一种用法,表示对一个二维数组,取该二维数组第一维中的所有数据,第二维中取0个数据,直观来说,X[:, 0]
就是取所以劥的第0个数据,X[:, 1]
就是取所有行的第1个数据。
X[n, :]
是取第1维中下标为n的元素的所有值
X[:, m:n]
,即取所有数据的第m到n-1列数据,含左不含右
Create a vector with values ranging from 10 to 49
Reverse a vector (first element becomes last)
Create a 8x8 matrix and fill it with a checkerboard pattern
1 | Z = np.zeros((8,8),dtype=int) |
获取一个多维数组中100号元素的位置
1 | print(np.unravel_index(99, (6, 7, 8))) |
Create a 5x5 matrix with values 1,2,3,4 just below the diagonal
Create a 2d array with 1 on the border and 0 inside
Create a random vector of size 30 and find the mean value
Create a 10x10 array with random values and find the minimum and maximum values
Create a 3x3 matrix with values ranging from 0 to 8
How to add a border (filled with 0’s) around an existing array?
Find indices of non-zero elements from [1,2,0,0,4,0]
获取值不为0的元素的下标
Create a checkerboard 8x8 matrix using the tile function
Normalize a 5x5 random matrix
Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
Given a 1D array, negate all elements which are between 3 and 8, in place
What is the output of the following script?
python自带的sum函数如下:
sum(iterable, [start])
后一个参数指定的是初始化值,会加到结果中去,故结果是-1+1+2+3+4=9
numpy中sum函数为:
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=, initial=)
不使用命名参数,即将-1
传给了axis,结果为10。如果要指定初始值,这样用np.sum(range(1, 5), initial=-1)
Consider an integer vector Z, which of these expressions are legal?
What are the result of the following expressions?
How to find common values between two arrays?
How to get all the dates corresponding to the month of July 2016?
Extract the integer part of a random array using 5 different methods
Consider a generator function that generates 10 integers and use it to build an array
How to sum a small array faster than np.sum?
Consider two random array A and B, check if they are equal?
Create a structured array with x
and y
coordinates covering the [0,1]x[0,1] area?
1 | # 关于np.subtract.outer |
How to find the closest value (to a given scalar) in a vector?
Create a structured array representing a position (x,y) and a color (r,g,b)?
参考:https://www.jianshu.com/p/d09778f4e055
关于python中的yield
-
通常的for…in…循环中,in后面是一个数组,这个数组就是一个可迭代对象,类似的还有链表,字符串,文件。它可以是mylist = [1, 2, 3],也可以是mylist = [x*x for x in range(3)]。
它的缺陷是所有数据都在内存中,如果有海量数据的话将会非常耗内存。 -
生成器是可以迭代的,但只可以读取它一次。因为用的时候才生成。比如 mygenerator = (x*x for x in range(3)),注意这里用到了(),它就不是数组,而上面的例子是[]。
-
我理解的生成器(generator)能够迭代的关键是它有一个next()方法,工作原理就是通过重复调用next()方法,直到捕获一个异常。可以用上面的mygenerator测试。
-
带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代,工作原理同上。
-
eld 是一个类似 return 的关键字,迭代一次遇到yield时就返回yield后面(右边)的值。重点是:下一次迭代时,从上一次迭代遇到的yield后面的代码(下一行)开始执行。
-
带有yield的函数不仅仅只用于for循环中,而且可用于某个函数的参数,只要这个函数的参数允许迭代参数。比如array.extend函数,它的原型是array.extend(iterable)。
-
send(msg)与next()的区别在于send可以传递参数给yield表达式,这时传递的参数会作为yield表达式的值,而yield的参数是返回给调用者的值。——换句话说,就是send可以强行修改上一个yield表达式值。比如函数中有一个yield赋值,a = yield 5,第一次迭代到这里会返回5,a还没有赋值。第二次迭代时,使用.send(10),那么,就是强行修改yield 5表达式的值为10,本来是5的,那么a=10
-
send(msg)与next()都有返回值,它们的返回值是当前迭代遇到yield时,yield后面表达式的值,其实就是当前迭代中yield后面的参数。
-
第一次调用时必须先next()或send(None),否则会报错,send后之所以为None是因为这时候没有上一个yield(根据第8条)。可以认为,next()等同于send(None)。
示例:
理解的关键在于:下次迭代时,代码从yield的下一条语句开始执行
numpy.random.uiform(low=0.0, high=1.0, size=None)
生成size
个符合均匀分布的浮点数,取值范围为[low, high)
,默认取值范围是[0.0, 1.0)
1 | import scipy |
上面这个库有下面这个函数(皮),可以非常方便的用于计算两个输入集合之间的距离
scipy.spatial.distance.cdist(XA, XB, metric='euclidean', p=None, V=None, VI=None, w=None)
该函数用于计算两个输入集合的距离,通过metric参数指定计算距离的不同方式得到冉的距离度量值。
metric的取值有狠毒哦,例如chebyshev(契比雪夫距离), euclidean(欧氏距离), hamming(汉明距离), cosine(余弦夹角), colleration(相关系数), jaccard(杰卡德相似系数), mahalamobis(马氏距离), minkowski(闵可夫斯基距离)
How to convert a float (32 bits) array into an integer (32 bits) in place?
How to read the following file?
genfromtxt()
函数创建数组表格数据,主要执行两个循环。第一个循环将文件的每一行转换成字符串序列,第二个循环将每个字符串序列转换成相应数据类型。
genfromtxt
能够考虑缺失的数据,但其他更快和更简单的函数像loadtxt
不能考虑缺失值。
What is the equivalent of enumerate for numpy arrays?
np.meshgrid()
:生成网格点坐标矩阵
上图中,每个交叉点就是网格点,描述这些网格点的坐标的矩阵,就是坐标矩阵
坐标矩阵——横坐标矩阵X中的每个元素与纵坐标矩阵Y中对应位置元素,共同构成一个点的完整坐标
np.put()
和np.take()
numpy
中take
函数与put
函数有着与神奇索引类似的作用,take
函数可以用于获取数组子集,而put
函数可以设置数组子集。
take
函数:
1 | arr = np.arange(6) * 100 # 初始化数组为[0, 100, 200, 300, 400, 500] |
put
函数:
1 | arr = np.arange(6) * 100 |
np.random.choice(a, size=None, replace=True, p=None)
从a(只要是ndarray都可以,但必须是一维的)中随机抽取数字,并组成指定大小(size)的数组
replace:True表示可以取相同数字,False表示不可以取相同的数字
数组p:与数组a相对应,表示取数组a中每个元素的概率,默认为选取每个元素的概率相同
对于axis
的理解
https://blog.csdn.net/weixin_37821353/article/details/88367211
简要的来说:
首先将矩阵下标理解称为如下形式:
然后axis = n
就表示仅仅第n
个下标变化,其余下标不变的为一组,然后再进行操作。
如上图,如果axis = 0
,则仅仅把第一个坐标变化,其他两个坐标一致的分为一组,因为分组之后的数组大小为:[4, 5]
How to sort an array by the nth column?
numpy.ndarray.flat()
将数组转换为1-D的迭代器,flat返回的是一个迭代器,可以用for访问数组的每一个元素
python 多维切片之冒号和三个点
https://blog.csdn.net/z13653662052/article/details/78010654
python中的None
代表新增加一个维度,别称为newaxis
,None
放在哪一维,就会在哪一维上出现新的维度。
三个点表示省略所有的冒号来用省略号代替,即a[:, :, None]
额a[..., None]
是一样的,因为...
代替了前面两个冒号
np.bincount(x, weight=None, minlength=None)
bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数。
举例:
1 | # 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7 |
weights这个参数。文档说,如果weights参数被指定,那么x会被它加权,也就是说,如果值n发现在位置i,那么out[n] += weight[i]而不是out[n] += 1.**因此,我们weights的大小必须与x相同,否则报错。
1 | w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) |
文档说,如果minlength被指定,那么输出数组中bin的数量至少为它指定的数(如果必要的话,bin的数量会更大,这取决于x)。
1 | # 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3 |
np.add.at()
1 | #np.add.at(dW, x, dout) |
Considering a four dimensions array, how to get sum over the last two axis at once?
np.roll(a, shift, axis=None)
:沿着给定轴滚动数组元素。超出最后位置的元素将会滚动到第一个位置(将a沿着axis的方向,滚动shift长度)
np.repeat(a, repeats, axis=None)
这边的repeats可以是一个数,也可以是一个矩阵。
1 | ''' |
np.stride_tricks.as_strided(x, shap, strides, subok, writeable)
1 | import numpy as np |