Professional Documents
Culture Documents
832ec6c0 1602239712292
832ec6c0 1602239712292
• Output-array([[ 0, 1, 2, 3, 4],
• [ 5, 6, 7, 8, 9],
• [10, 11, 12, 13, 14]])
• a.shape
• (3, 5)
Various array function example
• a.shape-Tuple of array dimensions.
• The shape property is usually used
• to get the current shape of an array,
• Example>>> a.shape
output=(3, 5)
• a.ndim-This function is used to display the dimensions of an array
• Example
• >>> a.ndim
• output=2
Various array function example
• a.dtype-describes how the bytes in the fixed-size block of memory corresponding
to an array item should be interpreted. It describes the following aspects of the
data:
• Type of the data (integer, float, Python object, etc.)
• Example
• a=np.array([2.6, 3, 4])
• >>> a.dtype
• Output-dtype('int')
• >>> b = np.array([1.2, 3.5, 5.1])
• >>> b.dtype
• Output-dtype('float')
Various array function example
• a.size-this function returns the count of total numbers of eleme
array
Example a = np.arange(15).reshape(3, 5)
print(a.size)
• output-15
• Type()-this function returns the class to which the numpy array
• Example
• >>> type(a)
• <class 'numpy.ndarray'>
• >>> b = np.array([6, 7, 8])
• >>> b
Various array function example
• The function zeros creates an array full of zeros,, and
• np.zeros( (3,4) )
Output-array([[ 0., 0., 0., 0.],
• [ 0., 0., 0., 0.],
• [ 0., 0., 0., 0.]])
Various array function example
• the function empty creates an array whose initial content is random and depends
on the state of the memory. By default, the dtype of the created array is float
• >>> np.empty( (2,3) ) # uninitialized, output may vary
• array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260],
• [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
Various array function example
• f = np.eye(4)-This function creates n array where all elements are
equal to zero, except for the k-th diagonal, whose values are equal to
one.
• print(f)-output- array([[1., 0., 0., 0.],
• [0., 1., 0., 0.],
• [0., 0., 1., 0.],
• [0., 0., 0., 1.]])
Various array function example
• g = np.full((4,4),6)- This function creates n array where all ele
are identicals
• Output- array([[6, 6, 6, 6],
• [6, 6, 6, 6],
• [6, 6, 6, 6],
• [6, 6, 6, 6]])
Array Creation
• import numpy as np
• a = np.array([2,3,4])
• print(a)
a = np.array(1,2,3,4) # WRONG
• print(a)
• a = np.array([1,2,3,4]) # RIGHT
• print(a)
• b = np.array([(1.5,2,3), (4,5,6)])
print( b)
• Output-array([[ 1.5, 2. , 3. ],
• [ 4. , 5. , 6. ]])
• c = np.array( [ [1,2], [3,4] ], dtype=complex )
• c
• Output-array([[ 1.+0.j, 2.+0.j],
• [ 3.+0.j, 4.+0.j]])
Array Creation
• b = np.array([12,23,34,45])
>>> b**2
• a = np.array( [20,30,40,50] )
>>> a<35
• Output-array([ True, True, False, False])
Matrix operations
A = np.array( [[1,1],[0,1]] )
B = np.array( [[2,0],[3,4]] )
A *( B ) # elementwise product
•OUTPUT-array([[2, 0],
• [0, 4]])
Matrix operations
A *( B ) # elementwise product
A@B # matrix product
•array([[5, 4],
• [3, 4]])
A.dot(B) # another matrix product
•array([[5, 4],
• [3, 4]])
Matrix operations
• >>> a = np.arange(10)
• >>>print( a)
• >>>print( a.sum())
• >>>print( a.min())
• >>> print(a.max())
Other Operations
• >>> b = np.arange(12).reshape(3,4)
• >>> b
• Output-array([[ 0, 1, 2, 3],
• [ 4, 5, 6, 7],
• [ 8, 9, 10, 11]])
>>> b.sum(axis=0) # sum of each column
Output-array([12, 15, 18, 21])
>>> b.min(axis=1) # min of each row
array([0, 4, 8])
Other Operations
• b = np.arange(12).reshape(3,4)
• print(b)
array([[ 0, 1, 2, 3],
• [ 4, 5, 6, 7],
• [ 8, 9, 10, 11]])
• >>> B = np.arange(3)
• >>> B
• Output-array([0, 1, 2])
• >>> np.exp(B)
• Output-array([ 1. , 2.71828183, 7.3890561 ])
• >>> np.sqrt(B)
• array([ 0. , 1. , 1.41421356])
• >>> C = np.array([2., -1., 4.])
• >>> np.add(B, C)
• array([ 2., 0., 6.])
Indexing, Slicing and Iterating
• One-dimensional arrays can be indexed, sliced and iterated over, much
like lists and other Python sequences.
• >>> a = np.arange(10)**3
• >>>print( a)
• array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
• >>> a[2]
•8
• >>> a[2:5]
• array([ 8, 27, 64])
Indexing, Slicing and Iterating
• for i in range(0,2):
• for j in range(0,3):
• if (a[i][j]%2==0):
• print(”Even numbers are…." ,a[i][j])
2D array with for loop
• a=np.array(([1,2,3],[4,5,6]))
• s=0
• for i in range(0,2):
• for j in range(0,3):
• s=s+a[i][j]
•
• print("Sum of all the elements are:" ,s)
from numpy import *
• a=np.array(L)
• a.shape=(2,2)
• print(a)
•
from numpy import *
from numpy import *
• from numpy import *
• a= array([10,20,30.5,-40])
• print("1",a)
• print("2",a+5)
• print("3",a-5)
• print("4",a*5)
• print("5",a/5)
• print("6",a%5)
• print("7",(a+5)**2-10)
• print("8",sin(a))
from numpy import *
• from numpy import *
• a=array([1,2,3,0])
• b=array([0,2,3,1])
• c=a==b
• print(“VALUE OF C1…..”,c)
• c=a>b
• print(“VALUE OF C2…..”, c)
• c=a<=b
• print(“VALUE OF C3…..”, c)
c=a>b
• print(“VALUE OF C4…..”, any(c))
from numpy import *