NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely. NumPy stands for Numerical Python.
pip install numpy
import numpy
import numpy as np
import numpy as np print(np.__version__)
arr = np.array([1, 2, 3, 4, 5]) print(arr)
arr = np.array(42)
arr = np.array([1, 2, 3, 4, 5])
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
a = np.array(42)
print(a.ndim)
arr = np.array([1, 2, 3, 4])
print(arr[0])
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])print('2nd element on 1st row: ', arr[0, 1])
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])print('Last element from 2nd dim: ', arr[1, -1])
arr = np.array([1, 2, 3, 4, 5, 6, 7])print(arr[:4])
arr = np.array([1, 2, 3, 4, 5, 6, 7])print(arr[1:5:2])
arr = np.array([1, 2, 3, 4]) print(arr.dtype)
arr = np.array([1, 2, 3, 4], dtype='S')
arr = np.array([1.1, 2.1, 3.1])newarr = arr.astype('i');
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr = np.array([1, 2, 3, 4, 5]) x = arr.copy()
y = arr.view()
print(x.base)
print(y.base)
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape)
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(4, 3)
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])newarr = arr.reshape(2, 3, 2)
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])print(arr.reshape(2, 4).base)
arr = np.array([[1, 2, 3], [4, 5, 6]])newarr = arr.reshape(-1)
arr = np.array([1, 2, 3]) for x in arr:
print(x)
arr = np.array([[1, 2, 3], [4, 5, 6]])for x in arr:
print(x)
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr:
print(x)
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for x in np.nditer(arr):
print(x)
arr = np.array([1, 2, 3])for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
print(x)
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])for x in np.nditer(arr[:, ::2]):
print(x)
arr = np.array([1, 2, 3])for idx, x in np.ndenumerate(arr):
print(idx, x)
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.array_split(arr, 3)
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]) newarr = np.array_split(arr, 3, axis=1)
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])newarr = np.hsplit(arr, 3)
arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4)
arr = np.array([6, 7, 8, 9])x = np.searchsorted(arr, 7,side='right')
arr = np.array([3, 2, 0, 1]) print(np.sort(arr))
arr = np.array([41, 42, 43, 44]) # Create an empty list
filter_arr = []
# go through each element in arr
for element in arr:
# if the element is higher than 42, set the value to True, otherwise False:
if element > 42:
filter_arr.append(True)
else:
filter_arr.append(False)
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
from numpy import random
x = random.randint(100)
x = random.rand() print(x)
x = random.choice([3, 5, 7, 9]) print(x)
x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(100))print(x)
arr = np.array([1, 2, 3, 4, 5]) random.shuffle(arr)
arr = np.array([1, 2, 3, 4, 5])print(random.permutation(arr))
x = random.normal(size=(2, 3)) print(x)
x = random.binomial(n=10, p=0.5, size=10)print(x)
x = random.poisson(lam=2, size=10) print(x)
x = random.uniform(size=(2, 3)) print(x)
x = random.logistic(loc=1, scale=2, size=(2, 3)) print(x)
x = random.multinomial(n=6, pvals=[1/6, 1/6, 1/6, 1/6, 1/6, 1/6]) print(x)
x = random.exponential(scale=2, size=(2, 3))print(x)
x = random.chisquare(df=2, size=(2, 3)) print(x)
x = random.rayleigh(scale=2, size=(2, 3)) print(x)
x = random.pareto(a=2, size=(2, 3)) print(x)
x = random.zipf(a=2, size=(2, 3)) print(x)
def myadd(x, y):
return x+y
myadd = np.frompyfunc(myadd, 2, 1)
print(myadd([1, 2, 3, 4], [5, 6, 7, 8])
print(type(np.add))
arr1 = np.array([10, 11, 12, 13, 14, 15])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.add(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.subtract(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.multiply(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 10, 8, 2, 33])
newarr = np.divide(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 6, 8, 2, 33])
newarr = np.power(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.mod(arr1, arr2)
print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])newarr = np.remainder(arr1, arr2)print(newarr)
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.divmod(arr1, arr2)
print(newarr)
arr = np.array([-1, -2, 1, 2, 3, -4])newarr = np.absolute(arr)print(newarr)
arr = np.trunc([-3.1666, 3.6667])
print(arr)
arr = np.fix([-3.1666, 3.6667])
print(arr)
arr = np.around(3.1666, 2)
print(arr)
arr = np.floor([-3.1666, 3.6667])
print(arr)
arr = np.ceil([-3.1666, 3.6667])
print(arr)
arr = np.arange(1, 10)
print(np.log2(arr))
arr = np.arange(1, 10)
print(np.log10(arr))
arr = np.arange(1, 10)
print(np.log(arr))
from math import log
import numpy as np
nplog = np.frompyfunc(log, 2, 1)
print(nplog(100, 15))
arr1 = np.array([1, 2, 3])
arr2 = np.array([1, 2, 3])
newarr = np.sum([arr1, arr2])
arr1 = np.array([1, 2, 3])
arr2 = np.array([1, 2, 3])
newarr = np.sum([arr1, arr2], axis=1)
print(newarr)
arr = np.array([1, 2, 3])
newarr = np.cumsum(arr)
print(newarr)
arr = np.array([1, 2, 3, 4])
x = np.prod(arr)
print(x)
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
newarr = np.prod([arr1, arr2], axis=1)
print(newarr)
arr = np.array([5, 6, 7, 8])
newarr = np.cumprod(arr)
print(newarr)
arr = np.array([10, 15, 25, 5])
newarr = np.diff(arr)
print(newarr)
num1 = 4
num2 = 6
x = np.lcm(num1, num2)
print(x)
arr = np.array([3, 6, 9])
x = np.lcm.reduce(arr)
print(x)
num1 = 6
num2 = 9
x = np.gcd(num1, num2)
print(x)
arr = np.array([20, 8, 32, 36, 16])
x = np.gcd.reduce(arr)
print(x)
arr = np.array([90, 180, 270, 360])
x = np.deg2rad(arr)
print(x)
arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 2*np.pi])
x = np.rad2deg(arr)
print(x)
x = np.arcsin(1.0)
print(x)
arr = np.array([1, -1, 0.1])
x = np.arcsin(arr)
print(x)
base = 3
perp = 4
x = np.hypot(base, perp)
print(x)
ax = np.sinh(np.pi/2)
print(x)
x = np.arcsinh(1.0)
print(x)
arr = np.array([1, -1, 0.1])
x = np.arcsinh(arr)
print(x)
arr = np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])
x = np.unique(arr)
print(x)
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.union1d(arr1, arr2)
print(newarr)
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.intersect1d(arr1, arr2, assume_unique=True)
print(newarr)
set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])
newarr = np.setdiff1d(set1, set2, assume_unique=True)
print(newarr)
set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])
newarr = np.setxor1d(set1, set2, assume_unique=True)
print(newarr)