In [1]:
import numpy as np
In [2]:
#creating array by providing numerical values
arr = np.array([[1,2,3,4],[5,6,7,8]])
arr
Out[2]:
In [3]:
#numpy supports multidimensional array
arr = np.arange(12)
arr
Out[3]:
In [4]:
#we can format the array to different dimensions
#Let's convert the arrray into 3 x 4
arr.reshape(3,4)
arr
Out[4]:
In [5]:
#The array is not reshaped as it returns a different objet
#To get what we intend let's assign the returned the object to arr itself
arr=arr.reshape(3,4)
arr
Out[5]:
In [6]:
#iterating over the array
for x in np.nditer(arr):
print(x)
In [7]:
#multiplication
arr*arr
Out[7]:
In [8]:
#addition
arr+arr
Out[8]:
In [9]:
#sum of all elements
arr.sum()
Out[9]:
In [10]:
#sum of each row separately
arr.sum(axis=0)
Out[10]:
In [11]:
#sum of each column separately
arr.sum(axis=1)
Out[11]:
In [12]:
#slicing
arr[0:2]
Out[12]:
In [13]:
#last row
arr[-1]
Out[13]: