@nrailgun
2017-09-04T16:40:56.000000Z
字数 3295
阅读 1667
程序设计
np.maximum.reduce([a,b])
Y = np.hstack([ np.eye(k), np.zeros(k, n - k) ])
Y = Y[:, y]
a = np.random.randint(10, size=(3,3))
a[a < 0] = 0
np.sum(a)
np.tile(m, (w, h))
np.array(b, dtype=int)
Add only rows with same classes, by convert row class vector to ground true table.
n = 3
m = 4
k = 2
X = np.random.randint(10, size=(n, m))
y = [0, 1, 0]
# Add row 0 and row 2 into one row, and row 1 into another row.
Y = np.hstack([ np.eye(k), np.zeros(k, n - k) ])
Y = Y[:, y]
np.dot(Y, X.T)
np.random.choice(range, n)
a[:, :, -1]
np.diag(M)
M.flags.writeable = False
M.mean(axis=1, keepdims=True)
M.argsort()
def gen():
for i in xrange(10):
yield i
a = np.fromiter(gen(), dtype=float, count=-1)
np.unique(z)
A = np.random.randint(10, size=(2,2,3,4))
B = A.reshape(A.shape[:, -2] + (-1,))
print np.sum(B, axis=-1)
np.sum(A * B.T, axis=1)
# Faster version
np.einsum("ij,ji->i", A, B)
A[[r1, r2]] = A[[r2, r1]]
import scipy.ndimage.filters as flt
inp = np.zeros((ksize, ksize))
inp[ksize / 2, ksize / 2] = 1
kern = flt.gaussian_filter(inp, sigma)
NOTE: It's different from cv2 ops, DO NOT mix them.
from scipy.misc import imread, imsave, imresize
# imread('x.png', True) returns gray-scale
img = imread('x.png')
print img.dtype, img.shape # Prints "uint8 (400, 248, 3)"
img_tinted = img * [1, 0.95, 0.9]
img_tinted = imresize(img_tinted, (300, 300))
imsave('y.png', img_tinted)
Plot:
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()
Subplot:
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
Show image:
import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
img = imread('assets/cat.jpg')
img_tinted = img * [1, 0.95, 0.9]
# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)
# Show the tinted image
plt.subplot(1, 2, 2)
# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()
Show Gray-Scale Image:
plt.imshow(img, cmap="Greys_r")
np.logical_not(a)
np.negative(b)
# ufunc can be add / multiply
np.ufunc.reduce(A, axis)
g = np.random.normal(scale=0, size=(2,2))
(i, j) = np.unravel_index(x.argmax(), x.shape)
# I is a list of 8 1*28*28 images.
# Show them in one image.
imshow(I.transpose(1, 0, 2).reshape(28, 8*28), cmap='gray')
# Or, diff is a 20 5*5 images list.
imshow(diff.reshape(4, 5, 5, 5)
.transpose(0, 2, 1, 3).reshape(4*5, 5*5), cmap='gray')
Not necessary to produce temporary array [ x1; x2 ]
, all you need is W1 * x1 + W2 * x2
.