import numpy as np
def cv(*args: int) -> np.array:
"""
Takes n integers, returns a n-dimensional vector column
:param *args: values to be integrated into array
:type *args: float
:returns: n-dimensional vector column
:returns type: np.array
"""
a = np.array(args)
return a.reshape(-1, 1)
cv(1,2,3)
import numpy as np
def cv(input_list: list) -> np.array:
"""
Takes n integers, returns a n-dimensional vector column
:input_list type: list(int)
:returns type: np.array
"""
a = np.array(input_list)
return a.reshape(-1, 1)
cv([1,4,2])
import numpy as np
def transpose(input_array: np.array) -> np.array:
"""
Returns the transpose of a given array
:returns type: np.array
"""
return np.transpose(input_array)
transpose(
np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
)
import numpy as np
def eqtest(
first_array: np.array,
second_array: np.array,
tolerance: float =0
) -> bool:
"""
Takes two arrays, compares them, with a tolerance of {tolerance}.
"""
if tolerance == 0:
return (first_array == second_array).all()
max_difference = max(abs(first_array - second_array))
return max_difference < tolerance
a=np.array([1,2,3])
b=np.array([0.74,2.3,3.01])
print(eqtest(a,b,0.5))
print(eqtest(a,b))
import numpy as np
def has_a_nan(input_array: np.array) -> bool:
"""
Checks wether a given array contains any NaN
"""
return np.isnan(input_array).any()
print(
has_a_nan(
np.array(
[1, np.NaN]
)
)
)
import numpy as np
print(
np.array([
[0]*10,
[1]*10,
[5]*10
])
)
import numpy as np
print(
np.array(
list(range(30, 72, 2))
)
)
import numpy as np
def randzeroone(length: int) -> np.array:
return np.random.rand(1, length)
print(
randzeroone(3)
)
import numpy as np
def randzeroone(length: int) -> np.array:
return np.random.rand(1, length)
print(
randzeroone(15)
)
import numpy as np
def create_vector() -> list:
return np.arange(15, 56)
print(
create_vector()[1:-1]
)
import numpy as np
# Quick and dirty
a = list()
for i in range(8):
b = list()
for j in range(8):
b.append(
1 if (i+j)%2==0 else 0
)
a.append(b)
# The numpy way
chess = np.zeros(64).reshape(8,8)
chess[1::2,1::2] = 1
chess[0::2,0::2] = 1
print(a, chess)
vector = np.random.rand(1, 100)
result = -10 + 20 * vector
result
init_vector = np.random.rand(1, 100)
vector = 20 * init_vector
vector[(vector >= 9) & (vector <= 15)] *= -1
vector
def multiply(a: np.array, b: np.array) -> np.array:
return a * b
a = np.array([1.0, 2.0, 3.0])
b = np.array([2.0, 2.0, 2.0])
multiply(a, b)
5 + 11 * np.random.rand(3, 4)
np.eye(3)
np.ones(4) - np.eye(4)
10 * np.random.rand(3,3,3)
def compute_sums(input_array: np.array) -> list:
"""
Returns the sum of all elements, the sum of each column and the sum of each row
"""
return (
np.sum(input_array),
np.sum(input_array, axis=0),
np.sum(input_array, axis=1)
)
a = np.array([[0, 1], [0, 5]])
print(compute_sums(a))
test_vector = np.random.rand(0, 100000)
np.save('sizetest.npy', vector)
# Result : 0.9 kb
import random
test_list = [random.random() for i in range(100000)]
with open('sizetest.txt', 'w') as f:
f.write(str(test_list))
# Result : 1.9 Mb
matrix = np.arange(1, 10).reshape(3, 3)
result = matrix[2]
result
# Pythonic way
def closest_value_finder(
input_array: np.array,
value_to_find: float
) -> float:
"""
Returns the closest element from value_to_find of input_array
"""
closest_value_found = input_array[0]
for elem in input_array:
if abs(value_to_find - elem) < abs(value_to_find - closest_value_found):
closest_value_found = elem
return closest_value_found
a = list(range(100))
print(closest_value_finder(a, 34.990622))