Basics of Numpy - Arithmetical operation and arrays handling code samples -1

Numpy is Numerical python, and as name suggests it primarily focuses on numerical operations . The array handling and the various operations to slice dice the data applying various transforms etc., is the zone of operations numpy has brought under its umbrella.


  • Basics on array from datatype, zeros, ones, shape, reshape, flags, dimensions

[It is mandatory to import the numpy package when playing around with any arithmetical operation or array by using this code in first line - import numpy as np ]

#numpy.ones
x = np.ones([2,2], dtype = int) 
print x

#numpy.zeros
# custom type  
x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  
print x 
y = np.zeros((5,), dtype = np.int) 
print y


#numpy.empty
x = np.empty([3,2], dtype = int) 
print x
a = np.array([[1,2,3],[4,5,6]]) 
print a.shape


# this resizes the ndarray 
a = np.array([[1,2,3],[4,5,6]]) 
a.shape = (3,2) 
print a 

a = np.array([[1,2,3],[4,5,6]]) 
b = a.reshape(3,2) 
print b
# an array of evenly spaced numbers 
a = np.arange(24) 
print a


# this is dimensional array 
a = np.arange(24) 
a.ndim  
# now reshape it 
b = a.reshape(2,4,3) 
print b 
# b is having three dimensions


# dtype of array is int8 (1 byte) 
x = np.array([1,2,3,4,5], dtype = np.int8) 
print x.itemsize

# dtype of array is now float32 (4 bytes) 
x = np.array([1,2,3,4,5], dtype = np.float32) 
print x.itemsize


Flags - an important aspect in array handling.
C_CONTIGUOUS (C) -The data is in a single, C-style contiguous segment
F_CONTIGUOUS (F) -The data is in a single, Fortran-style contiguous segment
OWNDATA      (O) -The array owns the memory it uses or borrows it from another object
WRITEABLE    (W) -The data area can be written to. Setting this to False locks the data, making it read-only
ALIGNED      (A) -The data and all elements are aligned appropriately for the hardware
UPDATEIFCOPY (U) -This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array
#numpy.flags
x = np.array([1,2,3,4,5]) 
print x.flags

a = np.array([1,2,3]) 
print a


# more than one dimensions 
a = np.array([[1, 2], [3, 4]]) 
print a


# minimum dimensions 
a = np.array([1, 2, 3,4,5], ndmin = 2) 
print a


# dtype parameter 
a = np.array([1, 2, 3], dtype = complex) 
print a


# start and stop parameters set  numpy.arange
x = np.arange(10,20,2) 
print x

  • Create array from list,tuple,  list of tuples, tuples, tuple of tuples or tuple of lists numpy.asarray

#convert list to ndarray
x = [11,22,33] 
a = np.asarray(x) 
print a
#set datatype
b = np.asarray(x, dtype = float) 
print b


# ndarray from tuple
x = (11,22,33) 
a = np.asarray(x) 
print a


# ndarray from list of tuples 
x = [(11,32,43),(44,65)] 
a = np.asarray(x) 
print a


#from buffer 
s = 'ArticlesonTesting' 
a = np.frombuffer(s, dtype = 'S1') 
print a


 #from writer

# create list object using range function 
list = range(20) 
print list

# obtain iterator object from list 
list = range(20) 
it = iter(list)  
# use iterator to create ndarray 
x = np.fromiter(it, dtype = float)
print x


#numpy.arange - Returns an ndarray object containing evenly spaced values within a given range. 

numpy.arange(start, stop, step, dtype)

x = np.arange(10,20,2,float)

print x

#numpy.linspace- Similar to arange() function and instead of step size, the number of evenly spaced values between the interval is specified. 

numpy.linspace(start, stop, num, endpoint, retstep, dtype) [num-number of evenly spaced samples to be generated. Default is 50]

x = np.linspace(10,20,5)

print x

# endpoint set to false 
x = np.linspace(10,20, 5, endpoint = False) 
print x
# find retstep value 
x = np.linspace(1,2,5, retstep = True)
print x

#numpy.logspace - Returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10.

numpy.logspace(start, stop, num, endpoint, base, dtype)

# default base is 10 
a = np.logspace(1.0, 2.0, num = 10) 
print a
# set base of log space to 2 
import numpy as np 
a = np.logspace(1,10,num = 10, base = 2)
print a


Post a Comment

Previous Post Next Post