|
| 1 | +# %% [markdown] |
| 2 | +# ##NUMPY |
| 3 | +# |
| 4 | +# numpy aims to provide an array object that is upto 50 x faster than traditional lists. |
| 5 | +# Numpy arrays are stored at one continous place in memory unlike lists so processes can access and manipulate them very efficiently. |
| 6 | + |
| 7 | +# %% |
| 8 | +import numpy as np; |
| 9 | +a = np.array([1,2,22,3]) |
| 10 | + |
| 11 | +# %% |
| 12 | +for i in range (len(a)): |
| 13 | + print(i*7) |
| 14 | + |
| 15 | +# %% |
| 16 | +import numpy as np; |
| 17 | +# Create a 2D array |
| 18 | +matrix = np.array([[1, 2], [3, 4]]) |
| 19 | +print("2D Array:\n", matrix) |
| 20 | + |
| 21 | +# Accessing elements |
| 22 | +print("Element at row 0, column 1:", matrix[0, 1]) # Output: 2 |
| 23 | +a = np.array([10, 20, 30]) |
| 24 | +b = np.array([1, 2, 3]) |
| 25 | + |
| 26 | +print("Addition:", a + b) # [11 22 33] |
| 27 | +print("Multiplication:", a * b) # [10 40 90] |
| 28 | +print("Mean of a:", np.mean(a)) # 20.0 |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +# %% |
| 33 | +import numpy as np; |
| 34 | +# Create a 3x3 matrix with random values |
| 35 | +rand_matrix = np.random.rand(3, 3) |
| 36 | +print("Random Matrix:\n", rand_matrix) |
| 37 | + |
| 38 | +# Reshape a flat array to 2D |
| 39 | +flat = np.arange(9) |
| 40 | +reshaped = flat.reshape(3, 3) |
| 41 | +print("Reshaped Array:\n", reshaped) |
| 42 | + |
| 43 | + |
| 44 | +# %% |
| 45 | +import numpy as np; |
| 46 | +arr = np.array([5, 10, 15, 20, 25]) |
| 47 | +filtered = arr[arr > 15] |
| 48 | +print("Filtered (greater than 15):", filtered) # [20 25] |
| 49 | + |
| 50 | + |
| 51 | +# %% |
| 52 | +import numpy as ss; |
| 53 | +arr1 = ss.array([2,3,4,5,5]); |
| 54 | +for i in range (len(arr1)): |
| 55 | + if i==1 : |
| 56 | + print(arr1[i]) |
| 57 | + |
| 58 | + |
| 59 | +# %% |
| 60 | +import numpy as np |
| 61 | + |
| 62 | +# Step 1: Define two arrays |
| 63 | +array1 = [1, 3, 5, 7, 9] |
| 64 | +array2 = [2, 3, 5, 8, 10] |
| 65 | + |
| 66 | +# Step 2: Get the union (remove duplicates) |
| 67 | +union_array = list(set(array1 + array2)) |
| 68 | +union_array.sort() |
| 69 | + |
| 70 | +# Step 3: Calculate mean and median |
| 71 | +mean_val = np.mean(union_array) |
| 72 | +median_val = np.median(union_array) |
| 73 | + |
| 74 | +# Step 4: Print results |
| 75 | +print("Array 1:", array1) |
| 76 | +print("Array 2:", array2) |
| 77 | +print("Union:", union_array) |
| 78 | +print("Mean:", mean_val) |
| 79 | +print("Median:", median_val) |
| 80 | + |
| 81 | + |
| 82 | +# %% |
| 83 | +from math import isqrt |
| 84 | +import numpy as np; |
| 85 | + |
| 86 | +# Arrays |
| 87 | +array1 = [2, 3, 4] |
| 88 | +array2 = [1, 2, 3] |
| 89 | + |
| 90 | +# Union and sort |
| 91 | +union_array = sorted(set(array1 + array2)) |
| 92 | + |
| 93 | +print("Star Patterns:\n") |
| 94 | + |
| 95 | +# Function to print a star block of given rows and cols |
| 96 | +def print_block(rows, cols): |
| 97 | + for _ in range(rows): |
| 98 | + print("*" * cols) |
| 99 | + print() |
| 100 | + |
| 101 | +# Generate all rectangle patterns for each number |
| 102 | +for num in union_array: |
| 103 | + print(f"Patterns for {num} stars:") |
| 104 | + for rows in range(1, num + 1): |
| 105 | + if num % rows == 0: |
| 106 | + cols = num // rows |
| 107 | + print_block(rows, cols) |
| 108 | + |
| 109 | + |
| 110 | +# %% |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments