Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
# operations against a number
first_matrix = np.array([2,2,2])
print (first_matrix)
plus_one_matrix = first_matrix + 1
print (plus_one_matrix)
sub_one_matrix = first_matrix - 1
print (sub_one_matrix)
division_matrix = first_matrix / 2
print (division_matrix)
multi_matrix = first_matrix * 3
print (multi_matrix)
# operations against another matrix
second_matrix = np.array([10,10,10])
print (second_matrix)
# adding
add_matrix = first_matrix + second_matrix
print (add_matrix)
# subtracting
sub_matrix = first_matrix - second_matrix
print (sub_matrix)
# multiplying
mul_matrix = first_matrix * second_matrix
print (mul_matrix)
# dividing
div_matrix = first_matrix / second_matrix
print (div_matrix)
np.random.seed(100)
random_matrix = np.random.random((1,3))
print (random_matrix)
# [0, 3)
zero_to_3 = 3 * np.random.random((1,3))
print (zero_to_3)
# [-10 to 10)
ten_ranges = 20 * np.random.random((1,3)) - 10
print (ten_ranges)
# reshaping
another_matrix = np.array([[1,2],[3,4]])
print (another_matrix)
# [[1,2]
# [3,4]]
one_by_four = np.reshape(another_matrix, (1,4))
print (one_by_four)
# [[1,2,3,4]]
four_by_one = np.reshape(another_matrix, (4,1))
print (four_by_one)
# [[1]
# [2]
# [3]
# [4]]
# transpose
original = np.array([[1,2,3], [4,5,6]])
print (original)
print (original.T)
# argmax
maxing = np.array([1,2,3,4])
max_elem = np.argmax(maxing)
print (max_elem)
# applying a function to everything in the matrix
test = np.array([-1,1])
def relu(x):
return (x>0)*x
print (relu(test))
# np.outer, input two vectors
a = [1,2,3]
b = [4,5,6]
print (np.outer(a,b))
#[[ 4 5 6]
# [ 8 10 12]
# [12 15 18]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment