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 ret a = 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 · 192 阅读 · 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 · 1204 阅读 · 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 · 125 阅读 · 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 · 1091 阅读 · 0 评论 -
numpy中的fancy indexing
bool索引 a: ndarray b: ndarray of bool a.shape == b.shape a[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 · 181 阅读 · 0 评论 -
python中的zip
a = [1, 2, 3] b = [4, 5, 6] zipped = list(zip(a, b)) aa, bb = zip(*zipped) # star used to unzip print(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 4 2 5 3 6 打乱数据 import numpy原创 2021-02-27 17:30:27 · 119 阅读 · 0 评论 -
numpy.lib.stride_tricks中的as_strided
用于产生类似滑动串口的效果 1D滑动窗口 from numpy.lib import stride_tricks def 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 ret Z =原创 2021-02-27 15:07:37 · 936 阅读 · 0 评论
分享