NumPy – Required functions used while programming

NumPy– Library function explanation by Sagar Jaybhay

Use of linspace(), zeros() and NumPy data types?

a) linspace:

link- https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

It will return an even space numbers array. Means what?

np.linspace(5,15,num=8) #o/p array([ 5. , 6.42857143, 7.85714286, 9.28571429, 10.71428571,12.14285714, 13.57142857, 15. ])

In this example value, 5 is the starting point or we can say first value in the array.

15 stops value means it should be the last value in that array. Now important num=8 is the actual steps or you can say the count total number of items in that array. If I say 5 to 15 means starting is 5 and end is 15 this is 2 in array what about middle number num parameter calculate and divide items in evenly space manner whos last value is always our stop parameter.

Here point to remember is that it is close range means here to start and stop parameter are included but in the arange function, the stop parameter never includes means this is open range.

b) zeros():-

https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html

It create array of given shape but array is filled with 0’s only.

np.zeros((3,2),dtype=int) #o/p-array([[0, 0],
				       [0, 0],
				       [0, 0]])

c) ones() :

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html

np.ones((3,2))#o/p - array([[1., 1.],[1., 1.],[1., 1.]])

This function will return an array of a given size with 1 value each.

 3 dimensional array

np.ones((2,4,3))#o/p -array([[[1., 1., 1.],
	       [1., 1., 1.],
       		[1., 1., 1.],
		[1., 1., 1.]],

	        [[1., 1., 1.],
       		 [1., 1., 1.],
	        [1., 1., 1.],
	        [1., 1., 1.]]])

In this above example 2 or first argument it means how many times we get aarray here 2 separate section you see. 4 it means number of rows in each array and last is 3 is the column in each array.

Q) How to access an array element?

In this example, we declared the array like this

a=np.arange(35)
a.shape=(5,7)
a
#-----------------------
the output of this code is 
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])

Now I want to get 17 value how I get this?

a[2][3], a[2,3] 

in this first value is row number and second is column number and both row and column start with 0 th index position.

But when we have 2-dimensional array like

a=np.arange(35*2)
a.shape=(2,5,7) 
# in this first parameter is dimension of an array the row and then columns
print(a ,a.size)

[[[ 0  1  2  3  4  5  6]
  [ 7  8  9 10 11 12 13]
  [14 15 16 17 18 19 20]
  [21 22 23 24 25 26 27]
  [28 29 30 31 32 33 34]]

 [[35 36 37 38 39 40 41]
  [42 43 44 45 46 47 48]
  [49 50 51 52 53 54 55]
  [56 57 58 59 60 61 62]
  [63 64 65 66 67 68 69]]] 

When you are playing with 2-dimensional arrays and A[1] this line means 1 array o/p is

array([[35, 36, 37, 38, 39, 40, 41],
      					     [42, 43, 44, 45, 46, 47, 48],
    					     [49, 50, 51, 52, 53, 54, 55],
  					     [56, 57, 58, 59, 60, 61, 62],
					     [63, 64, 65, 66, 67, 68, 69]])

In this a[1][2] it means array of 1 position with 2 row and the o/p is of this line is

array([49, 50, 51, 52, 53, 54, 55]) 

you get one dimensional array by using 2 values.

a[1][2][3] #o/p 52

Boolean Mask Array:

a=np.arange(2,10) 
# a is array([2, 3, 4, 5, 6, 7, 8, 9])
b=a>7
#o/p- array([False, False, False, False, False, False,  True,  True])

In this example, we create one array and from that, we use a comparison operator to compare the value. When It starts comparing with array then it’s actually comparing with each element in the array then it’s shown last is a Boolean array.

Structured Array:

In NumPy we mostly store homogeneous data. But in this, there might be the case where we required a heterogeneous data and this data required to hold In python program. So how we hold suppose an example

Employee data having age, Name, Salary, EmpID. So we can hold by creating a separate array for each field but it is not a feasible solution so we required some mechanism that we will able to show this data is related and belongs to same person or employee. For that Numpy provides a way to this. It might be a multi-dimensional array .

 Steps To Create Structured Data:

1) Create a data definition first this is also called compounded array

Employee_DataDefination=[(‘name’,’S6′),(‘salary’,’f8′),(‘gender’,’S1′)]

2) Then create arrays with zeros() or ones() function with defined data types which we are created above line.

a)employee_array=np.zeros((4),dtype=Employee_DataDefination)

b) other_way=

np.zeros(4, dtype={'names':('name', 'salary', 'gender'),'formats':('U6', 'f8', 'S1')})

The output of these 2 are the same.

