Skip to content

Instantly share code, notes, and snippets.

@XUJiahua
Last active December 10, 2016 13:14
Show Gist options
  • Save XUJiahua/401e84c02fc0cbbb2f633d8133b542bf to your computer and use it in GitHub Desktop.
Save XUJiahua/401e84c02fc0cbbb2f633d8133b542bf to your computer and use it in GitHub Desktop.
# http://www.scipy-lectures.org/intro/numpy/exercises.html
import numpy as np
# 1
a = np.linspace(1,15,15).reshape([3,5]).T
b = a[[1,3],:]
# 2
a = np.arange(25).reshape(5, 5)
b = np.array([1., 5, 10, 15, 20])
# array([ 1., 5., 10., 15., 20.])
# shape: (5,)
# b[:,np.newaxis] =
# array([[ 1.],
# [ 5.],
# [ 10.],
# [ 15.],
# [ 20.]])
# shape: (5, 1)
c = a/b[:,np.newaxis]
# 3
a = np.random.random([10,3])
# array([[ 0.09583662, 0.68293089, 0.08176245],
# [ 0.5044622 , 0.89548375, 0.586283 ],
# [ 0.14241036, 0.94341594, 0.80657279],
# [ 0.70212222, 0.79018404, 0.43425573],
# [ 0.73331918, 0.79883131, 0.66549872],
# [ 0.7642978 , 0.01023381, 0.96184134],
# [ 0.77596693, 0.73808234, 0.95999951],
# [ 0.92064225, 0.39235637, 0.65809024],
# [ 0.17385709, 0.75346265, 0.99849483],
# [ 0.65049734, 0.58286805, 0.57900828]])
b = np.abs(a-0.5)
# array([[ 0.40416338, 0.18293089, 0.41823755],
# [ 0.0044622 , 0.39548375, 0.086283 ],
# [ 0.35758964, 0.44341594, 0.30657279],
# [ 0.20212222, 0.29018404, 0.06574427],
# [ 0.23331918, 0.29883131, 0.16549872],
# [ 0.2642978 , 0.48976619, 0.46184134],
# [ 0.27596693, 0.23808234, 0.45999951],
# [ 0.42064225, 0.10764363, 0.15809024],
# [ 0.32614291, 0.25346265, 0.49849483],
# [ 0.15049734, 0.08286805, 0.07900828]])
c = np.argsort(b)
# 有问题,排序不对
# array([[1, 0, 2],
# [0, 2, 1],
# [2, 0, 1],?
# [2, 0, 1],?
# [2, 0, 1],?
# [0, 2, 1],
# [1, 0, 2],
# [1, 2, 0],?
# [1, 0, 2],
# [2, 1, 0]])
a[c==0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment