NumPy百題(四)

上一篇:NumPy百題(三)

61.Find the nearest value from a given value in an array

從數組中的給定值中找出最近的值
在這裏插入圖片描述

uniform() 函數

uniform() 方法將隨機生成下一個實數,它在 [x, y] 範圍內。

import random
random.uniform(x, y)

x – 隨機數的最小值,包含該值。
y – 隨機數的最大值,不包含該值。

uniform()是不能直接訪問的,需要導入 random 模塊,然後通過 random 靜態對象調用該方法。

numpy.ndarray.flat

numpy.ndarray.flat 是一個數組元素迭代器
在這裏插入圖片描述

62.Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator

如何用迭代器(iterator)計算兩個分別具有形狀(1,3)和(3,1)的數組
在這裏插入圖片描述

NumPy 迭代數組

NumPy 迭代器對象 numpy.nditer 提供了一種靈活訪問一個或者多個數組元素的方式。迭代器最基本的任務的可以完成對數組元素的訪問。
在這裏插入圖片描述

63.Create an array class that has a name attribute

創建一個具有name屬性的數組類
在這裏插入圖片描述

64.Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)?

考慮一個給定的向量,如何對由第二個向量索引的每個元素加1(小心重複的索引)?
在這裏插入圖片描述

65.How to accumulate elements of a vector (X) to an array (F) based on an index list (I)

根據索引列表(I),如何將向量(X)的元素累加到數組(F)
在這裏插入圖片描述

numpy.bincount(x, weights=None, minlength=None)

bin的數量比x中的最大值大1,每個bin給出了它的索引值在x中出現的次數。
在這裏插入圖片描述

66.Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors

考慮一個(dtype=ubyte) 的 (w,h,3)圖像,計算其唯一顏色的數量
在這裏插入圖片描述

67.Considering a four dimensions array, how to get sum over the last two axis at once

考慮一個四維數組,如何一次性計算出最後兩個軸(axis)的和
在這裏插入圖片描述
在這裏插入圖片描述

68.Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices

考慮一個一維向量D,如何使用相同大小的向量S來計算D子集的均值
在這裏插入圖片描述

69.How to get the diagonal of a dot product

如何獲得點積 dot prodcut的對角線元素
在這裏插入圖片描述

70.Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value

考慮一個向量[1,2,3,4,5],如何建立一個新的向量,在這個新向量中每個值之間有3個連續的零
在這裏插入圖片描述

提示

在這裏插入圖片描述

71.Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)

考慮一個維度(5,5,3)的數組,如何將其與一個(5,5)的數組相乘
在這裏插入圖片描述

72.How to swap two rows of an array

如何對一個數組中任意兩行做交換
在這裏插入圖片描述

拓展

在這裏插入圖片描述

73.Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles

考慮一個可以描述10個三角形的triplets,找到可以分割全部三角形的line segment
在這裏插入圖片描述

74.Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C

給定一個二進制的數組C,如何產生一個數組A滿足np.bincount(A)==C
在這裏插入圖片描述

numpy.repeat()

numpy.repeat(a,repeats,axis=None)
repeats可以爲一個數,也可以爲一個矩陣。
axis=None,時候就會flatten當前矩陣,實際上就是變成了一個行向量
axis=0,沿着y軸複製,實際上增加了行數
axis=1,沿着x軸複製,實際上增加列數
在這裏插入圖片描述

75.How to compute averages using a sliding window over an array

如何通過滑動窗口計算一個數組的平均數
在這裏插入圖片描述

numpy.cumsum()

numpy.cumsum(a, axis=None, dtype=None, out=None)
axis=0,按照行累加。
axis=1,按照列累加。
axis不給定具體值,就把numpy數組當成一個一維數組。
在這裏插入圖片描述

76.Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1])

考慮一個一維數組Z,構造一個2維數組,第一行是(Z[0],Z[1],Z[2]),後面每一行都後移一個元素(最後一行是(Z[-3],Z[-2],Z[-1]))
(提示: from numpy.lib import stride_tricks)
在這裏插入圖片描述

np.stride_tricks.as_strided(x, shap, strides, subok, writeable)

在這裏插入圖片描述
在這裏插入圖片描述

77.How to negate a boolean, or to change the sign of a float inplace

如何對布爾值取反,或者原位(in-place)改變浮點數的符號(sign)
在這裏插入圖片描述

78.Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])

考慮兩組點集P0和P1去描述一組線(二維)和一個點p,如何計算點p到每一條線 i (P0[i],P1[i])的距離
在這裏插入圖片描述

79.Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])

考慮兩組點集P0和P1去描述一組線(二維)和一組點集P,如何計算每一個點 j(P[j]) 到每一條線 i (P0[i],P1[i])的距離(接上一題)
在這裏插入圖片描述

80.Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary)

對於任意一個數組,寫一個可以給定中心元素和固定形狀,輸出數組子集的功能(如果有需要,可自動填充數值)
在這裏插入圖片描述
在這裏插入圖片描述

下一篇:NumPy百題(五)

發佈了82 篇原創文章 · 獲贊 122 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章