NumPy: Basics

Introduction to NumPy

  • Stands for Numerical Python.
  • Used in Data Analysis, ML, Image Processing etc.
  • Main data structure is ndarray containing elements of same data type.
  • We can think 1D numpy array as List, 2D as Matrix and 3D array as Cube.
  • Very fast due to optimized C and Fortran code under the hood.
  • Supports vectorized operations, broadcasting
  • Has pre-built functions for linear algebra, statistics, and more.

Creating NumPy Arrays

  • Can be created from Python list / tuple or NumPy functions.
  • Examples:
    1. my_list = [1, 2, 3] my_array = np.array(my_list) # or np.asarray(my_list) print(my_list) print(my_array) print(type(my_list)) print(type(my_array))
    2. my_tuple = [(4, 5, 6), (7, 8, 0)] my_array2 = np.array(my_tuple, dtype=float) # specify data type print(my_array2.shape) print(my_array2.dtype) print(my_array2.ndim)

Indexing and Slicing

  • Similar to Python lists but supports multi-dimensional indexing and slicing.
  • Indexing Examples:
    1. arr_1d = np.array([10, 20, 30, 40, 50]) print(arr_1d[0]) # 1st element arr_1d[-1] = 80 # Reassign Last element print(arr_1d[1:4]) # Slice from index 1 to 3 print(arr_1d[::2]) # Every 2nd element
    2. my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(my_array[0]) # First row print(my_array[1][2]) # or my_array[1, 2] Elements at 2nd row and 3rd column
    Slicing Examples:
    1. print(arr_1d[1:3]) # From index 1 to 2 (exclusive) print(arr_1d[1:]) # From index 1 to end print(arr_1d[:3]) # From start to index 2 (exclusive)
    2. print(my_array[0:2, 1:3]) # Slice 1st two rows and last two columns print(my_array[:, 0]) # All rows, 1st column print(my_array[1, :]) # 2nd row, all columns print(my_array[::2, ::2]) # Every 2nd row and column

Adding Elements to NumPy Arrays

  • Can be done using np.insert and np.append functions.
  • np.insert => Add element at specific index, np.append => Add element at end.
  • Insert:
    1. arr = np.array([1, 2, 3]) new_arr = np.insert(arr, 1, 10) # Insert 10 at index 1, shifts other right print(new_arr) # Output: [1, 10, 2, 3]
    2. arr_2d = np.array([[1, 2], [3, 4]]) new_arr_2d = np.append(arr_2d, [[5, 6]], axis=0) # Append as new row print(new_arr_2d)
    3. arr_2d = np.array([[1, 2], [3, 4]]) new_arr_2d = np.append(arr_2d, [[5], [6]], axis=1) # Append as new column print(new_arr_2d)
    Append:
    1. arr = np.array([1, 2, 3]) new_arr = np.append(arr, 4) # Append 4 at the end print(new_arr) # Output: [1, 2, 3, 4]
    2. arr_2d = np.array([[1, 2], [3, 4]]) new_arr_2d = np.append(arr_2d, [[5, 6]], axis=0) # Append as new row print(new_arr_2d)
    3. arr_2d = np.array([[1, 2], [3, 4]]) new_arr_2d = np.append(arr_2d, [[5], [6]], axis=1) # Append as new column print(new_arr_2d)

Removing Elements from NumPy Arrays

  • Can be done using np.delete function.
  • Examples:
    1. arr = np.array([1, 2, 3, 4]) new_arr = np.delete(arr, 1) # Delete element at index 1 print(new_arr) # Output: [1, 3, 4]
    2. arr_2d = np.array([[1, 2], [3, 4], [5, 6]]) new_arr_2d = np.delete(arr_2d, 0) # Delete first element and flatten print(new_arr_2d)
    3. arr_2d = np.array([[1, 2], [3, 4], [5, 6]]) new_arr_2d = np.delete(arr_2d, 0, axis=0) # Delete first row print(new_arr_2d)
    4. arr_2d = np.array([[1, 2], [3, 4], [5, 6]]) new_arr_2d = np.delete(arr_2d, 1, axis=1) # Delete second column

Vectorized Operations in NumPy

  • Vectorized operations perform element-wise operations on arrays without loops.
  • Common Operations: +, -, *, /, **, //, %, <, <=, >, >=, ==, !=, @ (matrix multiplication).
  • Examples:
    1. arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = arr1 + arr2 # Element-wise addition. print(result) # Output: [5, 7, 9]
    2. arr_1_2d = np.array([[1, 2], [3, 4]]) arr_2_2d = np.array([[5, 6], [7, 8]]) result = arr_1_2d * arr_2_2d # Element-wise multiplication print(result) # Output: [[5, 12], [21, 32]]
    3. mat_result = arr_1_2d @ arr_2_2d # Matrix multiplication print(mat_result) # Output: [[19, 22], [43, 50]]

