Numpy Part-2

In the last post I have left some points which I’ll try to sum up here.

          1. While using vstack() keep in mind the no.of columns must be equal in both the arrays and so with the case if one is using row_stack() and.
          2. Similarly while using hstack() or column_stack() keep in mind that there are equal no. of columns in both the matrices.
          3. The two methods concatenate() and append()
            perform the same function but the underlying difference in their implementation. The append() method is implemented in terms of latter.
          4. Like there are methods for stacking there are methods of splitting too vsplit() and hsplit(). If you want to split at particular index then keep in mind the shape of the matrix
          5. You can also create an array of all ones by np.ones((3,4)) or array of all zeros np.zeros((3, 4)), an array of constant np.full((3,3), 4, dtype = int64) and an array of random nos. np.random.random((3, 4)).

This is all for this time I’ll be adding these points if I gather some more information.

Advertisements

Numpy

The first step in machine learning is to learn is to choose appropriate tool to work upon.I began my search for that tool and finally ended up with SciKit learn. But before beginning my journey, I began working upon my skills on the libraries that are used along with scikit learn.

The first library which I chose to study was numpy. Numpy library according to much I have learnt is used to handle arrays. Now moving some formal definition of NumPy as per wikipedia is “NumPy  is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.”

Let’s begin with NumPy

import numpy as np

Later in while coding the name numpy would be seem tedious to use thus it is used in short form of np.
#Creating an Array
arr = np.array([1, 2, 3])
print(arr)

#Creating an array with range of nos. from 0, 100
arrOne = np.arange(0, 100)
#Creating an array within the range along with step size
arrTwo = np,linespace(1, 3, 0.5)

In Python, list and arrays seems one or the other form but they differ a lot in terms of their size and time taken to process them. Try running this code on your system.

#Comparing the list and array
list = range(0, 1000)
arrThree = np.arange(0, 1000)
import sys
print(sys.getsizeof(1) * len(list))
print(D.size * D.itemsize)
import time
L1 = range(1000)
L2 = range(1000)
start = time.time()
result = [(x+y) for x,y in zip(L1,L2)]
print(time.time()-start)
start = time.time()
AR1 = np.arange(1000)
AR2 = np.arange(1000)
result = AR1 + AR2
print(time.time()-start)

There might be the case that output may be same then in that case try to increase the range.

There are some functions in numpy that help to gain better insight of the array
#Understanding the array

arrFour = np.array([(1, 2, 3), (4, 5, 6),(7, 8, 9)])
print(arrFour.ndim) #Ouput : 2 (dimension)
print(arrFour.itemsize) #Ouput : 4 (item Size)
print(arrFour.dtype) #Ouput : int32 (data type)
print(arrFour.size) #Ouput : 9 (no. of elements)
print(arrFour.shape) #Ouput : (3, 3) (shape)
print(arrFour.reshape(9,1))

In numpy reshaping preserves the size of the array that is no new element is added or deleted while reshaping it is just a transformation from one form to another.

Two different array can be stacked to one n another either horizontally or vertically


arrFive = np.array([(1, 2, 3), (4, 5, 6)])
arrSix = np.array([(7, 8, 9), (10, 11, 12)])
#Column wise stacking
np.hstack((arrFive, arrSix))
#Row wise stacking
np.vstack((arrFive, arrSix))

All the elements in the n-dimensional array can be used in 1d by using the function ravel() or flatten().


arrSeven = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
arrSeven.ravel()
arrSeven.flatten()

The output of both the functions will be same with the only difference that flatten() returns copy of the array whereas ravel() returns the original view of the array.

To get a view of a particular set of row or column following code can be used

arrEight = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
print(arr[0:2])
print(arr[0:2, 2])

In python the indexing of the array starts from 0. In this code snippet the first statement will print print the row (1, 2, 3) and (4, 5, 6) while the second statement will going to print the 3rd elements of each row that are [3, 6].

There are other some standard mathematical operations that can be performed on the arrays such as square root (sqrt), log(log or log 10), standard deviation(std), sin etc.


arrNine = np.array([(1, 2, 3), (4, 5, 6),(7, 8, 9)])
print(np.sqrt(arrNine))
print(np.std(arrNine))
print(np.log10(arrNine))
print(np.log(arrNine))

Communication in Aviation

If you’re a frequent air traveller then you must have experienced the delays and cancellation of flights due to bad weather.

To us it is easily conveyed through calls or messages from the airlines but what about those aeroplanes which are still in the sky preparing to land or are on their way to destination?

Obviously there is Air Traffic Control room to take care of such situations but just think about the number of flights to be informed and this passing of information to take place in the form of text messages is a tedious task. If for a second, this is ignored then what if the pilot is informed about problem but due to different ethnicity they may not understand each other.

And in such situation the person in ATC room doesn’t have enough time to dedicate more than a minute or two to this. So now what?

The solution for this problem was NOTAM.

Notice to Airmen abbreviated as NOTAM is a notice filed with an aviation authority to alert pilots of potential hazards along a flight route or at a location that could affect the safety of the flight.

It is done in two steps:

  1. NOTAM is filed with an aviation authority to alert pilots about any en route hazard.
  2. Authority in turn provides means to disseminate relevant NOTAM to the pilot.

There are different version of NOTAM based on their usage.

  • BIRDTAM: when there is a passage of flock of birds in airspace
  • SNOWTAM: notification of runway status w.r.t. snow, ice and standing water
  • ASHTAM: notification of significant change in volcanic ash or dust contamination.

There are many other reasons to use NOTAMS like in case of temporary erection of tall obstruction, inoperable lights on tall obstruction, military exercises or flight of important people.

There are various sites which allow airline dispatchers, pilots and airport authority personnel to search for active NOTAMs.

There are also apps and websites available to decode NOTAMs.

For more information regarding NOTAM decoding please go through this link:

http://thinkaviation.net/notams-decoded/

PS: NOTAM is not actually a two-step procedure but for better understanding I had broken it in two steps 🙂

Enjoy Learning!!