![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
w112348
这个作者很懒,什么都没留下…
展开
-
手动实现keras.utils.to_categorical
def to_categorical_1d(train_labels): ret = np.zeros((len(train_labels), train_labels.max()+1)) ret[np.arange(len(ret)), train_labels] = 1 return reta = np.array([5, 0, 4, 1, 9, 2, 1, 3, 1, 4])R = to_categorical_1d(a)print(a)print(R)[5 0原创 2021-03-07 15:12:01 · 138 阅读 · 0 评论 -
image downsampling in python
1 适用于灰度图像或者三通道图像的降采样def downsampling(img, k): """ Down-sample an image of shape (h, w) into an image of shape(h/k, w/k), by averaging every k*k elements. img: an image of 2D or 3D k: edge length of the kernel Can apply t原创 2021-03-02 01:22:07 · 1024 阅读 · 0 评论 -
subarray of any ndarray with padding
先写一维的情况,然后改成多维的情况,这样比较好写且容易测试。numpy的符号运算与正常的算术符号运算相统一,实在是太方便了def extract(A, shape, position, fill=0): R = np.ones(shape) * fill A_shape = np.array(A.shape) R_shape = np.array(list(shape)) position = np.array(position) A_start = p原创 2021-02-28 22:58:02 · 76 阅读 · 0 评论 -
scipy.interpolate中的interp2d
a = np.array([[1, 2], [2, 3]])interObj = interpolate.interp2d([0, 2], [0, 2], a)rst = interObj(range(3), range(3))print(a)print(rst)[[1 2] [2 3]][[1. 1.5 2. ] [1.5 2. 2.5] [2. 2.5 3. ]]原创 2021-02-27 23:06:11 · 989 阅读 · 0 评论 -
numpy中的fancy indexing
bool索引a: ndarrayb: ndarray of boola.shape == b.shapea[b]a = np.arange(-2, 3)print(a)print(a[[True, False, True, False, False]])print(a > 0)print(a[a > 0])[-2 -1 0 1 2][-2 0][False False False True True][1 2]array索引1D数组a:ndarra原创 2021-02-27 22:53:03 · 111 阅读 · 0 评论 -
python中的zip
a = [1, 2, 3]b = [4, 5, 6]zipped = list(zip(a, b))aa, bb = zip(*zipped) # star used to unzipprint(zipped)print(aa, bb)[(1, 4), (2, 5), (3, 6)](1, 2, 3) (4, 5, 6)两个应用:循环for i, j in zip(a, b): print(i, j)1 42 53 6打乱数据import numpy原创 2021-02-27 17:30:27 · 70 阅读 · 0 评论 -
numpy.lib.stride_tricks中的as_strided
用于产生类似滑动串口的效果1D滑动窗口from numpy.lib import stride_tricksdef sliding_window_1d(a, window): shape = (len(a)-window+1, window) strides = (a.itemsize, a.itemsize) ret = stride_tricks.as_strided(Z, shape=shape, strides=strides) return retZ =原创 2021-02-27 15:07:37 · 792 阅读 · 0 评论