Broadcasting in NumPy

  • Allows operations on arrays of different shapes and sizes.
  • Examples:
    1. arr1 = np.array([[1, 2], [3, 4]]) result = arr1 + 10 # Broadcasts scalar 10 to all elements print(result) # Output: [[11, 12], [13, 14]]
    2. arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([10, 20]) result = arr1 + arr2 # Broadcasts arr2 across rows print(result) # Output: [[11, 22], [13, 24]]
    3. arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[10], [20]]) result = arr1 + arr2 # Broadcasts arr2 across columns print(result) # Output: [[11, 12], [23, 24]]

Mathematical Functions using NumPy

  • NumPy provides a wide range of mathematical functions that can be applied to arrays.
  • These functions operate element-wise and are optimized for performance.
  • For a1 = np.array([2, 5.2, 8.7]), numerical functions can be applied as follows:
Examples of Common Mathematical Functions in NumPy:
FunctionDescriptionExample
np.sqrtSquare root of each element in the array.np.sqrt(a1) => [1.41, 2.28, 2.95]
np.powerFirst array elements raised to powers from second array.np.power(a1, 2) => [4, 27.04, 75.69]
np.sinTrigonometric sine of each element in the array.np.sin(a1) => [0.91, -0.88, 0.67]
np.arcsinInverse sine of each element in the array.np.arcsin(a1) => [nan, nan, nan]
np.floorFloor of each element in the array.np.floor(a1) => [2.0, 5.0, 8.0]
np.ceilCeiling of each element in the array.np.ceil(a1) => [2.0, 6.0, 9.0]
np.roundRound each element in the array to the nearest integer.np.round(a1) => [2.0, 5.0, 9.0]
np.absAbsolute value of each element in the array.np.abs(a1) => [2.0, 5.2, 8.7]
np.degreesConvert angles from radians to degrees.np.degrees(a1) => [114.59, 298.20, 498.24]
np.radiansConvert angles from degrees to radians.np.radians(a1) => [0.03, 0.09, 0.15]
Examples for applying mathematical functions in NumPy

Apply string functions using NumPy

  • NumPy provides a set of vectorized string methods that can be applied to arrays of strings.
  • Accessed through the np.char module and operate element-wise on string arrays.
  • For s1 = np.array(["hello ", " world", "data science"]), string functions can be applied as:
Examples of Common String Functions in NumPy:
FunctionDescriptionExample
np.char.upperConvert all strings to uppercase.np.char.upper(s1) => ["HELLO ", " WORLD", "DATA SCIENCE"]
np.char.lowerConvert all strings to lowercase.np.char.lower(s1) => ["hello ", " world", "data science"]
np.char.titleConvert all strings to title case.np.char.title(s1) => ["Hello ", " World", "Data Science"]
np.char.stripRemove whitespace from all strings.np.char.strip(s1) => ["hello", "world", "data science"]
np.char.startswithCheck if each string starts with given prefix.np.char.startswith(s1, "h") => [True, False, False]
np.char.endswithCheck if each string ends with given suffix.np.char.endswith(s1, "e") => [False, False, True]
np.char.replaceReplace substring with another.np.char.replace(s1, " ", "_") => ["hello_", "_world", "data_science"]
np.char.splitSplit string by the specified separator.np.char.split(s1) => [["hello"], ["world"], ["data", "science"]]
np.char.joinJoin elements of an array with a specified separator.np.char.join("-", s1) => ["h-e-l-l-o- ", " -w-o-r-l-d", "d-a-t-a- -s-c-i-e-n-c-e"]
np.char.findFind the lowest index of the substring.np.char.find(s1, "o") => [4, 2, -1]
np.char.countCount occurrences of a substring.np.char.count(s1, "o") => [1, 1, 0]
np.char.str_lenReturn the length of each string.np.char.str_len(s1) => [6, 6, 12]
np.vectorize(func)Apply generic function to string.np.vectorize(func)(s1) => Applies func to each element in s1
Examples for applying string functions in NumPy

Aggregate Functions in NumPy

  • NumPy provides aggregate functions that operate on arrays to compute summary statistics.
  • These functions can be applied to the entire array or along specific axes.
  • For a2 = np.array([[1, 2], [3, 4]]), aggregate functions can be applied as:
