Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Science Essentials for Data Analysts/2.2 Numpy Lab .ipynb
3079 views
Kernel: Python 3 (ipykernel)
  • Create a NumPy array with elements ranging from 0 to 9. Then, reshape it into a 3x3 matrix.

  • Create a 2D array of Shape 2*4 with defined elements in row1 - M1

  • Create a 2D identify matrix of shape 3*3- M2

  • Create a 2D ones matrix with Shape 2*4-M3

Compute: 1. M1+M3 2. M3M1 3. 4M2

  • Given two NumPy arrays A and B, perform element-wise addition, subtraction, multiplication, and division.

  • Extract the middle row of a given 2D NumPy array. Then, extract the elements at odd indices from the last column.

  • Compute the mean, median, and standard deviation of a given NumPy array across rows and columns.

  • Concatenate two 1D NumPy arrays vertically and horizontally, respectively.

  • Given a 1D NumPy array of ages, find all ages greater than 30.

  • Given a 2D NumPy array representing student scores and a 1D array representing score weights, compute weighted averages for each student.

  • Generate a random 3x3 NumPy array of integers between 0 and 100.

import numpy as np arr = np.arange(9).reshape(3, 3) print(arr)
[[0 1 2] [3 4 5] [6 7 8]]
A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) addition = A + B subtraction = A - B multiplication = A * B division = A / B print("Addition:", addition) print("Subtraction:", subtraction) print("Multiplication:", multiplication) print("Division:", division)
Addition: [5 7 9] Subtraction: [-3 -3 -3] Multiplication: [ 4 10 18] Division: [0.25 0.4 0.5 ]
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) middle_row = arr[1] odd_indices_last_column = arr[::2, -1] print("Middle row:", middle_row) print("Odd indices of last column:", odd_indices_last_column)
Middle row: [4 5 6] Odd indices of last column: [3 9]
arr = np.array([1, 2, 3, 4, 5]) mean = np.mean(arr) median = np.median(arr) std_dev = np.std(arr) print("Mean:", mean) print("Median:", median) print("Standard Deviation:", std_dev)
Mean: 3.0 Median: 3.0 Standard Deviation: 1.4142135623730951
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) vertical_concat = np.vstack((arr1, arr2)) horizontal_concat = np.hstack((arr1, arr2)) print("Vertical Concatenation:") print(vertical_concat) print("\nHorizontal Concatenation:", horizontal_concat)
Vertical Concatenation: [[1 2 3] [4 5 6]] Horizontal Concatenation: [1 2 3 4 5 6]
ages = np.array([25, 35, 40, 20, 45]) over_30 = ages[ages > 30] print("Ages over 30:", over_30)
Ages over 30: [35 40 45]
scores = np.array([[80, 90, 70], [85, 88, 92]]) weights = np.array([0.3, 0.4, 0.3]) weighted_avg = np.dot(scores, weights) print("Weighted Averages for each student:", weighted_avg)
Weighted Averages for each student: [81. 88.3]
random_array = np.random.randint(0, 100, size=(3, 3)) print("Random Array:") print(random_array)
Random Array: [[95 10 19] [35 91 48] [62 42 5]]