import numpy as np
A1 = np.array([[1,2,3,4,8]])
A = np.array([1,2,3,4,8])
B = np.array([1,2,3,10,20])
print(A1.shape, B.shape)
print(type(A1), type(B))
c = np.zeros((2,2))
print(c)
print(type(c))
c = np.ones((3,3))
print(c)
c = np.full((3,3),10)
print(c)
c = np.eye((4))
print(c)
d = np.random.random((2,3))
print(d)
r = np.random.normal()
print(r)
r1 = np.random.randint(1,10,100)
print(r1)
r2 = np.random.randint(1,10,(4,5))
print(r2)
y = np.empty_like(r2)
print(y)
y = np.zeros_like(r2)
print(y)
y = np.ones_like(r2)
print(y)
print(r2.T) # Transpose
x = np.array([[1,2],[3,4]])
print(np.sum(x)) # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"
print(np.sqrt(x))
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
print(x+y)
print(np.add(x,y))
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
print(x)
print(y)
print(v)
print(w)
print(v.dot(w))
print('---------------')
print(np.dot(v, w))
print(x.dot(v))
print('---------------')
print(np.dot(x, v))
print(np.dot(v, x))
x = np.array([[1,2], [3,4]])
print(x)
a2d = np.array([[1,2,3],[4,5,6]])
b2d = np.array([[10,4,6],[2,4,6]])
print(a2d.shape)
r, c = b2d.shape
print(r,c)
c2d = a2d.T # Transpose
print(c2d)
print(a2d)
# Element wise multiplication
d2d = a2d * b2d
print(d2d)
d2d = np.multiply(a2d, b2d)
print(d2d)
# Matrix multiplication
e2d = np.dot(a2d,c2d)
print(e2d)
e2d = np.matmul(a2d,c2d)
print(e2d)
det1 = np.linalg.det(e2d)
print(det1)
eig, eig1 = np.linalg.eig(e2d)
print(eig)
print(eig1)
inv1 = np.linalg.inv(e2d)
print(inv1)
ind = a2d>2
print(ind)
print(a2d[ind])
print(np.sum(A))
print(np.mean(A))
print(np.median(A))
print(np.var(A))
print(np.std(A))
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a)
b = a[:2,1:3] #Last exclusive
print(b)
b[0, 0] = 77
print(b)
print(a)
c = np.copy(a)
print(c)
c[0,0] = 105
print(c)
print(a)
a = np.array([[1,2], [3, 4], [5, 6]])
print(a)
print(a[[0, 1, 2], [0, 1, 0]]) ## localtion (0,0), (1,1), (2,0)
print(np.array([a[0, 0], a[1, 1], a[2, 0]]))
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(a)
b = np.array([0, 2, 0, 1])
print(b)
print(a[np.arange(4), b])
a[np.arange(4), b] += 10
print(a)
x = np.array([1, 2])
print(x.dtype)
x = np.array([1.0, 2.0])
print(x.dtype)
x = np.array([1, 2], dtype=np.int64)
print(x.dtype)
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
print(x+y)
v = np.array([1,2,3])
print(v) # Prints "[1 2 3]"
print(v.T) # Prints "[1 2 3]"
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.zeros_like(x)
print(y)
for i in range(4):
y[i, :] = x[i, :] + v
print(y)
print(x)
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(x)
v = np.array([1, 0, 1])
print(v)
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print(vv) # Prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
y = x + vv # Add x and vv elementwise
print(y) # Prints "[[ 2 2 4
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = x + v # Add v to each row of x using broadcasting
print(y) # Prints "[[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
v = np.array([[1,2,3],[4,5,6]])
w = np.array([4,5])
print(np.reshape(v, (3, 2)))
v = np.array([1,2,3]) # v has shape (3,)
w = np.array([4,5]) # w has shape (2,)
print(np.reshape(v, (3, 1)) * w)
help(np.random)
import matplotlib.pyplot as plt
x = np.arange(-100,100,0.5) # Return evenly spaced values within a given interval
y = x**2
plt.plot(x,y)
plt.xlabel('X-axis')
plt.ylabel('Y-label')
plt.show()
x = np.arange(1,101)
print(x)
print(x.shape)
marks = np.random.randint(1,100,100)
marks1 = np.random.randint(1,100,100)
width = 0.5
plt.bar(x,marks)
plt.show()
plt.plot(x,marks,'*') # displaying points
plt.plot(x,marks1,'+')
plt.show()
from scipy.misc import imread, imsave, imresize, imshow
img = imread('cat.jpg')
print(img.dtype, img.shape)
img_tinted = img * [1, 0.95, 0.9]
#plt.figure()
plt.imshow(img)
#plt.imshow(img_tinted)
plt.show()
plt.imshow(img_tinted)
plt.show()
imsave('cat_tinted.jpg', img_tinted)
# 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()
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()
a) https://github.com/kuleshov/cs228-material/blob/master/tutorials/python/cs228-python-tutorial.ipynb
b) https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
c) https://matplotlib.org/users/pyplot_tutorial.html
d) https://docs.scipy.org/doc/scipy/reference/tutorial/index.html
Click here to download the source code of this tutorial.