Examples of Common Aggregate Functions in NumPy:
FunctionDescriptionExample
np.sumSum of array elements.np.sum(a2) => 10, np.sum(a2, axis=0) => [4, 6]
np.meanMean of array elements.np.mean(a2) => 2.5, np.mean(a2, axis=1) => [1.5, 3.5]
np.medianMedian of array elements.np.median(a2) => 2.5
np.stdStandard deviation of array elements.np.std(a2) => 1.118
np.varVariance of array elements.np.var(a2) => 1.25
np.minMinimum value in the array.np.min(a2) => 1
np.maxMaximum value in the array.np.max(a2) => 4
np.argminIndex of minimum value in the array.np.argmin(a2) => 0
np.argmaxIndex of maximum value in the array.np.argmax(a2) => 3
np.clipClip (limit) the values in the array.np.clip(a2, 2, 3) => [[2, 2], [3, 3]]
np.cumsumCumulative sum of array elements.np.cumsum(a2) => [1, 3, 6, 10]
np.cumprodCumulative product of array elements.np.cumprod(a2) => [1, 2, 6, 24]
np.covCovariance of two arrays.np.cov(a2, a2)
np.ptpPeak to peak (max - min) of array elements.np.ptp(a2) => 3
np.quantileQuantiles of the array.np.quantile(a2, 0.5) => 2.5
np.averageWeighted average of array elements.np.average(a2, weights=[0.25, 0.75]) => 3.25
Examples for applying aggregate functions in NumPy

NumPy Constants

  • NumPy provides several constants that are useful in mathematical computations.
Common NumPy Constants:
ConstantDescriptionValue
np.piThe mathematical constant π (pi).3.141592653589793
np.infPositive infinity.inf
-np.infNegative infinity.-inf
np.nanNot a Number (NaN) value.nan
Examples of commonly used constants in NumPy

Practice QuestionsNot started

  1. Indexing and Slicing

    Question 1 of 4

      Store sensor data of 24 hr temperature in a List as tmp_data = [1.3, 1.4, .., 0.8].
      1. Create a NumPy array from this list. Display array, it's shape, items data type.
      2. Display the temperature of 12 PM.
      3. Replace the temperature of 2 PM with value -1.
      4. Display temperature from 10 AM to 6 PM.
      5. Delete the temperature of 2 PM.
      6. Insert the value for 2 PM with median temperature of the day.
      7. Display minimum temperature of day and time it occurred.
      8. Display maximum temperature of day and time it occurred.
      9. Calculate the standard deviation of the temperatures.
  2. Modifying Arrays

    Question 2 of 4

      Assign info as [["Rabindra", "Instructor], ["Ram", "Student], ["Shyam", "Student"]].
      1. Create a NumPy array from this list. Display array, it's shape, items data type.
      2. Add a new row at end for Sita with role Student.
      3. Add a new column at end for Age with values 30, 25, 28, 22.
      4. Delete the row for Ram.
      5. Delete the column for Age.
      6. Add a 2 column as City with values Kathmandu, Mumbai, Newyork.
      7. Display the name of all students.
      8. Display the role of Rabindra.
      9. Display all info for Shyam.
  3. Vectorized Operation, Broadcast

    Question 3 of 4

      Store cost price of products as cost_prices = [100, 150, 200, 250, 300].
      1. Create a NumPy array from it as cp_ar. Display array, it's shape, items data type.
      2. Assuming a 13% profit, calculate the selling price for each product. Assign it as sp_ar.
      3. Calculate the profit for each product using sp_ar and cp_ar. Assign it as profit_ar.
      4. Create function price_range that accepts price & categorizes it as low, medium or high depending if its value is below 150, between 150-250 or above 250.
      5. Find price category for each product using price_range as store it as price_cat_ar.
      6. Convert values of price_cat_ar into uppercase, lowercase, titlecase & Display result.
      7. Create rev_ar by subtracting 2 from profit_ar.
      8. Round each element of sp_ar upto 2 decimal and store it as sp_ar_rounded.
  4. Vectorized Operation, Broadcast

    Question 4 of 4

    • Store stock price as [100, 150, 200, 250, 300, 270]. Using it calculate return for each day & store as returns_ar. (return = (current_price - previous_price)/previous_price)
    • A man deposits each day on bank as [10, 15, 20, 25, 30, 27]. Generate a array total_amount_ar that shows total amount deposited over the time. [10, 25, 45, ...].
    • You have a co2 emission data for 5 years as [100, 150, 200, 250, 300]. Normalize this reading so that value will transformed from 0-1. (x_norm = (x - min) / (max - min))
    • Write a function incrment_salary that takes current salary and returns increased salary. Hike Rule: <1000 => 20%, 1000-5000 => 10%, >5000 => 5%.
    • Apply incrment_salary to array [800, 1200, 3000, 6000]. Store result as new_salary_ar.