Data | Web

Shekhar

Biswas

  •  

    Hands on with NumPy

     

    Practice builds habits and good habits bring success.

    - SB
     

     

    NumPy is a Python library which stands for ‘Numerical Python’. It is the core module for scientific computing in Python. Although NumPy arrays are comparable to Python’s built-in list data type, NumPy arrays provide much more efficient storage and data operations as the size of arrays grow large. Efficient storage and manipulation of numerical arrays is absolutely fundamental to the process of doing data science. Therefore, all aspiring Data Scientists are supposed to have strong base in NumPy.
     

    Before solving the questions below, readers are requested to go through the official doc.

     

    1. In an  array, extract all the odd & even numbers into two separate arrays.

     

    Sample Input:

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

    Sample output:

    odd_array = array([1, 3, 5, 7, 9])

    even_array = array([2, 4, 6, 8])

     

    2. Replace all the odd numbers in a given array with '-1'.

    Sample Input:

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

    Sample output:

    new_array = array([-1, 2, -1, 4, -1, 6, -1, 8, -1])
     

    3. Get the common elements of 2 arrays.

    Sample Input:

    a = np.array([1,2,3,2,3,4,3,4,5,6,10])
    b = np.array([7,2,10,2,7,4,9,4,9,8])

    Sample output:

    common_element = array([2,4,10])

     

    4. Get the positions/ index where elements of 2 different arrays match.

    Sample Input:

    a = np.array([1,2,3,2,3,7,3,4,5,6])
    b = np.array([7,2,10,2,7,7,9,0,9,8])

    Sample output:

    matched_index = (array([1, 3, 5]),)

     

    5.  Get all items between 5 and 8 from a given array.

    Sample Input:

    a = np.array([2, 6, 7, 9, 10, 3, 27])

    Sample output:

    output = (array([6,7]),)

     

    6. Suppressing the scientific notation: Print rand_arr by suppressing the scientific notation (like 1e10).

    Sample Input:

    # Create the random array
    np.random.seed(100)
    rand_arr = np.random.random([3,3])/1e3
    rand_arr

    #> array([[  5.434049e-04,   2.783694e-04,   4.245176e-04],
    #>        [  8.447761e-04,   4.718856e-06,   1.215691e-04],
    #>        [  6.707491e-04,   8.258528e-04,   1.367066e-04]])

    Sample output:

    #> array([[ 0.000543,  0.000278,  0.000425],
    #>        [ 0.000845,  0.000005,  0.000122],
    #>        [ 0.000671,  0.000826,  0.000137]])

     

    7. Find the mean, median, standard deviation of iris's sepallength (1st column).

    To get the dataset, please follow the below steps:

    url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
    sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

     

    8. Find the 5th and 95th percentile of iris's sepallength.

    Below steps will fetch the data:

    url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
    sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

     

    9. Get the positions of top 5 maximum values in a given array.

     

    Sample Input:

    np.random.seed(100)
    array = np.random.uniform(1,50, 20)

    Sample output:

    Index of the max 5 items.

     

    10. Convert array_of_arrays into a flat linear 1d array.

     

    Sample Input:

    arr1 = np.arange(3)
    arr2 = np.arange(3,7)
    arr3 = np.arange(7,10)

    array_of_arrays = np.array([arr1, arr2, arr3])


    array_of_arrays will look like:

    array([array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])], dtype=object)

    Sample output:

    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

     

     

     

     

     

     

    Possible Solutions

     

     

Comments

  • (no comments)

Post Comments