├── numpy-Documentation.pdf ├── README.md └── Numpy └── Numpy_Functions.ipynb /numpy-Documentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dsmentors/Numpy_Study_Materials/HEAD/numpy-Documentation.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Numpy Interview Questions** 2 | 3 | 1. What is Numpy? 4 | 2. Why NumPy is used in Python? 5 | 3. What does NumPy mean in Python? 6 | 4. Where is NumPy used? 7 | 5. How to Install Numpy in Windows? 8 | 6. How to import numpy in python? 9 | 7. how to create 1D Array ? 10 | 8. How to create 2D Array ? 11 | 9. How to create 3D Array or ND Array ? 12 | 10.How to use shape for 1D Array ? 13 | 11.How to use shape for 2D Array ? 14 | 12. How to use shape for 3d or Nd Array ? 15 | 13. How to identified datatype for numpy array? 16 | 14. Print 5 zeros with numpy ? 17 | 15. print zeros with 2 rows and 3 columns ? 18 | 16. use of eye() diagonal values ? 19 | 17. use of diag() square matrix ? 20 | 18. Print Range Between 1 To 15 and show 4 integers random numbers 21 | 19. Print Range Between 1 To 100 and show 4 integers random numbers 22 | 20. Print Range Between random number 2 row and 3 cols integers random numbers 23 | 21. What Is The Difference Between Numpy And Scipy? 24 | 22. What Is The Preferred Way To Check For An Empty (zero Element) Array? 25 | 23. List the advantages NumPy Arrays have over (nested) Python lists? 26 | 24. How do you create a 3D array? 27 | 25. How can you identify the datatype of a given NumPy array? 28 | 26. Why NumPy is faster than list? 29 | 30 | **Questions on NumPy Array** 31 | 32 | 1. How to create an empty and a full NumPy array? 33 | 2. Create a Numpy array filled with all zeros 34 | 3. Create a Numpy array filled with all ones 35 | 4. Check whether a Numpy array contains a specified row 36 | 5. How to Remove rows in Numpy array that contains non-numeric values? 37 | 6. Remove single-dimensional entries from the shape of an array 38 | 7. Find the number of occurrences of a sequence in a NumPy array 39 | 8. Find the most frequent value in a NumPy array 40 | 9. Combining a one and a two-dimensional NumPy Array 41 | 10. How to build an array of all combinations of two NumPy arrays? 42 | 11. How to add a border around a NumPy array? 43 | 12. How to compare two NumPy arrays? 44 | 13. How to check whether specified values are present in NumPy array? 45 | 14. How to get all 2D diagonals of a 3D NumPy array? 46 | 15. Flatten a Matrix in Python using NumPy 47 | 16. Flatten a 2d numpy array into 1d array 48 | 17. Move axes of an array to new positions 49 | 18. Interchange two axes of an array 50 | 19. NumPy – Fibonacci Series using Binet Formula 51 | 20. Counts the number of non-zero values in the array 52 | 21. Count the number of elements along a given axis 53 | 22. Trim the leading and/or trailing zeros from a 1-D array 54 | 23. Change data type of given numpy array 55 | 24. Reverse a numpy array 56 | 25. How to make a NumPy array read-only? 57 | 58 | **Questions on NumPy Matrix** 59 | 60 | 1. Get the maximum value from given matrix 61 | 2. Get the minimum value from given matrix 62 | 3. Find the number of rows and columns of a given matrix using NumPy 63 | 4. Select the elements from a given matrix 64 | 5. Find the sum of values in a matrix 65 | 6. Calculate the sum of the diagonal elements of a NumPy array 66 | 7. Adding and Subtracting Matrices in Python 67 | 8. Ways to add row/columns in numpy array 68 | 9. Matrix Multiplication in NumPy 69 | 10. Get the eigen values of a matrix 70 | 11. How to Calculate the determinant of a matrix using NumPy? 71 | 12. How to inverse a matrix using NumPy 72 | 13. How to count the frequency of unique values in NumPy array? 73 | 14. Multiply matrices of complex numbers using NumPy in Python 74 | 15. Compute the outer product of two given vectors using NumPy in Python 75 | 16. Calculate inner, outer, and cross products of matrices and vectors using NumPy 76 | 17. Compute the covariance matrix of two given NumPy arrays 77 | 18. Convert covariance matrix to correlation matrix using Python 78 | 19. Compute the Kronecker product of two mulitdimension NumPy arrays 79 | 20. Convert the matrix into a list 80 | 81 | 82 | **Questions on NumPy Indexing** 83 | 84 | 1. Replace NumPy array elements that doesn’t satisfy the given condition 85 | 2. Return the indices of elements where the given condition is satisfied 86 | 3. Replace NaN values with average of columns 87 | 4. Replace negative value with zero in numpy array 88 | 5. How to get values of an NumPy array at certain index positions? 89 | 6. Find indices of elements equal to zero in a NumPy array 90 | 7. How to Remove columns in Numpy array that contains non-numeric values? 91 | 8. How to access different rows of a multidimensional NumPy array? 92 | 9. Get row numbers of NumPy array having element larger than X 93 | 10. Get filled the diagonals of NumPy array 94 | 11. Check elements present in the NumPy array 95 | 12. Combined array index by index 96 | 97 | **Questions on NumPy Linear Algebra** 98 | 99 | 1. Find a matrix or vector norm using NumPy 100 | 2. Calculate the QR decomposition of a given matrix using NumPy 101 | 3. Compute the condition number of a given matrix using NumPy 102 | 4. Compute the eigenvalues and right eigenvectors of a given square array using NumPy? 103 | 5. Calculate the Euclidean distance using NumPy 104 | 105 | **Questions on NumPy Random** 106 | 107 | 1. Create a Numpy array with random values 108 | 2. How to choose elements from the list with different probability using NumPy? 109 | 3. How to get weighted random choice in Python? 110 | 4. Generate Random Numbers From The Uniform Distribution using NumPy 111 | 5. Get Random Elements form geometric distribution 112 | 6. Get Random elements from Laplace distribution 113 | 7. Return a Matrix of random values from a uniform distribution 114 | 8. Return a Matrix of random values from a Gaussian distribution 115 | 116 | **Questions on NumPy Sorting and Searching** 117 | 118 | 1. How to get the indices of the sorted array using NumPy in Python? 119 | 2. Finding the k smallest values of a NumPy array 120 | 3. How to get the n-largest values of an array using NumPy? 121 | 4. Sort the values in a matrix 122 | 5. Filter out integers from float numpy array 123 | 6. Find the indices into a sorted array 124 | 125 | **Questions on NumPy Mathematics** 126 | 127 | 1. How to get element-wise true division of an array using Numpy? 128 | 2. How to calculate the element-wise absolute value of NumPy array? 129 | 3. Compute the negative of the NumPy array 130 | 4. Multiply 2d numpy array corresponding to 1d array 131 | 5. Computes the inner product of two arrays 132 | 6. Compute the nth percentile of the NumPy array 133 | 7. Calculate the n-th order discrete difference along the given axis 134 | 8. Calculate the sum of all columns in a 2D NumPy array 135 | 9. Calculate average values of two given NumPy arrays 136 | 10. How to compute numerical negative value for all elements in a given NumPy array? 137 | 11. How to get the floor, ceiling and truncated values of the elements of a numpy array? 138 | 12. How to round elements of the NumPy array to the nearest integer? 139 | 13. Find the round off the values of the given matrix 140 | 14. Determine the positive square-root of an array 141 | 142 | **Questions on NumPy Statistics** 143 | 144 | 1. Compute the median of the flattened NumPy array 145 | 2. Find Mean of a List of Numpy Array 146 | 3. Calculate the mean of array ignoring the NaN value 147 | 4. Get the mean value from given matrix 148 | 5. Compute the variance of the NumPy array 149 | 6. Compute the standard deviation of the NumPy array 150 | 7. Compute pearson product-moment correlation coefficients of two given NumPy arrays 151 | 8. Calculate the mean across dimension in a 2D NumPy array 152 | 9. Calculate the average, variance and standard deviation in Python using NumPy 153 | 10. Describe a NumPy Array in Python 154 | 155 | **Questions on NumPy Strings** 156 | 157 | 1. Repeat all the elements of a NumPy array of strings 158 | 2. How to split the element of a given NumPy array with spaces? 159 | 3. How to insert a space between characters of all the elements of a given NumPy array? 160 | 4. Find the length of each string element in the Numpy array 161 | 5. Swap the case of an array of string 162 | 6. Change the case to uppercase of elements of an array 163 | 7. Change the case to lowercase of elements of an array 164 | 8. Join String by a separator 165 | 9. Check if two same shaped string arrayss one by one 166 | 10. Count the number of substrings in an array 167 | 11. Find the lowest index of the substring in an array 168 | 12. Get the boolean array when values end with a particular character 169 | 170 | **More Questions on NumPy** 171 | 172 | 1. Different ways to convert a Python dictionary to a NumPy array 173 | 2. How to convert a list and tuple into NumPy arrays? 174 | 3. Ways to convert array of strings to array of floats 175 | 4. Convert a NumPy array into a csv file 176 | 5. How to Convert an image to NumPy array and save it to CSV file using Python? 177 | 6. How to save a NumPy array to a text file? 178 | 7. Load data from a text file 179 | 8. Plot line graph from NumPy array 180 | 9. Create Histogram using NumPy 181 | -------------------------------------------------------------------------------- /Numpy/Numpy_Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "abA_L2ncIYeB" 7 | }, 8 | "source": [ 9 | "# **NumPy with Python**" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "id": "F0lXOY-WJguQ" 16 | }, 17 | "source": [ 18 | "NumPy (short for Numerical Python) is an open source Python library for doing scientific computing with Python.\n", 19 | "\n", 20 | "It gives an ability to create multidimensional array objects and perform faster mathematical operations. The library contains a long list of useful mathematical functions, including some functions for linear algebra and complex mathematical operations such as Fourier Transform (FT) and random number generator (RNG).\n", 21 | "\n", 22 | "Most of the other libraries that we use in data analytics with Python, such as scikit-learn, SciPy and Pandas use some of NumPy's features." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": { 28 | "id": "oHfj4TumJxl3" 29 | }, 30 | "source": [ 31 | " **How to find the mean values across dimensions in a NumPy array in Python?**" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "id": "lomAxzTFJ5OC" 38 | }, 39 | "source": [ 40 | "### **Calculate mean values across each column**\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": { 47 | "colab": { 48 | "base_uri": "https://localhost:8080/" 49 | }, 50 | "id": "PJxnCGNNKMob", 51 | "outputId": "03bed14b-dbc4-4d7c-9095-b5d3db187226" 52 | }, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "[3. 4. 5. 6.]\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "\n", 64 | "import numpy as np\n", 65 | " \n", 66 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 67 | "mean_array = the_array.mean(axis=0)\n", 68 | "print(mean_array)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "id": "wK6w24fZKfEl" 75 | }, 76 | "source": [ 77 | "### **Calculate mean values of each row**\n" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "colab": { 85 | "base_uri": "https://localhost:8080/" 86 | }, 87 | "id": "hCNUuDUDKqWH", 88 | "outputId": "40993ade-42ec-484a-e94f-56ace106f892" 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "[2.5 6.5]\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "import numpy as np\n", 101 | " \n", 102 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 103 | "mean_array = the_array.mean(axis=1)\n", 104 | "print(mean_array)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "id": "btSeRMLPK3XR" 111 | }, 112 | "source": [ 113 | "### **Mean of First column only**\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": { 120 | "colab": { 121 | "base_uri": "https://localhost:8080/" 122 | }, 123 | "id": "DCNpX1LGK8H2", 124 | "outputId": "269c3d75-a01c-4f8e-8e76-156c316056d2" 125 | }, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "3.0\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "import numpy as np\n", 137 | " \n", 138 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 139 | "mean_array = the_array[:, 0].mean()\n", 140 | "print(mean_array)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "id": "EUxb_SnyLMdZ" 147 | }, 148 | "source": [ 149 | "### **Mean of Second column only**\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": { 156 | "colab": { 157 | "base_uri": "https://localhost:8080/" 158 | }, 159 | "id": "RcWGdGKCLnts", 160 | "outputId": "82b2a435-032d-4412-c207-04aad1f674e4" 161 | }, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "3.0\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "import numpy as np\n", 173 | "\n", 174 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 175 | "mean_array = the_array[:, 0].mean()\n", 176 | "print(mean_array)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "id": "UfiBoUIHMdo1" 183 | }, 184 | "source": [ 185 | "### **How to create NumPy array?**" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": { 191 | "id": "L9KOO86TMlEQ" 192 | }, 193 | "source": [ 194 | "To create a NumPy array we need to pass list of element values inside a square bracket as an argument to the np.array() function.\n", 195 | "A 3d array is a matrix of 2d array. A 3d array can also be called as a list of lists where every element is again a list of elements.\n", 196 | "\n" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "colab": { 204 | "base_uri": "https://localhost:8080/" 205 | }, 206 | "id": "4qoadax-MhKg", 207 | "outputId": "5e4f9431-51f4-4d88-9e2f-2491cfaed2da" 208 | }, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "[1 2 3 4 5 6]\n", 215 | "----------\n", 216 | "[[1 2 3]\n", 217 | " [4 5 6]]\n", 218 | "----------\n", 219 | "[[[ 1 2 3]\n", 220 | " [ 4 5 6]]\n", 221 | "\n", 222 | " [[ 7 8 9]\n", 223 | " [10 11 12]]]\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "import numpy as np\n", 229 | " \n", 230 | "array1d = np.array([1, 2, 3, 4, 5, 6])\n", 231 | "array2d = np.array([[1, 2, 3], [4, 5, 6]])\n", 232 | "array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", 233 | " \n", 234 | "print(array1d)\n", 235 | " \n", 236 | "print(\"-\" * 10)\n", 237 | "print(array2d)\n", 238 | " \n", 239 | "print(\"-\" * 10)\n", 240 | "print(array3d)\n" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": { 246 | "id": "Ujky0KMVM0bP" 247 | }, 248 | "source": [ 249 | "The main data structure for multidimensional arrays in NumPy is the ndarray class. Basic Attributes of the ndarray Class as follow:\n", 250 | "\n" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": { 256 | "id": "oUG-NWeENBTq" 257 | }, 258 | "source": [ 259 | "**Attribute\tDescription**\n", 260 | "Shape -\tA tuple that specifies the number of elements for each dimension of the array.\n", 261 | "Size - The total number elements in the array.\n", 262 | "Ndim -\tDetermines the dimension an array.\n", 263 | "nbytes -\tNumber of bytes used to store the data.\n", 264 | "dtype -\tDetermines the datatype of elements stored in array." 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": { 270 | "id": "nQA68W0cNXbw" 271 | }, 272 | "source": [ 273 | "**How to create NumPy array using arange function?**\n" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": { 279 | "id": "OVCy7IrEPv4B" 280 | }, 281 | "source": [ 282 | "The arange() function creates an array with evenly spaced values between the specified start, end, and increment values.\n", 283 | "General form: np.arange(Start, End, Increment)\n" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": { 289 | "id": "wjdV_rTWP2aR" 290 | }, 291 | "source": [ 292 | "The reshape function is used to change its dimension:" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": { 299 | "colab": { 300 | "base_uri": "https://localhost:8080/" 301 | }, 302 | "id": "ufbiwNxpOqlp", 303 | "outputId": "e4f56d88-62e4-4b05-b3c3-fdc0efd425fc" 304 | }, 305 | "outputs": [ 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "[0 1 2 3 4]\n", 311 | "[ 0 2 4 6 8 10]\n", 312 | "[[ 0 2 4]\n", 313 | " [ 6 8 10]]\n", 314 | "[[0 1 2]\n", 315 | " [3 4 5]\n", 316 | " [6 7 8]]\n" 317 | ] 318 | } 319 | ], 320 | "source": [ 321 | "import numpy as np\n", 322 | " \n", 323 | "array1d = np.arange(5) # 1 row and 5 columns\n", 324 | "print(array1d)\n", 325 | " \n", 326 | "array1d = np.arange(0, 12, 2) # 1 row and 6 columns\n", 327 | "print(array1d)\n", 328 | " \n", 329 | "array2d = np.arange(0, 12, 2).reshape(2, 3) # 2 rows 3 columns\n", 330 | "print(array2d)\n", 331 | " \n", 332 | "array3d = np.arange(9).reshape(3, 3) # 3 rows and columns\n", 333 | "print(array3d)\n" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": { 339 | "id": "YQakkXW7Rcmw" 340 | }, 341 | "source": [ 342 | "### **How to convert List or Tuple into NumPy array?**\n" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": { 348 | "id": "vMnnkvtwRkhj" 349 | }, 350 | "source": [ 351 | "The array() function can accept lists, tuples and other numpy.ndarray objects also to create new array object.\n" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": { 358 | "colab": { 359 | "base_uri": "https://localhost:8080/" 360 | }, 361 | "id": "n2uqQEUWRoH1", 362 | "outputId": "8bc70c82-558a-41a0-8ae7-3248c53b6043" 363 | }, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "\n", 370 | "\n", 371 | "\n", 372 | "\n", 373 | "[[1 2 3]\n", 374 | " [1 2 3]\n", 375 | " [1 2 3]]\n" 376 | ] 377 | } 378 | ], 379 | "source": [ 380 | "import numpy as np\n", 381 | " \n", 382 | "thelist = [1, 2, 3]\n", 383 | "print(type(thelist)) # \n", 384 | " \n", 385 | "array1 = np.array(thelist)\n", 386 | "print(type(array1)) # \n", 387 | " \n", 388 | "thetuple = ((1, 2, 3))\n", 389 | "print(type(thetuple)) # \n", 390 | " \n", 391 | "array2 = np.array(thetuple)\n", 392 | "print(type(array2)) # \n", 393 | " \n", 394 | "array3 = np.array([thetuple, thelist, array1])\n", 395 | "print(array3)\n" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": { 401 | "id": "nX2JuMwySziV" 402 | }, 403 | "source": [ 404 | "### **Detect if a NumPy array contains at least one non-numeric value?**\n" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "metadata": { 410 | "id": "nv-ioQ531m2y" 411 | }, 412 | "source": [ 413 | "Check NaN in array" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": null, 419 | "metadata": { 420 | "colab": { 421 | "base_uri": "https://localhost:8080/" 422 | }, 423 | "id": "1hgYoIH31zH5", 424 | "outputId": "45883cba-641c-4b7a-c216-384d8fefa6a9" 425 | }, 426 | "outputs": [ 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "True\n", 432 | "False\n" 433 | ] 434 | } 435 | ], 436 | "source": [ 437 | "import numpy as np\n", 438 | "the_array = np.array([np.nan, 2, 3, 4])\n", 439 | "array_has_nan = np.isnan(the_array).any()\n", 440 | "print(array_has_nan)\n", 441 | "the_array = np.array([1, 2, 3, 4])\n", 442 | "array_has_nan = np.isnan(the_array).any()\n", 443 | "print(array_has_nan)" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": { 449 | "id": "Fp4bl6xn2Eu8" 450 | }, 451 | "source": [ 452 | "**How to get shape of NumPy array?**" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": { 458 | "id": "JrD0JrDd2Mjq" 459 | }, 460 | "source": [ 461 | "The shape method determines the shape of NumPy array in form of (m, n) i.e (no. of rows) x (no. of columns)." 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": null, 467 | "metadata": { 468 | "colab": { 469 | "base_uri": "https://localhost:8080/" 470 | }, 471 | "id": "utG6UkTk2SQx", 472 | "outputId": "d210802c-7a54-4fcb-cde0-ce01ff15a4fa" 473 | }, 474 | "outputs": [ 475 | { 476 | "name": "stdout", 477 | "output_type": "stream", 478 | "text": [ 479 | "(6,)\n", 480 | "(2, 3)\n", 481 | "(2, 2, 3)\n" 482 | ] 483 | } 484 | ], 485 | "source": [ 486 | "import numpy as np\n", 487 | " \n", 488 | "array1d = np.array([1, 2, 3, 4, 5, 6])\n", 489 | "array2d = np.array([[1, 2, 3], [4, 5, 6]])\n", 490 | "array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", 491 | " \n", 492 | "print(array1d.shape)\n", 493 | "print(array2d.shape)\n", 494 | "print(array3d.shape)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": { 500 | "id": "DOmHpPfn2o7G" 501 | }, 502 | "source": [ 503 | "### **How to get and set data type of NumPy array?**" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": { 509 | "id": "fcJ7YHqX25a_" 510 | }, 511 | "source": [ 512 | "The dtype method determines the datatype of elements stored in NumPy array. You can also explicitly define the data type using the dtype option as an argument of array function." 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": null, 518 | "metadata": { 519 | "colab": { 520 | "base_uri": "https://localhost:8080/" 521 | }, 522 | "id": "fYgLkN9221Hj", 523 | "outputId": "7bb1c8e8-354d-473b-e0b1-c5073406f1b2" 524 | }, 525 | "outputs": [ 526 | { 527 | "name": "stdout", 528 | "output_type": "stream", 529 | "text": [ 530 | "int64\n", 531 | "float64\n", 532 | " 15))\n", 2139 | "print(\"-\" * 30)\n", 2140 | " \n", 2141 | "print(np.logical_and(thearray < 10, thearray > 15))\n", 2142 | "print(\"-\" * 30)\n", 2143 | " \n", 2144 | "print(np.logical_not(thearray < 20))\n", 2145 | "print(\"-\" * 30)\n" 2146 | ] 2147 | }, 2148 | { 2149 | "cell_type": "markdown", 2150 | "metadata": { 2151 | "id": "I9AzMHCgDmgL" 2152 | }, 2153 | "source": [ 2154 | "### **NumPy example of Standard Set Operations**" 2155 | ] 2156 | }, 2157 | { 2158 | "cell_type": "markdown", 2159 | "metadata": { 2160 | "id": "-fXiYxkKDpgs" 2161 | }, 2162 | "source": [ 2163 | "The standard set operations union (array of values that are in either of the two input arrays), intersection (unique values that are in both of the input arrays), and difference (unique values in array1 that are not in array2) are provided by np.union1d(), np.intersect1d(), and np.setdiff1d(), respectively." 2164 | ] 2165 | }, 2166 | { 2167 | "cell_type": "code", 2168 | "execution_count": null, 2169 | "metadata": { 2170 | "colab": { 2171 | "base_uri": "https://localhost:8080/" 2172 | }, 2173 | "id": "G5yomzI5DtKC", 2174 | "outputId": "5f10176b-7bd4-4adb-f811-3b5a15a7cb0e" 2175 | }, 2176 | "outputs": [ 2177 | { 2178 | "name": "stdout", 2179 | "output_type": "stream", 2180 | "text": [ 2181 | "[10 14 20 24 30 34 36 40 46 50]\n", 2182 | "[20 24]\n", 2183 | "[10 14 30 36]\n" 2184 | ] 2185 | } 2186 | ], 2187 | "source": [ 2188 | "import numpy as np\n", 2189 | " \n", 2190 | "array1 = np.array([[10, 20, 30], [14, 24, 36]])\n", 2191 | "array2 = np.array([[20, 40, 50], [24, 34, 46]])\n", 2192 | " \n", 2193 | "# Find the union of two arrays.\n", 2194 | "print(np.union1d(array1, array2))\n", 2195 | " \n", 2196 | "# Find the intersection of two arrays.\n", 2197 | "print(np.intersect1d(array1, array2))\n", 2198 | " \n", 2199 | "# Find the set difference of two arrays.\n", 2200 | "print(np.setdiff1d(array1, array2))" 2201 | ] 2202 | }, 2203 | { 2204 | "cell_type": "markdown", 2205 | "metadata": { 2206 | "id": "x2UfMC-4E2UD" 2207 | }, 2208 | "source": [ 2209 | "### **How to find the mean values across dimensions in a NumPy array in Python?**\n" 2210 | ] 2211 | }, 2212 | { 2213 | "cell_type": "markdown", 2214 | "metadata": { 2215 | "id": "bv_ULsRmE7VH" 2216 | }, 2217 | "source": [ 2218 | "Calculate mean values across each column" 2219 | ] 2220 | }, 2221 | { 2222 | "cell_type": "code", 2223 | "execution_count": null, 2224 | "metadata": { 2225 | "colab": { 2226 | "base_uri": "https://localhost:8080/" 2227 | }, 2228 | "id": "Vnpxzb_FE974", 2229 | "outputId": "dd0cae5f-96e8-4847-ab44-0bf79911bce9" 2230 | }, 2231 | "outputs": [ 2232 | { 2233 | "name": "stdout", 2234 | "output_type": "stream", 2235 | "text": [ 2236 | "[3. 4. 5. 6.]\n" 2237 | ] 2238 | } 2239 | ], 2240 | "source": [ 2241 | "import numpy as np\n", 2242 | " \n", 2243 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2244 | "mean_array = the_array.mean(axis=0)\n", 2245 | "print(mean_array)" 2246 | ] 2247 | }, 2248 | { 2249 | "cell_type": "markdown", 2250 | "metadata": { 2251 | "id": "_CcMulZ4FB4Q" 2252 | }, 2253 | "source": [ 2254 | "Calculate mean values of each row" 2255 | ] 2256 | }, 2257 | { 2258 | "cell_type": "code", 2259 | "execution_count": null, 2260 | "metadata": { 2261 | "colab": { 2262 | "base_uri": "https://localhost:8080/" 2263 | }, 2264 | "id": "9xYwxHsoFG5U", 2265 | "outputId": "51b96cb4-722e-4c9c-9933-e8b4df7538b4" 2266 | }, 2267 | "outputs": [ 2268 | { 2269 | "name": "stdout", 2270 | "output_type": "stream", 2271 | "text": [ 2272 | "[2.5 6.5]\n" 2273 | ] 2274 | } 2275 | ], 2276 | "source": [ 2277 | "import numpy as np\n", 2278 | " \n", 2279 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2280 | "mean_array = the_array.mean(axis=1)\n", 2281 | "print(mean_array)" 2282 | ] 2283 | }, 2284 | { 2285 | "cell_type": "markdown", 2286 | "metadata": { 2287 | "id": "OdXbn5QZFLKv" 2288 | }, 2289 | "source": [ 2290 | "Mean of First column only" 2291 | ] 2292 | }, 2293 | { 2294 | "cell_type": "code", 2295 | "execution_count": null, 2296 | "metadata": { 2297 | "colab": { 2298 | "base_uri": "https://localhost:8080/" 2299 | }, 2300 | "id": "0n_j26pgFNup", 2301 | "outputId": "18faf852-f663-40cb-f629-386cf13625d8" 2302 | }, 2303 | "outputs": [ 2304 | { 2305 | "name": "stdout", 2306 | "output_type": "stream", 2307 | "text": [ 2308 | "3.0\n" 2309 | ] 2310 | } 2311 | ], 2312 | "source": [ 2313 | "import numpy as np\n", 2314 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2315 | "mean_array = the_array[:, 0].mean()\n", 2316 | "print(mean_array)" 2317 | ] 2318 | }, 2319 | { 2320 | "cell_type": "markdown", 2321 | "metadata": { 2322 | "id": "c6aRy2iKFVK6" 2323 | }, 2324 | "source": [ 2325 | "Mean of Second column only\n" 2326 | ] 2327 | }, 2328 | { 2329 | "cell_type": "code", 2330 | "execution_count": null, 2331 | "metadata": { 2332 | "colab": { 2333 | "base_uri": "https://localhost:8080/" 2334 | }, 2335 | "id": "09bW2wsxFXH5", 2336 | "outputId": "7a463c9f-7667-40ef-d1a6-89c1901ea8c1" 2337 | }, 2338 | "outputs": [ 2339 | { 2340 | "name": "stdout", 2341 | "output_type": "stream", 2342 | "text": [ 2343 | "3.0\n" 2344 | ] 2345 | } 2346 | ], 2347 | "source": [ 2348 | "import numpy as np\n", 2349 | "the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2350 | "mean_array = the_array[:, 0].mean()\n", 2351 | "print(mean_array)" 2352 | ] 2353 | }, 2354 | { 2355 | "cell_type": "markdown", 2356 | "metadata": { 2357 | "id": "kq66uk_VFr6T" 2358 | }, 2359 | "source": [ 2360 | "### **How to use numpy.any() in Python?**" 2361 | ] 2362 | }, 2363 | { 2364 | "cell_type": "markdown", 2365 | "metadata": { 2366 | "id": "oytz3zCPGFUB" 2367 | }, 2368 | "source": [ 2369 | "Use np.any() to test if any list element evaluates to True\n" 2370 | ] 2371 | }, 2372 | { 2373 | "cell_type": "code", 2374 | "execution_count": null, 2375 | "metadata": { 2376 | "colab": { 2377 | "base_uri": "https://localhost:8080/" 2378 | }, 2379 | "id": "2PBqJNyPF4Zz", 2380 | "outputId": "f66c7e5e-e4a7-457c-ae9c-97283c6bb00d" 2381 | }, 2382 | "outputs": [ 2383 | { 2384 | "name": "stdout", 2385 | "output_type": "stream", 2386 | "text": [ 2387 | "True\n", 2388 | "False\n" 2389 | ] 2390 | } 2391 | ], 2392 | "source": [ 2393 | "import numpy as np\n", 2394 | "thearr = [[True, False], [True, True]]\n", 2395 | "thebool = np.any(thearr)\n", 2396 | "print(thebool)\n", 2397 | "thearr = [[False, False], [False, False]]\n", 2398 | "thebool = np.any(thearr)\n", 2399 | "print(thebool)" 2400 | ] 2401 | }, 2402 | { 2403 | "cell_type": "markdown", 2404 | "metadata": { 2405 | "id": "ESW5g5CrGY6f" 2406 | }, 2407 | "source": [ 2408 | "### **How to use numpy.all() in Python?**\n" 2409 | ] 2410 | }, 2411 | { 2412 | "cell_type": "markdown", 2413 | "metadata": { 2414 | "id": "UOgOto8-GduQ" 2415 | }, 2416 | "source": [ 2417 | "Use np.all() to test if all list element evaluates to True or False\n" 2418 | ] 2419 | }, 2420 | { 2421 | "cell_type": "code", 2422 | "execution_count": null, 2423 | "metadata": { 2424 | "colab": { 2425 | "base_uri": "https://localhost:8080/" 2426 | }, 2427 | "id": "_35daV-ZGf_x", 2428 | "outputId": "212282a6-377e-45de-9d08-b19b94aa38ac" 2429 | }, 2430 | "outputs": [ 2431 | { 2432 | "name": "stdout", 2433 | "output_type": "stream", 2434 | "text": [ 2435 | "True\n", 2436 | "False\n", 2437 | "False\n" 2438 | ] 2439 | } 2440 | ], 2441 | "source": [ 2442 | "import numpy as np\n", 2443 | "thelist = [[True, True], [True, True]]\n", 2444 | "thebool = np.all(thelist)\n", 2445 | "print(thebool)\n", 2446 | "thelist = [[False, False], [False, False]]\n", 2447 | "thebool = np.all(thelist)\n", 2448 | "print(thebool)\n", 2449 | "thelist = [[True, False], [True, False]]\n", 2450 | "thebool = np.all(thelist)\n", 2451 | "print(thebool)" 2452 | ] 2453 | }, 2454 | { 2455 | "cell_type": "markdown", 2456 | "metadata": { 2457 | "id": "MAaFaFW6Gtz_" 2458 | }, 2459 | "source": [ 2460 | "### **How to initialize a NumPy array in Python?**\n" 2461 | ] 2462 | }, 2463 | { 2464 | "cell_type": "markdown", 2465 | "metadata": { 2466 | "id": "3xtken_sG2J1" 2467 | }, 2468 | "source": [ 2469 | "Initialize Numpy Array with specified values\n" 2470 | ] 2471 | }, 2472 | { 2473 | "cell_type": "code", 2474 | "execution_count": null, 2475 | "metadata": { 2476 | "colab": { 2477 | "base_uri": "https://localhost:8080/" 2478 | }, 2479 | "id": "Hrq8eKYzG5CS", 2480 | "outputId": "f27c71c5-89aa-4596-ea41-3f0822b5f35a" 2481 | }, 2482 | "outputs": [ 2483 | { 2484 | "name": "stdout", 2485 | "output_type": "stream", 2486 | "text": [ 2487 | "[[1 2]\n", 2488 | " [3 4]\n", 2489 | " [5 6]]\n" 2490 | ] 2491 | } 2492 | ], 2493 | "source": [ 2494 | "import numpy as np\n", 2495 | "thearray = np.array([[1, 2], [3, 4], [5, 6]])\n", 2496 | "print(thearray)" 2497 | ] 2498 | }, 2499 | { 2500 | "cell_type": "markdown", 2501 | "metadata": { 2502 | "id": "Khro96e-HGmk" 2503 | }, 2504 | "source": [ 2505 | "Initialize an empty Numpy Array\n" 2506 | ] 2507 | }, 2508 | { 2509 | "cell_type": "code", 2510 | "execution_count": null, 2511 | "metadata": { 2512 | "colab": { 2513 | "base_uri": "https://localhost:8080/" 2514 | }, 2515 | "id": "tN6w_HHXHKsN", 2516 | "outputId": "533e4932-8b55-4462-8f45-852ae18c9261" 2517 | }, 2518 | "outputs": [ 2519 | { 2520 | "name": "stdout", 2521 | "output_type": "stream", 2522 | "text": [ 2523 | "[[3. 4.]\n", 2524 | " [5. 6.]]\n" 2525 | ] 2526 | } 2527 | ], 2528 | "source": [ 2529 | "import numpy as np\n", 2530 | "\n", 2531 | "thearray = np.empty((2, 2))\n", 2532 | "print(thearray)" 2533 | ] 2534 | }, 2535 | { 2536 | "cell_type": "markdown", 2537 | "metadata": { 2538 | "id": "RNcBdV4vHVVB" 2539 | }, 2540 | "source": [ 2541 | "Initialize an Numpy Array with Zeros\n" 2542 | ] 2543 | }, 2544 | { 2545 | "cell_type": "code", 2546 | "execution_count": null, 2547 | "metadata": { 2548 | "colab": { 2549 | "base_uri": "https://localhost:8080/" 2550 | }, 2551 | "id": "-DaYfQMrHZK1", 2552 | "outputId": "7a009b17-7a5d-4e07-e5b7-a5b8f9c47ca9" 2553 | }, 2554 | "outputs": [ 2555 | { 2556 | "name": "stdout", 2557 | "output_type": "stream", 2558 | "text": [ 2559 | "[[0. 0.]\n", 2560 | " [0. 0.]]\n" 2561 | ] 2562 | } 2563 | ], 2564 | "source": [ 2565 | "import numpy as np\n", 2566 | "thearray = np.zeros((2, 2))\n", 2567 | "print(thearray)" 2568 | ] 2569 | }, 2570 | { 2571 | "cell_type": "markdown", 2572 | "metadata": { 2573 | "id": "cc-vJ8RiHe7y" 2574 | }, 2575 | "source": [ 2576 | "Initialize an Numpy Array with Ones\n" 2577 | ] 2578 | }, 2579 | { 2580 | "cell_type": "code", 2581 | "execution_count": null, 2582 | "metadata": { 2583 | "colab": { 2584 | "base_uri": "https://localhost:8080/" 2585 | }, 2586 | "id": "Ab4ofM13HhjT", 2587 | "outputId": "326dbad4-cbd3-4ac5-8e0a-1fb4bb8e7687" 2588 | }, 2589 | "outputs": [ 2590 | { 2591 | "name": "stdout", 2592 | "output_type": "stream", 2593 | "text": [ 2594 | "[[1. 1.]\n", 2595 | " [1. 1.]]\n" 2596 | ] 2597 | } 2598 | ], 2599 | "source": [ 2600 | "import numpy as np\n", 2601 | "thearray = np.ones((2, 2))\n", 2602 | "print(thearray)" 2603 | ] 2604 | }, 2605 | { 2606 | "cell_type": "markdown", 2607 | "metadata": { 2608 | "id": "ZnJysfw7HoYO" 2609 | }, 2610 | "source": [ 2611 | "### **How to check if a NumPy array is empty in Python?**" 2612 | ] 2613 | }, 2614 | { 2615 | "cell_type": "markdown", 2616 | "metadata": { 2617 | "id": "sYKpkPex5i3C" 2618 | }, 2619 | "source": [ 2620 | "Use numpy.ndarray.size to check if array is empty\n" 2621 | ] 2622 | }, 2623 | { 2624 | "cell_type": "code", 2625 | "execution_count": null, 2626 | "metadata": { 2627 | "colab": { 2628 | "base_uri": "https://localhost:8080/" 2629 | }, 2630 | "id": "Ss9NT0925r_y", 2631 | "outputId": "cfab1e19-160a-4808-c6ab-a2427cb9ff6e" 2632 | }, 2633 | "outputs": [ 2634 | { 2635 | "name": "stdout", 2636 | "output_type": "stream", 2637 | "text": [ 2638 | "True\n", 2639 | "False\n" 2640 | ] 2641 | } 2642 | ], 2643 | "source": [ 2644 | "import numpy as np\n", 2645 | " \n", 2646 | "the_array = np.array([])\n", 2647 | "is_empty = the_array.size == 0\n", 2648 | "print(is_empty)\n", 2649 | " \n", 2650 | "the_array = np.array([1, 2, 3])\n", 2651 | "is_empty = the_array.size == 0\n", 2652 | "print(is_empty)" 2653 | ] 2654 | }, 2655 | { 2656 | "cell_type": "markdown", 2657 | "metadata": { 2658 | "id": "A3AkSE7H6STc" 2659 | }, 2660 | "source": [ 2661 | "### **How to create a 3D NumPy array with values in Python?**\n" 2662 | ] 2663 | }, 2664 | { 2665 | "cell_type": "markdown", 2666 | "metadata": { 2667 | "id": "ThzwKMkX6V0q" 2668 | }, 2669 | "source": [ 2670 | "3D numpy array with specific values" 2671 | ] 2672 | }, 2673 | { 2674 | "cell_type": "code", 2675 | "execution_count": null, 2676 | "metadata": { 2677 | "colab": { 2678 | "base_uri": "https://localhost:8080/" 2679 | }, 2680 | "id": "QxeI6F396bez", 2681 | "outputId": "ba727604-f9b5-4737-830e-bbf5c6c9965e" 2682 | }, 2683 | "outputs": [ 2684 | { 2685 | "name": "stdout", 2686 | "output_type": "stream", 2687 | "text": [ 2688 | "[[[1 2]\n", 2689 | " [3 4]]\n", 2690 | "\n", 2691 | " [[5 6]\n", 2692 | " [7 8]]]\n" 2693 | ] 2694 | } 2695 | ], 2696 | "source": [ 2697 | "import numpy as np\n", 2698 | "the_3d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n", 2699 | "print(the_3d_array)" 2700 | ] 2701 | }, 2702 | { 2703 | "cell_type": "markdown", 2704 | "metadata": { 2705 | "id": "qRqi95aN65VY" 2706 | }, 2707 | "source": [ 2708 | "### **How to create a 3D NumPy array of Zeros in Python?**\n" 2709 | ] 2710 | }, 2711 | { 2712 | "cell_type": "markdown", 2713 | "metadata": { 2714 | "id": "047xxgVl7BLf" 2715 | }, 2716 | "source": [ 2717 | "Create 3D Numpy array of Zeros\n" 2718 | ] 2719 | }, 2720 | { 2721 | "cell_type": "code", 2722 | "execution_count": null, 2723 | "metadata": { 2724 | "colab": { 2725 | "base_uri": "https://localhost:8080/" 2726 | }, 2727 | "id": "o3zk5jSV7EHR", 2728 | "outputId": "16e06625-883f-4c8d-af2a-9cd962a1a213" 2729 | }, 2730 | "outputs": [ 2731 | { 2732 | "name": "stdout", 2733 | "output_type": "stream", 2734 | "text": [ 2735 | "[[[0. 0.]\n", 2736 | " [0. 0.]]\n", 2737 | "\n", 2738 | " [[0. 0.]\n", 2739 | " [0. 0.]]]\n" 2740 | ] 2741 | } 2742 | ], 2743 | "source": [ 2744 | "import numpy as np\n", 2745 | "the_3d_array = np.zeros((2, 2, 2))\n", 2746 | "print(the_3d_array)" 2747 | ] 2748 | }, 2749 | { 2750 | "cell_type": "markdown", 2751 | "metadata": { 2752 | "id": "BAedckNC7dfg" 2753 | }, 2754 | "source": [ 2755 | "### **How to create a 3D NumPy array of Ones in Python?**\n" 2756 | ] 2757 | }, 2758 | { 2759 | "cell_type": "markdown", 2760 | "metadata": { 2761 | "id": "TzSmn4Px74v1" 2762 | }, 2763 | "source": [ 2764 | "Create 3D Numpy array of Ones\n" 2765 | ] 2766 | }, 2767 | { 2768 | "cell_type": "code", 2769 | "execution_count": null, 2770 | "metadata": { 2771 | "colab": { 2772 | "base_uri": "https://localhost:8080/" 2773 | }, 2774 | "id": "RueHvXLQ78GQ", 2775 | "outputId": "b9c02173-7dac-408a-8660-187e22b8c1be" 2776 | }, 2777 | "outputs": [ 2778 | { 2779 | "name": "stdout", 2780 | "output_type": "stream", 2781 | "text": [ 2782 | "[[[1. 1.]\n", 2783 | " [1. 1.]]\n", 2784 | "\n", 2785 | " [[1. 1.]\n", 2786 | " [1. 1.]]]\n" 2787 | ] 2788 | } 2789 | ], 2790 | "source": [ 2791 | "import numpy as np\n", 2792 | "the_3d_array = np.ones((2, 2, 2))\n", 2793 | "print(the_3d_array)" 2794 | ] 2795 | }, 2796 | { 2797 | "cell_type": "markdown", 2798 | "metadata": { 2799 | "id": "0gnOXdl-8YUj" 2800 | }, 2801 | "source": [ 2802 | "### **5 examples to filter a NumPy array based on two conditions in Python**\n" 2803 | ] 2804 | }, 2805 | { 2806 | "cell_type": "markdown", 2807 | "metadata": { 2808 | "id": "wSe6a7c88dL3" 2809 | }, 2810 | "source": [ 2811 | "Example-1\n" 2812 | ] 2813 | }, 2814 | { 2815 | "cell_type": "code", 2816 | "execution_count": null, 2817 | "metadata": { 2818 | "colab": { 2819 | "base_uri": "https://localhost:8080/" 2820 | }, 2821 | "id": "lcEPf-yi8gKV", 2822 | "outputId": "80b2831a-041d-4cad-a84b-847dbf16bb1b" 2823 | }, 2824 | "outputs": [ 2825 | { 2826 | "name": "stdout", 2827 | "output_type": "stream", 2828 | "text": [ 2829 | "[4 5 6 7]\n" 2830 | ] 2831 | } 2832 | ], 2833 | "source": [ 2834 | "import numpy as np\n", 2835 | "the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 2836 | "filter_arr = np.logical_and(np.greater(the_array, 3), np.less(the_array, 8))\n", 2837 | "print(the_array[filter_arr])" 2838 | ] 2839 | }, 2840 | { 2841 | "cell_type": "markdown", 2842 | "metadata": { 2843 | "id": "iTcdCdZ08o66" 2844 | }, 2845 | "source": [ 2846 | "Example-2\n" 2847 | ] 2848 | }, 2849 | { 2850 | "cell_type": "code", 2851 | "execution_count": null, 2852 | "metadata": { 2853 | "colab": { 2854 | "base_uri": "https://localhost:8080/" 2855 | }, 2856 | "id": "t6v5S3vu8r6T", 2857 | "outputId": "d803c4ae-05f5-486a-ab29-aa7088f111bd" 2858 | }, 2859 | "outputs": [ 2860 | { 2861 | "name": "stdout", 2862 | "output_type": "stream", 2863 | "text": [ 2864 | "[1 2 4]\n" 2865 | ] 2866 | } 2867 | ], 2868 | "source": [ 2869 | "import numpy as np\n", 2870 | "the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 2871 | "filter_arr = np.logical_or(the_array < 3, the_array == 4)\n", 2872 | "print(the_array[filter_arr])" 2873 | ] 2874 | }, 2875 | { 2876 | "cell_type": "markdown", 2877 | "metadata": { 2878 | "id": "9gGV1RPR84Wz" 2879 | }, 2880 | "source": [ 2881 | "Example-3\n" 2882 | ] 2883 | }, 2884 | { 2885 | "cell_type": "code", 2886 | "execution_count": null, 2887 | "metadata": { 2888 | "colab": { 2889 | "base_uri": "https://localhost:8080/" 2890 | }, 2891 | "id": "xKkpAfg_87Xy", 2892 | "outputId": "2cce2ec6-9fa7-4752-8d43-2fdf7e51ab25" 2893 | }, 2894 | "outputs": [ 2895 | { 2896 | "name": "stdout", 2897 | "output_type": "stream", 2898 | "text": [ 2899 | "[1]\n" 2900 | ] 2901 | } 2902 | ], 2903 | "source": [ 2904 | "import numpy as np\n", 2905 | "the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 2906 | "filter_arr = np.logical_not(the_array > 1, the_array < 5)\n", 2907 | "print(the_array[filter_arr])" 2908 | ] 2909 | }, 2910 | { 2911 | "cell_type": "markdown", 2912 | "metadata": { 2913 | "id": "Ezh0cXAM9Ff4" 2914 | }, 2915 | "source": [ 2916 | "Example-4\n" 2917 | ] 2918 | }, 2919 | { 2920 | "cell_type": "code", 2921 | "execution_count": null, 2922 | "metadata": { 2923 | "colab": { 2924 | "base_uri": "https://localhost:8080/" 2925 | }, 2926 | "id": "Dn0uMx1p9IWh", 2927 | "outputId": "a778f471-48d9-41f8-8cae-478e0d6b136c" 2928 | }, 2929 | "outputs": [ 2930 | { 2931 | "name": "stdout", 2932 | "output_type": "stream", 2933 | "text": [ 2934 | "[1 2 3 4 8]\n" 2935 | ] 2936 | } 2937 | ], 2938 | "source": [ 2939 | "import numpy as np\n", 2940 | "the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 2941 | "filter_arr = np.logical_or(the_array == 8, the_array < 5)\n", 2942 | "print(the_array[filter_arr])" 2943 | ] 2944 | }, 2945 | { 2946 | "cell_type": "markdown", 2947 | "metadata": { 2948 | "id": "IHjW-f3r9OH4" 2949 | }, 2950 | "source": [ 2951 | "Example-5\n" 2952 | ] 2953 | }, 2954 | { 2955 | "cell_type": "code", 2956 | "execution_count": null, 2957 | "metadata": { 2958 | "colab": { 2959 | "base_uri": "https://localhost:8080/" 2960 | }, 2961 | "id": "Pcga4NfB9U3O", 2962 | "outputId": "2857a9a0-82f3-481f-b86d-85abcf498e9e" 2963 | }, 2964 | "outputs": [ 2965 | { 2966 | "name": "stdout", 2967 | "output_type": "stream", 2968 | "text": [ 2969 | "[]\n" 2970 | ] 2971 | } 2972 | ], 2973 | "source": [ 2974 | "import numpy as np\n", 2975 | "the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 2976 | "filter_arr = np.logical_and(the_array == 8, the_array < 5)\n", 2977 | "print(the_array[filter_arr])" 2978 | ] 2979 | }, 2980 | { 2981 | "cell_type": "markdown", 2982 | "metadata": { 2983 | "id": "6-vmS0wE9hxx" 2984 | }, 2985 | "source": [ 2986 | "### **How to find the index of the max value in a NumPy array in Python?**\n" 2987 | ] 2988 | }, 2989 | { 2990 | "cell_type": "markdown", 2991 | "metadata": { 2992 | "id": "rjDYMzrO9pGZ" 2993 | }, 2994 | "source": [ 2995 | "Find index of max value\n" 2996 | ] 2997 | }, 2998 | { 2999 | "cell_type": "code", 3000 | "execution_count": null, 3001 | "metadata": { 3002 | "colab": { 3003 | "base_uri": "https://localhost:8080/" 3004 | }, 3005 | "id": "xzlX5uEr9rtJ", 3006 | "outputId": "f27eef11-6835-4fd8-9e65-d41c4db3dbde" 3007 | }, 3008 | "outputs": [ 3009 | { 3010 | "name": "stdout", 3011 | "output_type": "stream", 3012 | "text": [ 3013 | "2\n" 3014 | ] 3015 | } 3016 | ], 3017 | "source": [ 3018 | "\n", 3019 | "import numpy as np\n", 3020 | " \n", 3021 | "the_array = np.array([11, 22, 53, 14, 15])\n", 3022 | " \n", 3023 | "max_index_col = np.argmax(the_array, axis=0)\n", 3024 | "print(max_index_col)" 3025 | ] 3026 | }, 3027 | { 3028 | "cell_type": "markdown", 3029 | "metadata": { 3030 | "id": "w98saCPI99p9" 3031 | }, 3032 | "source": [ 3033 | "### **How to append a NumPy array in Python?**\n" 3034 | ] 3035 | }, 3036 | { 3037 | "cell_type": "markdown", 3038 | "metadata": { 3039 | "id": "fwqesq2E-BUZ" 3040 | }, 3041 | "source": [ 3042 | "Append Numpy Array\n" 3043 | ] 3044 | }, 3045 | { 3046 | "cell_type": "code", 3047 | "execution_count": null, 3048 | "metadata": { 3049 | "colab": { 3050 | "base_uri": "https://localhost:8080/" 3051 | }, 3052 | "id": "JguKWio_-Dra", 3053 | "outputId": "1ec2d331-7c47-48d6-f203-65bf9968ba50" 3054 | }, 3055 | "outputs": [ 3056 | { 3057 | "name": "stdout", 3058 | "output_type": "stream", 3059 | "text": [ 3060 | "[[0 1]\n", 3061 | " [2 3]\n", 3062 | " [4 5]]\n", 3063 | "**********\n", 3064 | "[[0 1 7]\n", 3065 | " [2 3 8]\n", 3066 | " [4 5 9]]\n" 3067 | ] 3068 | } 3069 | ], 3070 | "source": [ 3071 | "import numpy as np\n", 3072 | "the_array = np.array([[0, 1], [2, 3]])\n", 3073 | "row_to_append = np.array([[4, 5]])\n", 3074 | "the_array = np.append(the_array, row_to_append, 0)\n", 3075 | "print(the_array)\n", 3076 | "print('*' * 10)\n", 3077 | "columns_to_append = np.array([[7], [8], [9]])\n", 3078 | "the_array = np.append(the_array, columns_to_append, 1)\n", 3079 | "print(the_array)" 3080 | ] 3081 | }, 3082 | { 3083 | "cell_type": "markdown", 3084 | "metadata": { 3085 | "id": "gGyzv9xV-eHx" 3086 | }, 3087 | "source": [ 3088 | "### **How to multiply each element of Numpy array in Python?**\n" 3089 | ] 3090 | }, 3091 | { 3092 | "cell_type": "markdown", 3093 | "metadata": { 3094 | "id": "4Ov_wpLb-h8q" 3095 | }, 3096 | "source": [ 3097 | "Example-1\n" 3098 | ] 3099 | }, 3100 | { 3101 | "cell_type": "code", 3102 | "execution_count": null, 3103 | "metadata": { 3104 | "colab": { 3105 | "base_uri": "https://localhost:8080/" 3106 | }, 3107 | "id": "NEqiAkk0-kEk", 3108 | "outputId": "40b17a52-7168-406b-9ef9-0b3f51d2b313" 3109 | }, 3110 | "outputs": [ 3111 | { 3112 | "name": "stdout", 3113 | "output_type": "stream", 3114 | "text": [ 3115 | "36\n" 3116 | ] 3117 | } 3118 | ], 3119 | "source": [ 3120 | "import numpy as np\n", 3121 | "the_array = np.array([[1, 2, 3], [1, 2, 3]])\n", 3122 | "prod = np.prod(the_array)\n", 3123 | "print(prod)" 3124 | ] 3125 | }, 3126 | { 3127 | "cell_type": "markdown", 3128 | "metadata": { 3129 | "id": "YeCQvkit-2UU" 3130 | }, 3131 | "source": [ 3132 | "Example-2\n" 3133 | ] 3134 | }, 3135 | { 3136 | "cell_type": "code", 3137 | "execution_count": null, 3138 | "metadata": { 3139 | "colab": { 3140 | "base_uri": "https://localhost:8080/" 3141 | }, 3142 | "id": "I9QuCTAp-6IW", 3143 | "outputId": "09b3b62e-3a7f-4cd8-a530-a3db78b07ede" 3144 | }, 3145 | "outputs": [ 3146 | { 3147 | "name": "stdout", 3148 | "output_type": "stream", 3149 | "text": [ 3150 | "[1 4 9]\n" 3151 | ] 3152 | } 3153 | ], 3154 | "source": [ 3155 | "import numpy as np\n", 3156 | "the_array = np.array([[1, 2, 3], [1, 2, 3]])\n", 3157 | "prod = np.prod(the_array, 0)\n", 3158 | "print(prod)" 3159 | ] 3160 | }, 3161 | { 3162 | "cell_type": "markdown", 3163 | "metadata": { 3164 | "id": "3PM1c6ZM_CWH" 3165 | }, 3166 | "source": [ 3167 | "Example-3\n" 3168 | ] 3169 | }, 3170 | { 3171 | "cell_type": "code", 3172 | "execution_count": null, 3173 | "metadata": { 3174 | "colab": { 3175 | "base_uri": "https://localhost:8080/" 3176 | }, 3177 | "id": "TUUnYdMD_E0i", 3178 | "outputId": "93b8bce7-f5a0-4fd6-f993-809d49f67a2d" 3179 | }, 3180 | "outputs": [ 3181 | { 3182 | "name": "stdout", 3183 | "output_type": "stream", 3184 | "text": [ 3185 | "[6 6]\n" 3186 | ] 3187 | } 3188 | ], 3189 | "source": [ 3190 | "import numpy as np\n", 3191 | "the_array = np.array([[1, 2, 3], [1, 2, 3]])\n", 3192 | "prod = np.prod(the_array, 1)\n", 3193 | "print(prod)" 3194 | ] 3195 | }, 3196 | { 3197 | "cell_type": "markdown", 3198 | "metadata": { 3199 | "id": "c4-b0G5S_LMD" 3200 | }, 3201 | "source": [ 3202 | "Example-4\n" 3203 | ] 3204 | }, 3205 | { 3206 | "cell_type": "code", 3207 | "execution_count": null, 3208 | "metadata": { 3209 | "colab": { 3210 | "base_uri": "https://localhost:8080/" 3211 | }, 3212 | "id": "OVNy2YXZ_NFb", 3213 | "outputId": "b7df5a22-6c0d-44fb-d906-9f1abd693136" 3214 | }, 3215 | "outputs": [ 3216 | { 3217 | "name": "stdout", 3218 | "output_type": "stream", 3219 | "text": [ 3220 | "6\n" 3221 | ] 3222 | } 3223 | ], 3224 | "source": [ 3225 | "import numpy as np\n", 3226 | "the_array = np.array([1, 2, 3])\n", 3227 | "prod = np.prod(the_array)\n", 3228 | "print(prod)" 3229 | ] 3230 | }, 3231 | { 3232 | "cell_type": "markdown", 3233 | "metadata": { 3234 | "id": "AuTaC9bU_acU" 3235 | }, 3236 | "source": [ 3237 | "### **How to add a column to a NumPy array in Python?**\n" 3238 | ] 3239 | }, 3240 | { 3241 | "cell_type": "markdown", 3242 | "metadata": { 3243 | "id": "r9oGHG8B_e_f" 3244 | }, 3245 | "source": [ 3246 | "Append Column Numpy Array\n" 3247 | ] 3248 | }, 3249 | { 3250 | "cell_type": "code", 3251 | "execution_count": null, 3252 | "metadata": { 3253 | "colab": { 3254 | "base_uri": "https://localhost:8080/" 3255 | }, 3256 | "id": "FzyIfRHt_g3L", 3257 | "outputId": "c1698c7e-9817-40a6-d773-0801c3830bd7" 3258 | }, 3259 | "outputs": [ 3260 | { 3261 | "name": "stdout", 3262 | "output_type": "stream", 3263 | "text": [ 3264 | "[[1 2 5]\n", 3265 | " [3 4 6]]\n" 3266 | ] 3267 | } 3268 | ], 3269 | "source": [ 3270 | "import numpy as np\n", 3271 | "the_array = np.array([[1, 2], [3, 4]])\n", 3272 | "columns_to_append = np.array([[5], [6]])\n", 3273 | "the_array = np.append(the_array, columns_to_append, 1)\n", 3274 | "print(the_array)" 3275 | ] 3276 | }, 3277 | { 3278 | "cell_type": "markdown", 3279 | "metadata": { 3280 | "id": "43tiG0CT_uxo" 3281 | }, 3282 | "source": [ 3283 | "### **How to add a column to a NumPy array in Python?**\n" 3284 | ] 3285 | }, 3286 | { 3287 | "cell_type": "markdown", 3288 | "metadata": { 3289 | "id": "6Wurzomw_z-5" 3290 | }, 3291 | "source": [ 3292 | "Append Column Numpy Array\n" 3293 | ] 3294 | }, 3295 | { 3296 | "cell_type": "code", 3297 | "execution_count": null, 3298 | "metadata": { 3299 | "colab": { 3300 | "base_uri": "https://localhost:8080/" 3301 | }, 3302 | "id": "y4lexlTK_1_O", 3303 | "outputId": "596f3801-b573-4b7e-a276-afda10368a67" 3304 | }, 3305 | "outputs": [ 3306 | { 3307 | "name": "stdout", 3308 | "output_type": "stream", 3309 | "text": [ 3310 | "[[1 2 5]\n", 3311 | " [3 4 6]]\n" 3312 | ] 3313 | } 3314 | ], 3315 | "source": [ 3316 | "import numpy as np\n", 3317 | "the_array = np.array([[1, 2], [3, 4]])\n", 3318 | "columns_to_append = np.array([[5], [6]])\n", 3319 | "the_array = np.append(the_array, columns_to_append, 1)\n", 3320 | "print(the_array)" 3321 | ] 3322 | }, 3323 | { 3324 | "cell_type": "markdown", 3325 | "metadata": { 3326 | "id": "i7R1vYqUPQ5h" 3327 | }, 3328 | "source": [ 3329 | "### **How to add a list to a NumPy array in Python?**\n" 3330 | ] 3331 | }, 3332 | { 3333 | "cell_type": "markdown", 3334 | "metadata": { 3335 | "id": "3vqwiGG0PX0k" 3336 | }, 3337 | "source": [ 3338 | "Insert list in Numpy Array\n" 3339 | ] 3340 | }, 3341 | { 3342 | "cell_type": "code", 3343 | "execution_count": null, 3344 | "metadata": { 3345 | "colab": { 3346 | "base_uri": "https://localhost:8080/" 3347 | }, 3348 | "id": "LEx-VSzWPabH", 3349 | "outputId": "a3221fc7-39f5-4953-ffc0-6f875c748ec4" 3350 | }, 3351 | "outputs": [ 3352 | { 3353 | "name": "stdout", 3354 | "output_type": "stream", 3355 | "text": [ 3356 | "[[1 2 5]\n", 3357 | " [3 4 6]]\n" 3358 | ] 3359 | } 3360 | ], 3361 | "source": [ 3362 | "import numpy as np\n", 3363 | "the_array = np.array([[1, 2], [3, 4]])\n", 3364 | "columns_to_append = [5, 6]\n", 3365 | "the_array = np.insert(the_array, 2, columns_to_append, axis=1)\n", 3366 | "print(the_array)" 3367 | ] 3368 | }, 3369 | { 3370 | "cell_type": "markdown", 3371 | "metadata": { 3372 | "id": "VpE6Q8V-PnTA" 3373 | }, 3374 | "source": [ 3375 | "### **How to convert a NumPy array into a list in Python?**\n" 3376 | ] 3377 | }, 3378 | { 3379 | "cell_type": "markdown", 3380 | "metadata": { 3381 | "id": "attXjYHJP0bl" 3382 | }, 3383 | "source": [ 3384 | "Numpy array to List\n" 3385 | ] 3386 | }, 3387 | { 3388 | "cell_type": "code", 3389 | "execution_count": null, 3390 | "metadata": { 3391 | "colab": { 3392 | "base_uri": "https://localhost:8080/" 3393 | }, 3394 | "id": "B-aXFgWDP2hK", 3395 | "outputId": "695a3522-4b46-448f-bc63-54dec9422f29" 3396 | }, 3397 | "outputs": [ 3398 | { 3399 | "name": "stdout", 3400 | "output_type": "stream", 3401 | "text": [ 3402 | "[[1, 2], [3, 4]]\n" 3403 | ] 3404 | } 3405 | ], 3406 | "source": [ 3407 | "import numpy as np\n", 3408 | "the_array = np.array([[1, 2], [3, 4]])\n", 3409 | "print(the_array.tolist())" 3410 | ] 3411 | }, 3412 | { 3413 | "cell_type": "markdown", 3414 | "metadata": { 3415 | "id": "N7ov2bZMQAkA" 3416 | }, 3417 | "source": [ 3418 | "### **How to delete values from a NumPy array in Python?**\n" 3419 | ] 3420 | }, 3421 | { 3422 | "cell_type": "markdown", 3423 | "metadata": { 3424 | "id": "3UidaqMMQF18" 3425 | }, 3426 | "source": [ 3427 | "Example-1\n" 3428 | ] 3429 | }, 3430 | { 3431 | "cell_type": "code", 3432 | "execution_count": null, 3433 | "metadata": { 3434 | "colab": { 3435 | "base_uri": "https://localhost:8080/" 3436 | }, 3437 | "id": "tZjFGv-eQK6T", 3438 | "outputId": "971b7e4f-a881-416a-d2bc-934ccbd6d68c" 3439 | }, 3440 | "outputs": [ 3441 | { 3442 | "name": "stdout", 3443 | "output_type": "stream", 3444 | "text": [ 3445 | "[[1 2]\n", 3446 | " [3 4]]\n", 3447 | "[1 4]\n" 3448 | ] 3449 | } 3450 | ], 3451 | "source": [ 3452 | "import numpy as np\n", 3453 | "the_array = np.array([[1, 2], [3, 4]])\n", 3454 | "print(the_array)\n", 3455 | "the_array = np.delete(the_array, [1, 2])\n", 3456 | "print(the_array)" 3457 | ] 3458 | }, 3459 | { 3460 | "cell_type": "markdown", 3461 | "metadata": { 3462 | "id": "SKicj58BQWBM" 3463 | }, 3464 | "source": [ 3465 | "Example-2\n" 3466 | ] 3467 | }, 3468 | { 3469 | "cell_type": "code", 3470 | "execution_count": null, 3471 | "metadata": { 3472 | "colab": { 3473 | "base_uri": "https://localhost:8080/" 3474 | }, 3475 | "id": "fwqH9OigQaGz", 3476 | "outputId": "43f7526c-5eca-4d42-fbfa-6c5a6ea36ca2" 3477 | }, 3478 | "outputs": [ 3479 | { 3480 | "name": "stdout", 3481 | "output_type": "stream", 3482 | "text": [ 3483 | "[1 2 3 4]\n", 3484 | "[1 3 4]\n" 3485 | ] 3486 | } 3487 | ], 3488 | "source": [ 3489 | "import numpy as np\n", 3490 | "the_array = np.array([1, 2, 3, 4])\n", 3491 | "print(the_array)\n", 3492 | "the_array = np.delete(the_array, np.where(the_array == 2))\n", 3493 | "print(the_array)" 3494 | ] 3495 | }, 3496 | { 3497 | "cell_type": "markdown", 3498 | "metadata": { 3499 | "id": "SF48guvzQgVL" 3500 | }, 3501 | "source": [ 3502 | "Example-3\n" 3503 | ] 3504 | }, 3505 | { 3506 | "cell_type": "code", 3507 | "execution_count": null, 3508 | "metadata": { 3509 | "colab": { 3510 | "base_uri": "https://localhost:8080/" 3511 | }, 3512 | "id": "AmADtTc8Qj57", 3513 | "outputId": "c4629815-d021-4621-f3a7-c38d7b829595" 3514 | }, 3515 | "outputs": [ 3516 | { 3517 | "name": "stdout", 3518 | "output_type": "stream", 3519 | "text": [ 3520 | "[[1 2]\n", 3521 | " [3 4]]\n", 3522 | "[3 4]\n" 3523 | ] 3524 | } 3525 | ], 3526 | "source": [ 3527 | "import numpy as np\n", 3528 | "the_array = np.array([[1, 2], [3, 4]])\n", 3529 | "print(the_array)\n", 3530 | "the_array = np.delete(the_array, np.where(the_array == 3))\n", 3531 | "print(the_array)" 3532 | ] 3533 | }, 3534 | { 3535 | "cell_type": "markdown", 3536 | "metadata": { 3537 | "id": "Fh7XzLmVQ7pE" 3538 | }, 3539 | "source": [ 3540 | "### **How to check if a value exists in NumPy Array?**\n" 3541 | ] 3542 | }, 3543 | { 3544 | "cell_type": "markdown", 3545 | "metadata": { 3546 | "id": "TBxcL-s4TTbr" 3547 | }, 3548 | "source": [ 3549 | "Checking if a value exists in an Numpy array\n" 3550 | ] 3551 | }, 3552 | { 3553 | "cell_type": "code", 3554 | "execution_count": null, 3555 | "metadata": { 3556 | "colab": { 3557 | "base_uri": "https://localhost:8080/" 3558 | }, 3559 | "id": "UXGlcSaZTqIj", 3560 | "outputId": "fc4b27e3-61ec-4c97-e070-3e8ac63a0763" 3561 | }, 3562 | "outputs": [ 3563 | { 3564 | "name": "stdout", 3565 | "output_type": "stream", 3566 | "text": [ 3567 | "True\n" 3568 | ] 3569 | } 3570 | ], 3571 | "source": [ 3572 | "import numpy as np\n", 3573 | "\n", 3574 | "the_array = np.array([[1, 2], [3, 4]])\n", 3575 | "n = 3\n", 3576 | "\n", 3577 | "if n in the_array:\n", 3578 | " print(True)\n", 3579 | "else:\n", 3580 | " print(False)" 3581 | ] 3582 | }, 3583 | { 3584 | "cell_type": "markdown", 3585 | "metadata": { 3586 | "id": "RlU0BUoFVcXV" 3587 | }, 3588 | "source": [ 3589 | "### **How to get the transpose of a NumPy array in Python?**\n" 3590 | ] 3591 | }, 3592 | { 3593 | "cell_type": "markdown", 3594 | "metadata": { 3595 | "id": "djA0tF37WfRL" 3596 | }, 3597 | "source": [ 3598 | "Transpose of numpy Array\n" 3599 | ] 3600 | }, 3601 | { 3602 | "cell_type": "code", 3603 | "execution_count": null, 3604 | "metadata": { 3605 | "colab": { 3606 | "base_uri": "https://localhost:8080/" 3607 | }, 3608 | "id": "EA4RHT6hWiX9", 3609 | "outputId": "3166a0ab-a499-4b61-9540-fb24448ffd14" 3610 | }, 3611 | "outputs": [ 3612 | { 3613 | "name": "stdout", 3614 | "output_type": "stream", 3615 | "text": [ 3616 | "[[1 2]\n", 3617 | " [3 4]]\n", 3618 | "[[1 3]\n", 3619 | " [2 4]]\n" 3620 | ] 3621 | } 3622 | ], 3623 | "source": [ 3624 | "import numpy as np\n", 3625 | "the_array = np.array([[1, 2], [3, 4]])\n", 3626 | "print(the_array)\n", 3627 | "print(the_array.T)" 3628 | ] 3629 | }, 3630 | { 3631 | "cell_type": "markdown", 3632 | "metadata": { 3633 | "id": "AFOfJ1elWrXK" 3634 | }, 3635 | "source": [ 3636 | "### **How to count frequency of unique values in a NumPy array in Python?**\n" 3637 | ] 3638 | }, 3639 | { 3640 | "cell_type": "markdown", 3641 | "metadata": { 3642 | "id": "zhZpG4_5W9WI" 3643 | }, 3644 | "source": [ 3645 | "Find number of times a value appears in the list\n" 3646 | ] 3647 | }, 3648 | { 3649 | "cell_type": "code", 3650 | "execution_count": null, 3651 | "metadata": { 3652 | "colab": { 3653 | "base_uri": "https://localhost:8080/" 3654 | }, 3655 | "id": "yOxz_69ZW_7t", 3656 | "outputId": "35d90e0c-f717-4dae-c133-34020af967d4" 3657 | }, 3658 | "outputs": [ 3659 | { 3660 | "name": "stdout", 3661 | "output_type": "stream", 3662 | "text": [ 3663 | "[[3 1]\n", 3664 | " [4 1]\n", 3665 | " [5 1]\n", 3666 | " [7 2]\n", 3667 | " [9 2]]\n" 3668 | ] 3669 | } 3670 | ], 3671 | "source": [ 3672 | "import numpy as np\n", 3673 | "the_array = np.array([9, 7, 4, 7, 3, 5, 9])\n", 3674 | "frequencies = np.asarray((np.unique(the_array, return_counts=True))).T\n", 3675 | "print(frequencies)" 3676 | ] 3677 | }, 3678 | { 3679 | "cell_type": "markdown", 3680 | "metadata": { 3681 | "id": "5yGED2MaX5Hq" 3682 | }, 3683 | "source": [ 3684 | "### **How to repeat a row in NumPy array in Python?**\n" 3685 | ] 3686 | }, 3687 | { 3688 | "cell_type": "markdown", 3689 | "metadata": { 3690 | "id": "8cxPk7iXX-BU" 3691 | }, 3692 | "source": [ 3693 | "Repeat Row\n" 3694 | ] 3695 | }, 3696 | { 3697 | "cell_type": "code", 3698 | "execution_count": null, 3699 | "metadata": { 3700 | "colab": { 3701 | "base_uri": "https://localhost:8080/" 3702 | }, 3703 | "id": "HHPBB2_JX_8c", 3704 | "outputId": "40d11621-b4cb-4c91-9882-70b251a96805" 3705 | }, 3706 | "outputs": [ 3707 | { 3708 | "name": "stdout", 3709 | "output_type": "stream", 3710 | "text": [ 3711 | "[[1 2 3]\n", 3712 | " [1 2 3]\n", 3713 | " [1 2 3]]\n" 3714 | ] 3715 | } 3716 | ], 3717 | "source": [ 3718 | "import numpy as np\n", 3719 | "the_array = np.array([1, 2, 3])\n", 3720 | "repeat = 3\n", 3721 | "new_array = np.tile(the_array, (repeat, 1))\n", 3722 | "print(new_array)" 3723 | ] 3724 | }, 3725 | { 3726 | "cell_type": "markdown", 3727 | "metadata": { 3728 | "id": "22e4gvf1YKfk" 3729 | }, 3730 | "source": [ 3731 | "### **How to repeat a column in NumPy array in Python?**\n" 3732 | ] 3733 | }, 3734 | { 3735 | "cell_type": "markdown", 3736 | "metadata": { 3737 | "id": "VkP2s0YQYOTx" 3738 | }, 3739 | "source": [ 3740 | "Repeat Column\n" 3741 | ] 3742 | }, 3743 | { 3744 | "cell_type": "code", 3745 | "execution_count": null, 3746 | "metadata": { 3747 | "colab": { 3748 | "base_uri": "https://localhost:8080/" 3749 | }, 3750 | "id": "aUp-fn_lYRNB", 3751 | "outputId": "e9f23298-f301-4f91-e810-4178d72fb249" 3752 | }, 3753 | "outputs": [ 3754 | { 3755 | "name": "stdout", 3756 | "output_type": "stream", 3757 | "text": [ 3758 | "[[1 1 1]\n", 3759 | " [2 2 2]\n", 3760 | " [3 3 3]]\n" 3761 | ] 3762 | } 3763 | ], 3764 | "source": [ 3765 | "import numpy as np\n", 3766 | "the_array = np.array([1, 2, 3])\n", 3767 | "repeat = 3\n", 3768 | "new_array = np.transpose([the_array] * repeat)\n", 3769 | "print(new_array)" 3770 | ] 3771 | }, 3772 | { 3773 | "cell_type": "markdown", 3774 | "metadata": { 3775 | "id": "B5T7WySKgyjD" 3776 | }, 3777 | "source": [ 3778 | "### **Numpy get random set of rows from 2D array**\n" 3779 | ] 3780 | }, 3781 | { 3782 | "cell_type": "markdown", 3783 | "metadata": { 3784 | "id": "NRlSV82PhnKO" 3785 | }, 3786 | "source": [ 3787 | "Example-1\n" 3788 | ] 3789 | }, 3790 | { 3791 | "cell_type": "code", 3792 | "execution_count": null, 3793 | "metadata": { 3794 | "colab": { 3795 | "base_uri": "https://localhost:8080/" 3796 | }, 3797 | "id": "JA3DNk2VhpMb", 3798 | "outputId": "140f6812-6618-4c99-dcdf-aff447ec075e" 3799 | }, 3800 | "outputs": [ 3801 | { 3802 | "name": "stdout", 3803 | "output_type": "stream", 3804 | "text": [ 3805 | "[[20 21 22 23 24 25 26 27 28 29]\n", 3806 | " [40 41 42 43 44 45 46 47 48 49]]\n" 3807 | ] 3808 | } 3809 | ], 3810 | "source": [ 3811 | "# import module\n", 3812 | "import numpy as np\n", 3813 | "# create 2D array\n", 3814 | "the_array = np.arange(50).reshape((5, 10))\n", 3815 | "# row manipulation\n", 3816 | "np.random.shuffle(the_array)\n", 3817 | "# display random rows\n", 3818 | "rows = the_array[:2, :]\n", 3819 | "print(rows)" 3820 | ] 3821 | }, 3822 | { 3823 | "cell_type": "markdown", 3824 | "metadata": { 3825 | "id": "71vj8yVYh3bj" 3826 | }, 3827 | "source": [ 3828 | "Example-2\n" 3829 | ] 3830 | }, 3831 | { 3832 | "cell_type": "code", 3833 | "execution_count": null, 3834 | "metadata": { 3835 | "colab": { 3836 | "base_uri": "https://localhost:8080/" 3837 | }, 3838 | "id": "yCt4CbSxh5rK", 3839 | "outputId": "7513aeb6-43ec-436a-8edb-b96e92a5d550" 3840 | }, 3841 | "outputs": [ 3842 | { 3843 | "name": "stdout", 3844 | "output_type": "stream", 3845 | "text": [ 3846 | "[[ 8 9 10 11]\n", 3847 | " [ 4 5 6 7]]\n" 3848 | ] 3849 | } 3850 | ], 3851 | "source": [ 3852 | "# import module\n", 3853 | "import random\n", 3854 | "import numpy as np\n", 3855 | "# create 2D array\n", 3856 | "the_array = np.arange(16).reshape((4, 4))\n", 3857 | "# row manipulation\n", 3858 | "rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)\n", 3859 | "# display random rows\n", 3860 | "rows = the_array[rows_id, :]\n", 3861 | "print(rows)" 3862 | ] 3863 | }, 3864 | { 3865 | "cell_type": "markdown", 3866 | "metadata": { 3867 | "id": "yiGmE98ciGGi" 3868 | }, 3869 | "source": [ 3870 | "Example-3\n" 3871 | ] 3872 | }, 3873 | { 3874 | "cell_type": "code", 3875 | "execution_count": null, 3876 | "metadata": { 3877 | "colab": { 3878 | "base_uri": "https://localhost:8080/" 3879 | }, 3880 | "id": "ktvd45I9iIdo", 3881 | "outputId": "0e2d4540-3ea0-4cd0-def0-27f427994f27" 3882 | }, 3883 | "outputs": [ 3884 | { 3885 | "name": "stdout", 3886 | "output_type": "stream", 3887 | "text": [ 3888 | "[[ 4 5 6 7]\n", 3889 | " [ 8 9 10 11]]\n" 3890 | ] 3891 | } 3892 | ], 3893 | "source": [ 3894 | "# import module\n", 3895 | "import numpy as np\n", 3896 | "# create 2D array\n", 3897 | "the_array = np.arange(16).reshape((4, 4))\n", 3898 | "number_of_rows = the_array.shape[0]\n", 3899 | "random_indices = np.random.choice(number_of_rows, \n", 3900 | " size=2, replace=False)\n", 3901 | "# display random rows\n", 3902 | "rows = the_array[random_indices, :]\n", 3903 | "print(rows)" 3904 | ] 3905 | }, 3906 | { 3907 | "cell_type": "markdown", 3908 | "metadata": { 3909 | "id": "a9whYZcajEj0" 3910 | }, 3911 | "source": [ 3912 | "### **How do I randomly select rows from NumPy array?**\n" 3913 | ] 3914 | }, 3915 | { 3916 | "cell_type": "markdown", 3917 | "metadata": { 3918 | "id": "BMPP7Ow1jIj4" 3919 | }, 3920 | "source": [ 3921 | "Example-1\n" 3922 | ] 3923 | }, 3924 | { 3925 | "cell_type": "code", 3926 | "execution_count": null, 3927 | "metadata": { 3928 | "colab": { 3929 | "base_uri": "https://localhost:8080/" 3930 | }, 3931 | "id": "QPck1gjgjLm7", 3932 | "outputId": "4faabc41-1f83-4976-d37a-00f30676c9a3" 3933 | }, 3934 | "outputs": [ 3935 | { 3936 | "name": "stdout", 3937 | "output_type": "stream", 3938 | "text": [ 3939 | "[[20 21 22 23 24 25 26 27 28 29]\n", 3940 | " [30 31 32 33 34 35 36 37 38 39]]\n" 3941 | ] 3942 | } 3943 | ], 3944 | "source": [ 3945 | "\n", 3946 | "# import module\n", 3947 | "import numpy as np\n", 3948 | " \n", 3949 | "# create 2D array\n", 3950 | "the_array = np.arange(50).reshape((5, 10))\n", 3951 | " \n", 3952 | "# row manipulation\n", 3953 | "np.random.shuffle(the_array)\n", 3954 | " \n", 3955 | "# display random rows\n", 3956 | "rows = the_array[:2, :]\n", 3957 | "print(rows)" 3958 | ] 3959 | }, 3960 | { 3961 | "cell_type": "markdown", 3962 | "metadata": { 3963 | "id": "Y-NeN_CpjPnx" 3964 | }, 3965 | "source": [ 3966 | "Example-2\n" 3967 | ] 3968 | }, 3969 | { 3970 | "cell_type": "code", 3971 | "execution_count": null, 3972 | "metadata": { 3973 | "colab": { 3974 | "base_uri": "https://localhost:8080/" 3975 | }, 3976 | "id": "M3ckuJiljUZK", 3977 | "outputId": "fe16178a-eda3-4ffa-a717-9f50c7482b0d" 3978 | }, 3979 | "outputs": [ 3980 | { 3981 | "name": "stdout", 3982 | "output_type": "stream", 3983 | "text": [ 3984 | "[[ 4 5 6 7]\n", 3985 | " [ 8 9 10 11]]\n" 3986 | ] 3987 | } 3988 | ], 3989 | "source": [ 3990 | "# import module\n", 3991 | "import random\n", 3992 | "import numpy as np\n", 3993 | "# create 2D array\n", 3994 | "the_array = np.arange(16).reshape((4, 4))\n", 3995 | "# row manipulation\n", 3996 | "rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)\n", 3997 | "# display random rows\n", 3998 | "rows = the_array[rows_id, :]\n", 3999 | "print(rows)" 4000 | ] 4001 | }, 4002 | { 4003 | "cell_type": "markdown", 4004 | "metadata": { 4005 | "id": "rO912j7ojdZa" 4006 | }, 4007 | "source": [ 4008 | "Example-3\n" 4009 | ] 4010 | }, 4011 | { 4012 | "cell_type": "code", 4013 | "execution_count": null, 4014 | "metadata": { 4015 | "colab": { 4016 | "base_uri": "https://localhost:8080/" 4017 | }, 4018 | "id": "4-TJYfEMjgfW", 4019 | "outputId": "5801e2b1-fcfb-435e-d5ec-ef0fc932d5d1" 4020 | }, 4021 | "outputs": [ 4022 | { 4023 | "name": "stdout", 4024 | "output_type": "stream", 4025 | "text": [ 4026 | "[[ 0 1 2 3]\n", 4027 | " [12 13 14 15]]\n" 4028 | ] 4029 | } 4030 | ], 4031 | "source": [ 4032 | "# import module\n", 4033 | "import numpy as np\n", 4034 | "# create 2D array\n", 4035 | "the_array = np.arange(16).reshape((4, 4))\n", 4036 | "number_of_rows = the_array.shape[0]\n", 4037 | "random_indices = np.random.choice(number_of_rows, \n", 4038 | " size=2, \n", 4039 | " replace=False)\n", 4040 | "# display random rows\n", 4041 | "rows = the_array[random_indices, :]\n", 4042 | "print(rows)" 4043 | ] 4044 | }, 4045 | { 4046 | "cell_type": "markdown", 4047 | "metadata": { 4048 | "id": "mO3dGNs5kjUa" 4049 | }, 4050 | "source": [ 4051 | "### **How do I generate a random number in NumPy?**\n" 4052 | ] 4053 | }, 4054 | { 4055 | "cell_type": "markdown", 4056 | "metadata": { 4057 | "id": "G8kYbZ-dkllZ" 4058 | }, 4059 | "source": [ 4060 | "Example-1\n" 4061 | ] 4062 | }, 4063 | { 4064 | "cell_type": "code", 4065 | "execution_count": null, 4066 | "metadata": { 4067 | "colab": { 4068 | "base_uri": "https://localhost:8080/" 4069 | }, 4070 | "id": "lS9PBc7Iknlh", 4071 | "outputId": "043ad3c9-e175-40ce-9721-2476eb990e30" 4072 | }, 4073 | "outputs": [ 4074 | { 4075 | "name": "stdout", 4076 | "output_type": "stream", 4077 | "text": [ 4078 | "[[10 11 12 13 14 15 16 17 18 19]\n", 4079 | " [40 41 42 43 44 45 46 47 48 49]]\n" 4080 | ] 4081 | } 4082 | ], 4083 | "source": [ 4084 | "# import module\n", 4085 | "import numpy as np\n", 4086 | "# create 2D array\n", 4087 | "the_array = np.arange(50).reshape((5, 10))\n", 4088 | "# row manipulation\n", 4089 | "np.random.shuffle(the_array)\n", 4090 | "# display random rows\n", 4091 | "rows = the_array[:2, :]\n", 4092 | "print(rows)" 4093 | ] 4094 | }, 4095 | { 4096 | "cell_type": "markdown", 4097 | "metadata": { 4098 | "id": "bfyGZFB8kw-N" 4099 | }, 4100 | "source": [ 4101 | "Example-2\n" 4102 | ] 4103 | }, 4104 | { 4105 | "cell_type": "code", 4106 | "execution_count": null, 4107 | "metadata": { 4108 | "colab": { 4109 | "base_uri": "https://localhost:8080/" 4110 | }, 4111 | "id": "Npxdap2Pky-0", 4112 | "outputId": "79268085-38ce-4039-a9d0-502824e1316d" 4113 | }, 4114 | "outputs": [ 4115 | { 4116 | "name": "stdout", 4117 | "output_type": "stream", 4118 | "text": [ 4119 | "[[ 4 5 6 7]\n", 4120 | " [ 8 9 10 11]]\n" 4121 | ] 4122 | } 4123 | ], 4124 | "source": [ 4125 | "# import module\n", 4126 | "import random\n", 4127 | "import numpy as np\n", 4128 | "# create 2D array\n", 4129 | "the_array = np.arange(16).reshape((4, 4))\n", 4130 | "# row manipulation\n", 4131 | "rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)\n", 4132 | "# display random rows\n", 4133 | "rows = the_array[rows_id, :]\n", 4134 | "print(rows)" 4135 | ] 4136 | }, 4137 | { 4138 | "cell_type": "markdown", 4139 | "metadata": { 4140 | "id": "103zYuFbk6x2" 4141 | }, 4142 | "source": [ 4143 | "Example-3\n" 4144 | ] 4145 | }, 4146 | { 4147 | "cell_type": "code", 4148 | "execution_count": null, 4149 | "metadata": { 4150 | "colab": { 4151 | "base_uri": "https://localhost:8080/" 4152 | }, 4153 | "id": "p53N33puk9FO", 4154 | "outputId": "d6b908b8-d758-40dd-e1cd-7c32796ef819" 4155 | }, 4156 | "outputs": [ 4157 | { 4158 | "name": "stdout", 4159 | "output_type": "stream", 4160 | "text": [ 4161 | "[[4 5 6 7]\n", 4162 | " [0 1 2 3]]\n" 4163 | ] 4164 | } 4165 | ], 4166 | "source": [ 4167 | "# import module\n", 4168 | "import numpy as np\n", 4169 | "# create 2D array\n", 4170 | "the_array = np.arange(16).reshape((4, 4))\n", 4171 | "number_of_rows = the_array.shape[0]\n", 4172 | "random_indices = np.random.choice(number_of_rows, size=2, replace=False)\n", 4173 | "# display random rows\n", 4174 | "rows = the_array[random_indices, :]\n", 4175 | "print(rows)" 4176 | ] 4177 | }, 4178 | { 4179 | "cell_type": "markdown", 4180 | "metadata": { 4181 | "id": "7VrRORbqlbEF" 4182 | }, 4183 | "source": [ 4184 | "### **How to print a NumPy array without scientific notation in Python?**\n" 4185 | ] 4186 | }, 4187 | { 4188 | "cell_type": "markdown", 4189 | "metadata": { 4190 | "id": "hIMQRlsBlm_g" 4191 | }, 4192 | "source": [ 4193 | "Suppress Scientific Notation\n" 4194 | ] 4195 | }, 4196 | { 4197 | "cell_type": "code", 4198 | "execution_count": null, 4199 | "metadata": { 4200 | "colab": { 4201 | "base_uri": "https://localhost:8080/" 4202 | }, 4203 | "id": "TyIwgVd_lo9J", 4204 | "outputId": "8fd73a1d-8e3f-4580-f33c-ff0238ec7a19" 4205 | }, 4206 | "outputs": [ 4207 | { 4208 | "name": "stdout", 4209 | "output_type": "stream", 4210 | "text": [ 4211 | "[3.740000 5162.000000 13683628846.639999 12783387559.860001 1.810000]\n" 4212 | ] 4213 | } 4214 | ], 4215 | "source": [ 4216 | "import numpy as np\n", 4217 | "np.set_printoptions(suppress=True,\n", 4218 | "formatter={'float_kind': '{:f}'.format})\n", 4219 | "the_array = np.array([3.74, 5162, 13683628846.64, 12783387559.86, 1.81])\n", 4220 | "print(the_array)" 4221 | ] 4222 | }, 4223 | { 4224 | "cell_type": "markdown", 4225 | "metadata": { 4226 | "id": "_FSTb0Til7UU" 4227 | }, 4228 | "source": [ 4229 | "### **How do you convert an array of strings to an array of floats in Python?**\n" 4230 | ] 4231 | }, 4232 | { 4233 | "cell_type": "markdown", 4234 | "metadata": { 4235 | "id": "68VR8cFqmOWH" 4236 | }, 4237 | "source": [ 4238 | "Convert Numpy array of Strings to an array of floats\n" 4239 | ] 4240 | }, 4241 | { 4242 | "cell_type": "code", 4243 | "execution_count": null, 4244 | "metadata": { 4245 | "colab": { 4246 | "base_uri": "https://localhost:8080/" 4247 | }, 4248 | "id": "53YObFVZmSZT", 4249 | "outputId": "18d76352-41b5-47ec-edaf-d9acc43d36bf" 4250 | }, 4251 | "outputs": [ 4252 | { 4253 | "name": "stdout", 4254 | "output_type": "stream", 4255 | "text": [ 4256 | "[1.100000 2.200000 3.300000]\n" 4257 | ] 4258 | } 4259 | ], 4260 | "source": [ 4261 | "import numpy as np\n", 4262 | "string_arr = np.array(['1.1', '2.2', '3.3'])\n", 4263 | "float_arr = string_arr.astype(np.float64)\n", 4264 | "print(float_arr)" 4265 | ] 4266 | }, 4267 | { 4268 | "cell_type": "markdown", 4269 | "metadata": { 4270 | "id": "MyEVHgndmlXi" 4271 | }, 4272 | "source": [ 4273 | "### **How do you convert an array of strings to an array of floats in Python?**\n" 4274 | ] 4275 | }, 4276 | { 4277 | "cell_type": "markdown", 4278 | "metadata": { 4279 | "id": "Nxk2Fv14m5qe" 4280 | }, 4281 | "source": [ 4282 | "Convert Numpy array of Strings to an array of floats\n" 4283 | ] 4284 | }, 4285 | { 4286 | "cell_type": "code", 4287 | "execution_count": null, 4288 | "metadata": { 4289 | "colab": { 4290 | "base_uri": "https://localhost:8080/" 4291 | }, 4292 | "id": "ykGZDYuOm7pG", 4293 | "outputId": "b964db7d-279f-431c-b1f3-566e33365701" 4294 | }, 4295 | "outputs": [ 4296 | { 4297 | "name": "stdout", 4298 | "output_type": "stream", 4299 | "text": [ 4300 | "[1.100000 2.200000 3.300000]\n" 4301 | ] 4302 | } 4303 | ], 4304 | "source": [ 4305 | "import numpy as np\n", 4306 | "\n", 4307 | "\n", 4308 | "string_arr = np.array(['1.1', '2.2', '3.3'])\n", 4309 | "float_arr = string_arr.astype(np.float64)\n", 4310 | "print(float_arr)" 4311 | ] 4312 | }, 4313 | { 4314 | "cell_type": "markdown", 4315 | "metadata": { 4316 | "id": "Se_txqiAnMOU" 4317 | }, 4318 | "source": [ 4319 | "### **5 examples pretty-print a Numpy array with given precision**\n" 4320 | ] 4321 | }, 4322 | { 4323 | "cell_type": "markdown", 4324 | "metadata": { 4325 | "id": "9R9O8DHwn54z" 4326 | }, 4327 | "source": [ 4328 | "Example-1\n" 4329 | ] 4330 | }, 4331 | { 4332 | "cell_type": "code", 4333 | "execution_count": null, 4334 | "metadata": { 4335 | "colab": { 4336 | "base_uri": "https://localhost:8080/" 4337 | }, 4338 | "id": "E_Hmg1ULn77A", 4339 | "outputId": "4c8127a2-e9e7-4788-df80-818d3be99328" 4340 | }, 4341 | "outputs": [ 4342 | { 4343 | "name": "stdout", 4344 | "output_type": "stream", 4345 | "text": [ 4346 | "[[1.100000 0.900000 0.000001]\n", 4347 | " [1.100000 0.900000 0.000001]\n", 4348 | " [1.100000 0.900000 0.000001]]\n", 4349 | "[[1.100000 0.900000 0.000001]\n", 4350 | " [1.100000 0.900000 0.000001]\n", 4351 | " [1.100000 0.900000 0.000001]]\n" 4352 | ] 4353 | } 4354 | ], 4355 | "source": [ 4356 | "import numpy as np\n", 4357 | "x = np.array([[1.1, 0.9, 1e-6]] * 3)\n", 4358 | "print(x)\n", 4359 | "print(np.array_str(x, precision=1, suppress_small=True))" 4360 | ] 4361 | }, 4362 | { 4363 | "cell_type": "markdown", 4364 | "metadata": { 4365 | "id": "K7aAuMJroHti" 4366 | }, 4367 | "source": [ 4368 | "Example-2\n" 4369 | ] 4370 | }, 4371 | { 4372 | "cell_type": "code", 4373 | "execution_count": null, 4374 | "metadata": { 4375 | "colab": { 4376 | "base_uri": "https://localhost:8080/" 4377 | }, 4378 | "id": "BWzr06yuoRe3", 4379 | "outputId": "b1596d89-8d40-417c-de35-2d4a3ffc9841" 4380 | }, 4381 | "outputs": [ 4382 | { 4383 | "name": "stdout", 4384 | "output_type": "stream", 4385 | "text": [ 4386 | "[0.163827 0.223324 0.314750 0.479486 0.562441 0.404024 0.518934 0.920951\n", 4387 | " 0.331923 0.670102]\n", 4388 | "[0.164 0.223 0.315 0.479 0.562 0.404 0.519 0.921 0.332 0.67 ]\n" 4389 | ] 4390 | } 4391 | ], 4392 | "source": [ 4393 | "import numpy as np\n", 4394 | "x = np.random.random(10)\n", 4395 | "print(x)\n", 4396 | "np.set_printoptions(precision=3)\n", 4397 | "print(x)" 4398 | ] 4399 | }, 4400 | { 4401 | "cell_type": "markdown", 4402 | "metadata": { 4403 | "id": "vt7sWJJvoZsp" 4404 | }, 4405 | "source": [ 4406 | "Example-3\n" 4407 | ] 4408 | }, 4409 | { 4410 | "cell_type": "code", 4411 | "execution_count": null, 4412 | "metadata": { 4413 | "colab": { 4414 | "base_uri": "https://localhost:8080/" 4415 | }, 4416 | "id": "P-Wl4wnlobGH", 4417 | "outputId": "39ae7889-cbdd-4d48-e738-10ba4f1b05fc" 4418 | }, 4419 | "outputs": [ 4420 | { 4421 | "name": "stdout", 4422 | "output_type": "stream", 4423 | "text": [ 4424 | "[[1.1 0.9 0. ]\n", 4425 | " [1.1 0.9 0. ]\n", 4426 | " [1.1 0.9 0. ]]\n", 4427 | "[[1.1 0.9 0. ]\n", 4428 | " [1.1 0.9 0. ]\n", 4429 | " [1.1 0.9 0. ]]\n" 4430 | ] 4431 | } 4432 | ], 4433 | "source": [ 4434 | "import numpy as np\n", 4435 | "x = np.array([[1.1, 0.9, 1e-6]] * 3)\n", 4436 | "print(x)\n", 4437 | "np.set_printoptions(suppress=True)\n", 4438 | "print(x)" 4439 | ] 4440 | }, 4441 | { 4442 | "cell_type": "markdown", 4443 | "metadata": { 4444 | "id": "xZlTZNmoojss" 4445 | }, 4446 | "source": [ 4447 | "Example-4\n" 4448 | ] 4449 | }, 4450 | { 4451 | "cell_type": "code", 4452 | "execution_count": null, 4453 | "metadata": { 4454 | "colab": { 4455 | "base_uri": "https://localhost:8080/" 4456 | }, 4457 | "id": "Dcx83yzvopjw", 4458 | "outputId": "fa5a291e-72fc-4148-9320-7cb19bb2626c" 4459 | }, 4460 | "outputs": [ 4461 | { 4462 | "name": "stdout", 4463 | "output_type": "stream", 4464 | "text": [ 4465 | "[[1.1 0.9 0. ]\n", 4466 | " [1.1 0.9 0. ]\n", 4467 | " [1.1 0.9 0. ]]\n", 4468 | "[[ 1.100 0.900 0.000]\n", 4469 | " [ 1.100 0.900 0.000]\n", 4470 | " [ 1.100 0.900 0.000]]\n" 4471 | ] 4472 | } 4473 | ], 4474 | "source": [ 4475 | "import numpy as np\n", 4476 | "x = np.array([[1.1, 0.9, 1e-6]] * 3)\n", 4477 | "print(x)\n", 4478 | "np.set_printoptions(formatter={'float': '{: 0.3f}'.format})\n", 4479 | "print(x)" 4480 | ] 4481 | }, 4482 | { 4483 | "cell_type": "markdown", 4484 | "metadata": { 4485 | "id": "2uarzXIsoxn_" 4486 | }, 4487 | "source": [ 4488 | "Example-5\n" 4489 | ] 4490 | }, 4491 | { 4492 | "cell_type": "code", 4493 | "execution_count": null, 4494 | "metadata": { 4495 | "colab": { 4496 | "base_uri": "https://localhost:8080/" 4497 | }, 4498 | "id": "yzTdISz8o0pB", 4499 | "outputId": "f0824a71-153a-451c-c6d6-287f808937f3" 4500 | }, 4501 | "outputs": [ 4502 | { 4503 | "name": "stdout", 4504 | "output_type": "stream", 4505 | "text": [ 4506 | "[[1.502 1.865 3.182]\n", 4507 | " [8.692 4.561 3.605]\n", 4508 | " [7.122 5.532 0.107]]\n" 4509 | ] 4510 | } 4511 | ], 4512 | "source": [ 4513 | "import numpy as np\n", 4514 | "x = np.random.random((3, 3)) * 9\n", 4515 | "print(np.array2string(x, formatter={'float_kind': '{0:.3f}'.format}))" 4516 | ] 4517 | }, 4518 | { 4519 | "cell_type": "markdown", 4520 | "metadata": { 4521 | "id": "ZW8NBNqdpCos" 4522 | }, 4523 | "source": [ 4524 | "### **Convert Numpy array to JSON**\n" 4525 | ] 4526 | }, 4527 | { 4528 | "cell_type": "markdown", 4529 | "metadata": { 4530 | "id": "O2n834uspHkQ" 4531 | }, 4532 | "source": [ 4533 | "Array to JSON\n" 4534 | ] 4535 | }, 4536 | { 4537 | "cell_type": "code", 4538 | "execution_count": null, 4539 | "metadata": { 4540 | "colab": { 4541 | "base_uri": "https://localhost:8080/" 4542 | }, 4543 | "id": "8jFAhheGpKpd", 4544 | "outputId": "41658bd9-1e61-45d1-c4cd-5fc4c8673218" 4545 | }, 4546 | "outputs": [ 4547 | { 4548 | "name": "stdout", 4549 | "output_type": "stream", 4550 | "text": [ 4551 | "[{'x': 49, 'y': 7, 'z': 44}, {'x': 27, 'y': 13, 'z': 35}, {'x': 27, 'y': 13, 'z': 35}]\n" 4552 | ] 4553 | } 4554 | ], 4555 | "source": [ 4556 | "import numpy as np\n", 4557 | "the_array = np.array([[49, 7, 44], [27, 13, 35], [27, 13, 35]])\n", 4558 | "lists = the_array.tolist()\n", 4559 | "print([{'x': x[0], 'y': x[1], 'z': x[2]} for i, x in enumerate(lists)])" 4560 | ] 4561 | }, 4562 | { 4563 | "cell_type": "markdown", 4564 | "metadata": { 4565 | "id": "TH8O5Kfxqj4r" 4566 | }, 4567 | "source": [ 4568 | "### **How do you replace items that satisfy a condition with another value in Numpy array?**\n" 4569 | ] 4570 | }, 4571 | { 4572 | "cell_type": "markdown", 4573 | "metadata": { 4574 | "id": "OSE0d3ooqn4K" 4575 | }, 4576 | "source": [ 4577 | "Replace all elements which are greater than 30 to 0\n" 4578 | ] 4579 | }, 4580 | { 4581 | "cell_type": "code", 4582 | "execution_count": null, 4583 | "metadata": { 4584 | "colab": { 4585 | "base_uri": "https://localhost:8080/" 4586 | }, 4587 | "id": "srPLKHJ4qqEi", 4588 | "outputId": "84b6a519-df46-4d2c-9f8b-22f6fd72aae3" 4589 | }, 4590 | "outputs": [ 4591 | { 4592 | "name": "stdout", 4593 | "output_type": "stream", 4594 | "text": [ 4595 | "[ 0 7 0 27 13 0 0]\n" 4596 | ] 4597 | } 4598 | ], 4599 | "source": [ 4600 | "import numpy as np\n", 4601 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4602 | "an_array = np.where(the_array > 30, 0, the_array)\n", 4603 | "print(an_array)" 4604 | ] 4605 | }, 4606 | { 4607 | "cell_type": "markdown", 4608 | "metadata": { 4609 | "id": "crdYe0A4qyVo" 4610 | }, 4611 | "source": [ 4612 | "Replace all elements which are greater than 30 and less than 50 to 0\n" 4613 | ] 4614 | }, 4615 | { 4616 | "cell_type": "code", 4617 | "execution_count": null, 4618 | "metadata": { 4619 | "colab": { 4620 | "base_uri": "https://localhost:8080/" 4621 | }, 4622 | "id": "ZB-0Jhxiq1LC", 4623 | "outputId": "7db9cb40-01bd-42b5-fee5-f02f6bab7f7e" 4624 | }, 4625 | "outputs": [ 4626 | { 4627 | "name": "stdout", 4628 | "output_type": "stream", 4629 | "text": [ 4630 | "[ 0 7 0 27 13 0 71]\n" 4631 | ] 4632 | } 4633 | ], 4634 | "source": [ 4635 | "import numpy as np\n", 4636 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4637 | "an_array = np.where((the_array > 30) & (the_array < 50), 0, the_array)\n", 4638 | "print(an_array)" 4639 | ] 4640 | }, 4641 | { 4642 | "cell_type": "markdown", 4643 | "metadata": { 4644 | "id": "fi8ooZIwq7Cn" 4645 | }, 4646 | "source": [ 4647 | "Add 5 to all elements which are greater than 40\n" 4648 | ] 4649 | }, 4650 | { 4651 | "cell_type": "code", 4652 | "execution_count": null, 4653 | "metadata": { 4654 | "colab": { 4655 | "base_uri": "https://localhost:8080/" 4656 | }, 4657 | "id": "_f3zXBuYq-R9", 4658 | "outputId": "9b13c7f8-5774-4505-e058-8f9f36d9c2bf" 4659 | }, 4660 | "outputs": [ 4661 | { 4662 | "name": "stdout", 4663 | "output_type": "stream", 4664 | "text": [ 4665 | "[54 7 49 27 13 35 76]\n" 4666 | ] 4667 | } 4668 | ], 4669 | "source": [ 4670 | "import numpy as np\n", 4671 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4672 | "an_array = np.where(the_array > 40, the_array + 5, the_array)\n", 4673 | "print(an_array)" 4674 | ] 4675 | }, 4676 | { 4677 | "cell_type": "markdown", 4678 | "metadata": { 4679 | "id": "QeSbMcbrrDiR" 4680 | }, 4681 | "source": [ 4682 | "Replace all elements of array which greater than 25 with Nan\n" 4683 | ] 4684 | }, 4685 | { 4686 | "cell_type": "code", 4687 | "execution_count": null, 4688 | "metadata": { 4689 | "colab": { 4690 | "base_uri": "https://localhost:8080/" 4691 | }, 4692 | "id": "4n2ChGFirFrK", 4693 | "outputId": "f5b674ff-c662-43b6-ec7b-d5f829faf20e" 4694 | }, 4695 | "outputs": [ 4696 | { 4697 | "name": "stdout", 4698 | "output_type": "stream", 4699 | "text": [ 4700 | "[ nan 7.000 nan nan 13.000 nan nan]\n" 4701 | ] 4702 | } 4703 | ], 4704 | "source": [ 4705 | "import numpy as np\n", 4706 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4707 | "an_array = np.where(the_array > 25, np.NaN, the_array)\n", 4708 | "print(an_array)" 4709 | ] 4710 | }, 4711 | { 4712 | "cell_type": "markdown", 4713 | "metadata": { 4714 | "id": "oY__MAlErNT2" 4715 | }, 4716 | "source": [ 4717 | "Replace all elements of array which greater than 25 with 1 otherwise 0\n" 4718 | ] 4719 | }, 4720 | { 4721 | "cell_type": "code", 4722 | "execution_count": null, 4723 | "metadata": { 4724 | "colab": { 4725 | "base_uri": "https://localhost:8080/" 4726 | }, 4727 | "id": "SqpYdRQOrRgb", 4728 | "outputId": "340e194e-c784-4937-a1ad-33da5bb41eac" 4729 | }, 4730 | "outputs": [ 4731 | { 4732 | "name": "stdout", 4733 | "output_type": "stream", 4734 | "text": [ 4735 | "[1 0 1 1 0 1 1]\n" 4736 | ] 4737 | } 4738 | ], 4739 | "source": [ 4740 | "import numpy as np\n", 4741 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4742 | "an_array = np.asarray([0 if val < 25 else 1 for val in the_array])\n", 4743 | "print(an_array)" 4744 | ] 4745 | }, 4746 | { 4747 | "cell_type": "markdown", 4748 | "metadata": { 4749 | "id": "MceWQfoCrYeF" 4750 | }, 4751 | "source": [ 4752 | "### **How do I sort a NumPy array in descending order?**\n" 4753 | ] 4754 | }, 4755 | { 4756 | "cell_type": "markdown", 4757 | "metadata": { 4758 | "id": "za5Bku89ridj" 4759 | }, 4760 | "source": [ 4761 | "Sort Numpy in Descending Order\n" 4762 | ] 4763 | }, 4764 | { 4765 | "cell_type": "code", 4766 | "execution_count": null, 4767 | "metadata": { 4768 | "colab": { 4769 | "base_uri": "https://localhost:8080/" 4770 | }, 4771 | "id": "Wbz3bxxIrkcQ", 4772 | "outputId": "85796039-1460-4c53-f0ea-c4df385e0b51" 4773 | }, 4774 | "outputs": [ 4775 | { 4776 | "name": "stdout", 4777 | "output_type": "stream", 4778 | "text": [ 4779 | "[71 49 44 35 27 13 7]\n" 4780 | ] 4781 | } 4782 | ], 4783 | "source": [ 4784 | "import numpy as np\n", 4785 | "the_array = np.array([49, 7, 44, 27, 13, 35, 71])\n", 4786 | "sort_array = np.sort(the_array)[::-1]\n", 4787 | "print(sort_array)" 4788 | ] 4789 | }, 4790 | { 4791 | "cell_type": "markdown", 4792 | "metadata": { 4793 | "id": "trPzsgZErpDF" 4794 | }, 4795 | "source": [ 4796 | "Sort 2D Numpy in Descending Order\n" 4797 | ] 4798 | }, 4799 | { 4800 | "cell_type": "code", 4801 | "execution_count": null, 4802 | "metadata": { 4803 | "colab": { 4804 | "base_uri": "https://localhost:8080/" 4805 | }, 4806 | "id": "gX7Qfhwersk1", 4807 | "outputId": "5c52a036-347a-4dbd-9918-d8cab39a497c" 4808 | }, 4809 | "outputs": [ 4810 | { 4811 | "name": "stdout", 4812 | "output_type": "stream", 4813 | "text": [ 4814 | "[[ 4 7 49]\n", 4815 | " [13 27 35]]\n" 4816 | ] 4817 | } 4818 | ], 4819 | "source": [ 4820 | "import numpy as np\n", 4821 | "the_array = np.array([[49, 7, 4], [27, 13, 35]])\n", 4822 | "sort_array = np.sort(the_array)[::1]\n", 4823 | "print(sort_array)" 4824 | ] 4825 | }, 4826 | { 4827 | "cell_type": "markdown", 4828 | "metadata": { 4829 | "id": "IBuLpXY1rzMH" 4830 | }, 4831 | "source": [ 4832 | "Sort Numpy in Descending Order\n" 4833 | ] 4834 | }, 4835 | { 4836 | "cell_type": "code", 4837 | "execution_count": null, 4838 | "metadata": { 4839 | "colab": { 4840 | "base_uri": "https://localhost:8080/" 4841 | }, 4842 | "id": "Ish9ol9Mr1Jl", 4843 | "outputId": "54d8875d-8794-4662-dac6-b350aa454da5" 4844 | }, 4845 | "outputs": [ 4846 | { 4847 | "name": "stdout", 4848 | "output_type": "stream", 4849 | "text": [ 4850 | "[[49 7 4]\n", 4851 | " [35 27 13]\n", 4852 | " [12 5 3]]\n" 4853 | ] 4854 | } 4855 | ], 4856 | "source": [ 4857 | "import numpy as np\n", 4858 | "the_array = np.array([[49, 7, 4], [27, 13, 35], [12, 3, 5]])\n", 4859 | "a_idx = np.argsort(-the_array)\n", 4860 | "sort_array = np.take_along_axis(the_array, a_idx, axis=1)\n", 4861 | "print(sort_array)" 4862 | ] 4863 | }, 4864 | { 4865 | "cell_type": "markdown", 4866 | "metadata": { 4867 | "id": "atNZZKLe1xst" 4868 | }, 4869 | "source": [ 4870 | "### **How to get the indices list of all NaN value in numpy array?**\n" 4871 | ] 4872 | }, 4873 | { 4874 | "cell_type": "markdown", 4875 | "metadata": { 4876 | "id": "S_iTnSSv12fL" 4877 | }, 4878 | "source": [ 4879 | "NaN Values Identifier\n" 4880 | ] 4881 | }, 4882 | { 4883 | "cell_type": "code", 4884 | "execution_count": null, 4885 | "metadata": { 4886 | "colab": { 4887 | "base_uri": "https://localhost:8080/" 4888 | }, 4889 | "id": "O0Dz5Bk215G4", 4890 | "outputId": "c7b79b97-4545-4aad-b4c1-f53ea0e34203" 4891 | }, 4892 | "outputs": [ 4893 | { 4894 | "name": "stdout", 4895 | "output_type": "stream", 4896 | "text": [ 4897 | "[ True False False False]\n" 4898 | ] 4899 | } 4900 | ], 4901 | "source": [ 4902 | "import numpy as np\n", 4903 | "the_array = np.array([np.nan, 2, 3, 4])\n", 4904 | "array_has_nan = np.isnan(the_array)\n", 4905 | "print(array_has_nan)" 4906 | ] 4907 | }, 4908 | { 4909 | "cell_type": "markdown", 4910 | "metadata": { 4911 | "id": "DJexTqho2MJM" 4912 | }, 4913 | "source": [ 4914 | "### **How to count frequency of unique values in a NumPy array in Python?**\n" 4915 | ] 4916 | }, 4917 | { 4918 | "cell_type": "markdown", 4919 | "metadata": { 4920 | "id": "EYHPNwVX2Rad" 4921 | }, 4922 | "source": [ 4923 | "Find number of times a value appears in the list\n" 4924 | ] 4925 | }, 4926 | { 4927 | "cell_type": "code", 4928 | "execution_count": null, 4929 | "metadata": { 4930 | "colab": { 4931 | "base_uri": "https://localhost:8080/" 4932 | }, 4933 | "id": "cMjaD6y22UdE", 4934 | "outputId": "dac01629-944d-4d2d-ff2b-c950d6acf963" 4935 | }, 4936 | "outputs": [ 4937 | { 4938 | "name": "stdout", 4939 | "output_type": "stream", 4940 | "text": [ 4941 | "[[3 1]\n", 4942 | " [4 1]\n", 4943 | " [5 1]\n", 4944 | " [7 2]\n", 4945 | " [9 2]]\n" 4946 | ] 4947 | } 4948 | ], 4949 | "source": [ 4950 | "import numpy as np\n", 4951 | "the_array = np.array([9, 7, 4, 7, 3, 5, 9])\n", 4952 | "frequencies = np.asarray((np.unique(the_array, return_counts=True))).T\n", 4953 | "print(frequencies)" 4954 | ] 4955 | }, 4956 | { 4957 | "cell_type": "markdown", 4958 | "metadata": { 4959 | "id": "uU24bWcq2e8z" 4960 | }, 4961 | "source": [ 4962 | "### **NumPy logspace array example**\n" 4963 | ] 4964 | }, 4965 | { 4966 | "cell_type": "markdown", 4967 | "metadata": { 4968 | "id": "ZUkLuqf42iM2" 4969 | }, 4970 | "source": [ 4971 | "The logspace() function, generates an array with values that are logarithmically spaced between the given start and end values.\n" 4972 | ] 4973 | }, 4974 | { 4975 | "cell_type": "code", 4976 | "execution_count": null, 4977 | "metadata": { 4978 | "colab": { 4979 | "base_uri": "https://localhost:8080/" 4980 | }, 4981 | "id": "YZk6YJ2P2mIw", 4982 | "outputId": "76c1db33-ed3c-418b-969d-0bba17968b2f" 4983 | }, 4984 | "outputs": [ 4985 | { 4986 | "name": "stdout", 4987 | "output_type": "stream", 4988 | "text": [ 4989 | "[ 99999999999999996863366107917975552.000\n", 4990 | " 774263682681124670544724320378779860992.000\n", 4991 | " 5994842503189371886826677967444404233830400.000\n", 4992 | " 46415888336128008167730349234172247696520249344.000\n", 4993 | " 359381366380463281283941836486392749747640633131008.000\n", 4994 | " 2782559402207120232763458068720065504805713505071136768.000\n", 4995 | " 21544346900319042507794853304661524399926133253645094879232.000\n", 4996 | " 166810053720006931335676798952449111386676701152823247869313024.000\n", 4997 | " 1291549665014887989938837143310708849765284993070817403699789299712.000\n", 4998 | " 10000000000000000725314363815292351261583744096465219555182101554790400.000]\n" 4999 | ] 5000 | } 5001 | ], 5002 | "source": [ 5003 | "import numpy as np\n", 5004 | " \n", 5005 | "thearray = np.logspace(5, 10, num=10, base=10000000.0, dtype=float)\n", 5006 | "print(thearray)\n" 5007 | ] 5008 | }, 5009 | { 5010 | "cell_type": "markdown", 5011 | "metadata": { 5012 | "id": "PQSn_snI27Ce" 5013 | }, 5014 | "source": [ 5015 | "### **Joining and Stacking of NumPy arrays**\n" 5016 | ] 5017 | }, 5018 | { 5019 | "cell_type": "markdown", 5020 | "metadata": { 5021 | "id": "VBA2NKRy3op7" 5022 | }, 5023 | "source": [ 5024 | "NumPy uses the concept of stacking and provide a number of functions to perform: vertical stacking(row wise) using vstack(), horizontal stacking(column wise) using hstack() and depth wise stacking(along third axis) using dstack(). The concatenate() function creates a new array by appending arrays after each other, along a given axis. The append() function appends an element to an array and creates a new copy of the array.\n" 5025 | ] 5026 | }, 5027 | { 5028 | "cell_type": "code", 5029 | "execution_count": null, 5030 | "metadata": { 5031 | "colab": { 5032 | "base_uri": "https://localhost:8080/" 5033 | }, 5034 | "id": "BQ3svuVQ3rfu", 5035 | "outputId": "245399bc-f824-4f01-e078-a7cb32cf67d1" 5036 | }, 5037 | "outputs": [ 5038 | { 5039 | "name": "stdout", 5040 | "output_type": "stream", 5041 | "text": [ 5042 | "[[ 1 2 3 7 8 9]\n", 5043 | " [ 4 5 6 10 11 12]]\n", 5044 | "----------\n", 5045 | "[[ 1 2 3]\n", 5046 | " [ 4 5 6]\n", 5047 | " [ 7 8 9]\n", 5048 | " [10 11 12]]\n", 5049 | "----------\n", 5050 | "[[[ 1 7]\n", 5051 | " [ 2 8]\n", 5052 | " [ 3 9]]\n", 5053 | "\n", 5054 | " [[ 4 10]\n", 5055 | " [ 5 11]\n", 5056 | " [ 6 12]]]\n", 5057 | "----------\n", 5058 | "[[ 1 2 3]\n", 5059 | " [ 4 5 6]\n", 5060 | " [ 7 8 9]\n", 5061 | " [10 11 12]]\n", 5062 | "----------\n", 5063 | "[[ 1 2 3]\n", 5064 | " [ 4 5 6]\n", 5065 | " [ 7 8 9]\n", 5066 | " [10 11 12]]\n", 5067 | "----------\n", 5068 | "[[ 1 2 3 7 8 9]\n", 5069 | " [ 4 5 6 10 11 12]]\n" 5070 | ] 5071 | } 5072 | ], 5073 | "source": [ 5074 | "import numpy as np\n", 5075 | " \n", 5076 | "array1 = np.array([[1, 2, 3], [4, 5, 6]])\n", 5077 | "array2 = np.array([[7, 8, 9], [10, 11, 12]])\n", 5078 | " \n", 5079 | "# Stack arrays in sequence horizontally (column wise).\n", 5080 | "arrayH = np.hstack((array1, array2))\n", 5081 | "print(arrayH)\n", 5082 | " \n", 5083 | "print(\"-\" * 10)\n", 5084 | " \n", 5085 | "# Stack arrays in sequence vertically (row wise).\n", 5086 | "arrayV = np.vstack((array1, array2))\n", 5087 | "print(arrayV)\n", 5088 | " \n", 5089 | "print(\"-\" * 10)\n", 5090 | " \n", 5091 | "# Stack arrays in sequence depth wise (along third axis).\n", 5092 | "arrayD = np.dstack((array1, array2))\n", 5093 | "print(arrayD)\n", 5094 | " \n", 5095 | "print(\"-\" * 10)\n", 5096 | " \n", 5097 | "# Appending arrays after each other, along a given axis.\n", 5098 | "arrayC = np.concatenate((array1, array2))\n", 5099 | "print(arrayC)\n", 5100 | " \n", 5101 | "print(\"-\" * 10)\n", 5102 | " \n", 5103 | "# Append values to the end of an array.\n", 5104 | "arrayA = np.append(array1, array2, axis=0)\n", 5105 | "print(arrayA)\n", 5106 | " \n", 5107 | "print(\"-\" * 10)\n", 5108 | "arrayA = np.append(array1, array2, axis=1)\n", 5109 | "print(arrayA)\n" 5110 | ] 5111 | } 5112 | ], 5113 | "metadata": { 5114 | "colab": { 5115 | "name": "python NUMPY.ipynb", 5116 | "provenance": [] 5117 | }, 5118 | "kernelspec": { 5119 | "display_name": "Python 3 (ipykernel)", 5120 | "language": "python", 5121 | "name": "python3" 5122 | }, 5123 | "language_info": { 5124 | "codemirror_mode": { 5125 | "name": "ipython", 5126 | "version": 3 5127 | }, 5128 | "file_extension": ".py", 5129 | "mimetype": "text/x-python", 5130 | "name": "python", 5131 | "nbconvert_exporter": "python", 5132 | "pygments_lexer": "ipython3", 5133 | "version": "3.9.7" 5134 | } 5135 | }, 5136 | "nbformat": 4, 5137 | "nbformat_minor": 1 5138 | } 5139 | --------------------------------------------------------------------------------