Useful attributes of an ndarray object are:
ndarray.ndim: the number of dimensions of the array.
ndarray.shape: A tuple of integers showing the size of the array in each dimension.
ndarray.size: Total number of elements in the array.
ndarray.dtype: Type of the array elements.
import numpy as np
x=[1,2,5,3,6, 4 ,5 ]
y=np.array(x)
z=np.array([[1,2,3],[9,8,7]]) # Create an array from a list
print(x)
print(y)
print (type(x))
print(type(y))
print(z)
np.array(((1,2,3),(9,8,7))) # Create an array from a tuple
a = np.array([[1.0,2,3],[9,8,7]]) # Type of array will be float
a.dtype
print(a)
a = np.array([[1,2,3],[9,8,7]],dtype=float) # Specify type explicitly
a.dtype
np.zeros((3,7)) # Create array of zeros
np.ones((3,2)) # Create array of ones
np.arange( 0, 1.1, .2 ) # Create array from a sequence of numbers
np.linspace(.1,.7,5 ) # Create 5 linearly spaced numbers from .1 to .7
Array creation functions
a=np.linspace(1,6,6 ) # Create a 1 by 6 array
b=a.reshape(3,2) # Reshape the array into a 3 by 2 array
print("a = ",a)
print("b = ",b)
a = np.array([[1,2,3],[9,8,7]]) # create a 2 by 3 array
b=a.ravel()
print("a = ",a)
print("b = ",b)
a = np.array([[1,2,3],[9,8,7]]) # create a 2 by 3 array
b = np.sin(a)
print (b)
NOTE : All arithmetic operations in numpy are applied elementwise. This includes multiplication and division of arrays. The matrix product of two arrays should be performed by using the dot function.
a = np.array([1, 2, 3, 4])
a+5 # Scalar, 5, is added to every element of array
a = np.array([1, 2, 3, 4])
a*3 # Every element of array is multiplied by the scalar
a = np.array([1, 2, 3, 4])
a/3.0 # Every element of array is divided by the scalar
a=np.array([[1,2,3],[9,8,7]])
b=np.array([[6,5,4],[2,4,6]])
print(a)
print(b)
c=a*b # Elementwise multiplication
d=np.dot(a,b)
print(c)
print(d)
a=np.array([[1,2,3],[9,8,7]])
b=np.array([[6,5,4],[2,4,6]])
a/b # Elementwise division
a=np.array([[1,2],[9,8]])
b=np.array([[6,5],[2,4]])
c=a*b # Elementwise multiplication
d=np.dot(a,b) # Mathematical multiplication
print("c = ",c)
print("d = ",d)
Broadcasting is Numpy's terminology for performing mathematical operations between arrays with different shapes.
If all input arrays do not have the same number of dimensions, a “1” will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions.
If an array has a size of 1 along a particular dimension it acts as if it has the size of the largest shape along that dimension by copying along that dimension.
a=np.array([[[1,3,4],[9,8, 5]],[[2,1,5],[8,6,2]]])
b=np.array([6,5,3])
print(np.shape(a))
print(np.shape(b))
print(a+b) # Elementwise addition with broadcasting
print("------------------\n",a[0])
Explanation:
Rule 1: Array a's shape is (2,2,3) and array b's shape is (3,). According to rule 1 of broadcasting a 1 is prepended to the shape of array b until it has the same dimensions as array a i.e. (1,1,3).
Rule 2: Array b is extended along the dimensions which have size 1 by copying the array as many time as needed until it is the same size as array a. This makes the size of array b to be (2,2,3) and its content to be [[[6,5,3],[6,5,3]],[[6,5,3],[6,5,3]]].
After applying the broadcasting rules, the two arrays are the same size and elementwise add operation is performed.
Below is another example of broadcasting in numpy
a=np.array([[1],[2],[3]])
print(np.shape(a))
b=np.array([4,5,6])
a*b # Elementwise multiplication with broadcasting