We are giving formats in both example meaning of this explained below. the number after this initial means maximum limit.

The output of these 2 are the same.

We are giving formats in both example meaning of this explained below. the number after this initial means maximum limit.

Character Description Example
‘b’ Byte np.dtype(‘b’)
‘i’ Signed integer np.dtype(‘i4’) == np.int32
‘u’ Unsigned integer np.dtype(‘u1’) == np.uint8
‘f’ Floating point np.dtype(‘f8’) == np.int64
‘c’ Complex floating point np.dtype(‘c16’) == np.complex128
‘S’, ‘a’ String np.dtype(‘S5’)
‘U’ Unicode string np.dtype(‘U’) == np.str_
‘V’ Raw data (void) np.dtype(‘V’) == np.void

Advanced Structure array:

You also able to create each element which holds the matrix or array.

tp = np.dtype([('Empid', 'i8'), ('fat', 'f8', (3, 3))])
X = np.zeros(1, dtype=tp)
print(X[0])
print(X['fat'][0])

RecordArrays

Numpy also provides np.recarray class, which is identical to a structured array but one more feature is that fields can be accessed as attributes rather than a dictionary. This is a thin wrapper around the structure array.

Data=[('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. )
 ('Doug', 19, 61.5)]
data_rec = data.view(np.recarray)
data_rec.age #o/p array([25, 45, 37, 19], dtype=int32)
other_way=np.zeros(4, dtype={'names':('name', 'salary', 'gender'),
                          'formats':('U6', 'f8', 'S1')})

other_way[0]=('SagarJaybhay',1234567890,'M2')
other_way[1]=('Rani',12345,'F')
other_way[2]=('Raju',6789,'M')
other_way["name"] #o/p array(['SagarJ', 'Rani', 'Raju', ''], dtype='<U6')

Views and Copies:

In views, we make multiple references to same memory location while in copies we copy data from one memory location to a different memory location and in views, we have more than one reference which points to same memory location.

first_array=np.array([121,0,122,1,14,15])
second_array=first_array

Check 2 arrays memory location and data

first_array is second_array #o/p True

Check Memory location

print('First Array Memory Location ', id(first_array), ' Second Array value ',id(second_array)) 
#o/p is the same memory location

In below if you change the value of one array is reflected in the second reference array

first_array[1]=2000
first_array ,second_array

If you change the shape of the second array this will reflect the first array also.

second_array.shape=3,2

Now we create a third array with the same value as the first array

third_array=np.array([ 121, 2000,  122,    1,   14,   15])

in this when we use equality operator the result is True but when we use Is keyword then showing false.

third_array == first_array  #o /p True
third_array is first_array  #o/p false

Now we check the memory address of the first and third array

print('First Array Memory Location ', id(first_array), ' Third Array value ',id(third_array))

Views :

It is another way of seeing the data but if youthink technically data shared between 2 objects are the same. So the questionis how you create a view 2 ways

  1. By Slicing the original array
  2. Change the dtype

So we have viewed () method in numpy. By using this we can create another copy of the same data but whatever operation we do on that is not reflected on the second array.

fourth_array=first_array.view()
np.reshape(fourth_array,fourth_array.size) #o/p –Used to reshape the array 
first_array

so in this, we change the shape is not reflecting the original one but if you change the value then the value will be reflected in both arrays.

fourth_array=first_array.view()
fourth_array=np.reshape(fourth_array,fourth_array.size)
fourth_array[3]=-10001
print('First Array Memory Location ', id(first_array), ' Fourth Array value ',id(fourth_array)) #o/p memory location Is different for these 2 arrays

Copy (Deep Copy):

Copy() function did the deep copy means it copies array with data and doesn’t share an original copy.

fifth_array=first_array.copy()
fifth_array
#print('First Array Memory Location ', id(first_array), ' Fifth Array value ',id(fifth_array))

fifth_array=np.reshape(fifth_array,fifth_array.size)
#first_array
fifth_array[3]=-20002
fifth_array

Now when you use copy function then it is create a fresh new copy and if any one change is not affected or modify the other one.

Adding and Removing Elements from Numpy Arrays

1) Append:- This method appends the values at the end of the array

a.  numpy.append(arrvaluesaxis=None) this is the method which has 3 parameters and one axis is optional or default.

1) arr: This is our array on which second parameter values are appended.

2) Values: this is the second parameter whose values appended to the first array.

3) Axis: if you don’t specify this both values and array are flattened.

a=np.array(np.arange(24)).reshape(2,3,4)
b=np.append(a,[31,32,33,34])

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *