|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "c46fd5c5", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Creating Random Arrays with Numpy" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "a2a4149f", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## Introduction" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "id": "4b488198", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "This notebook was created by [Jupyter AI](https://github.com/jupyterlab/jupyter-ai) with the following prompt:\n", |
| 25 | + "\n", |
| 26 | + "> /generate Create a Jupyter notebook that shows how to create a random array using numpy." |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "c6460605", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "This Jupyter notebook demonstrates how to create a random array using numpy. It covers topics such as importing necessary packages, creating a random array, setting the array size and shape, setting the data type of the array, and generating a random array with specified parameters. Each section includes sample code for creating a random array and printing the results. This notebook is useful for anyone looking to generate random arrays in their data analysis or machine learning projects." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "id": "9cfcc84c", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Creating a random array" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 1, |
| 48 | + "id": "4c50ec33", |
| 49 | + "metadata": { |
| 50 | + "tags": [] |
| 51 | + }, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "import numpy as np" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 2, |
| 60 | + "id": "02a9481d", |
| 61 | + "metadata": { |
| 62 | + "tags": [] |
| 63 | + }, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "np.random.seed(123)\n", |
| 67 | + "random_array = np.random.rand(3, 4)" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 3, |
| 73 | + "id": "5ed2a6c8", |
| 74 | + "metadata": { |
| 75 | + "tags": [] |
| 76 | + }, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "Random array:\n", |
| 83 | + " [[0.69646919 0.28613933 0.22685145 0.55131477]\n", |
| 84 | + " [0.71946897 0.42310646 0.9807642 0.68482974]\n", |
| 85 | + " [0.4809319 0.39211752 0.34317802 0.72904971]]\n" |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "source": [ |
| 90 | + "print(\"Random array:\\n\", random_array)" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "id": "2e9d4225", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "## Setting the array size and shape" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 4, |
| 104 | + "id": "15bfe3cb", |
| 105 | + "metadata": { |
| 106 | + "tags": [] |
| 107 | + }, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "import numpy as np" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 5, |
| 116 | + "id": "2aedfee5", |
| 117 | + "metadata": { |
| 118 | + "tags": [] |
| 119 | + }, |
| 120 | + "outputs": [], |
| 121 | + "source": [ |
| 122 | + "# Set the size and shape of the random array\n", |
| 123 | + "array_size = (3, 4) # number of rows and columns" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 6, |
| 129 | + "id": "6dc9a89a", |
| 130 | + "metadata": { |
| 131 | + "tags": [] |
| 132 | + }, |
| 133 | + "outputs": [], |
| 134 | + "source": [ |
| 135 | + "# Create the random array using the specified size and shape\n", |
| 136 | + "random_array = np.random.rand(*array_size) # *array_size unpacks the tuple" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 7, |
| 142 | + "id": "7b4b2ae5", |
| 143 | + "metadata": { |
| 144 | + "tags": [] |
| 145 | + }, |
| 146 | + "outputs": [ |
| 147 | + { |
| 148 | + "name": "stdout", |
| 149 | + "output_type": "stream", |
| 150 | + "text": [ |
| 151 | + "Random array:\n", |
| 152 | + " [[0.43857224 0.0596779 0.39804426 0.73799541]\n", |
| 153 | + " [0.18249173 0.17545176 0.53155137 0.53182759]\n", |
| 154 | + " [0.63440096 0.84943179 0.72445532 0.61102351]]\n" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "# Print the random array\n", |
| 160 | + "print(\"Random array:\\n\", random_array)" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "id": "863dd179", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "## Setting the data type of the array" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 8, |
| 174 | + "id": "fed55a87", |
| 175 | + "metadata": { |
| 176 | + "tags": [] |
| 177 | + }, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "import numpy as np" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 9, |
| 186 | + "id": "d2fa2a10", |
| 187 | + "metadata": { |
| 188 | + "tags": [] |
| 189 | + }, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "# Set the data type of the random array to be created\n", |
| 193 | + "dtype = np.int32" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 10, |
| 199 | + "id": "9c462fdb", |
| 200 | + "metadata": { |
| 201 | + "tags": [] |
| 202 | + }, |
| 203 | + "outputs": [], |
| 204 | + "source": [ |
| 205 | + "# Set the size and shape of the random array\n", |
| 206 | + "array_size = (3, 4) # number of rows and columns" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "code", |
| 211 | + "execution_count": 11, |
| 212 | + "id": "ddcf206e", |
| 213 | + "metadata": { |
| 214 | + "tags": [] |
| 215 | + }, |
| 216 | + "outputs": [], |
| 217 | + "source": [ |
| 218 | + "# Create the random array using the specified size, shape, and data type\n", |
| 219 | + "random_array = np.random.randint(low=0, high=10, size=array_size, dtype=dtype)" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": 12, |
| 225 | + "id": "fcc3d78c", |
| 226 | + "metadata": { |
| 227 | + "tags": [] |
| 228 | + }, |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "name": "stdout", |
| 232 | + "output_type": "stream", |
| 233 | + "text": [ |
| 234 | + "Random array:\n", |
| 235 | + " [[4 6 1 5]\n", |
| 236 | + " [6 2 1 8]\n", |
| 237 | + " [3 5 0 2]]\n" |
| 238 | + ] |
| 239 | + } |
| 240 | + ], |
| 241 | + "source": [ |
| 242 | + "# Print the random array\n", |
| 243 | + "print(\"Random array:\\n\", random_array)" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "markdown", |
| 248 | + "id": "f1c81186", |
| 249 | + "metadata": {}, |
| 250 | + "source": [ |
| 251 | + "## Generating a random array with specified parameters" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": 13, |
| 257 | + "id": "b0526789", |
| 258 | + "metadata": { |
| 259 | + "tags": [] |
| 260 | + }, |
| 261 | + "outputs": [], |
| 262 | + "source": [ |
| 263 | + "import numpy as np" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": 14, |
| 269 | + "id": "9ebd784c", |
| 270 | + "metadata": { |
| 271 | + "tags": [] |
| 272 | + }, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "array_size = (5, 7) \n", |
| 276 | + "min_val = -10\n", |
| 277 | + "max_val = 10" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": 15, |
| 283 | + "id": "56567059", |
| 284 | + "metadata": { |
| 285 | + "tags": [] |
| 286 | + }, |
| 287 | + "outputs": [], |
| 288 | + "source": [ |
| 289 | + "def create_random_array(size, low, high):\n", |
| 290 | + " return np.random.randint(low=low, high=high, size=size)" |
| 291 | + ] |
| 292 | + }, |
| 293 | + { |
| 294 | + "cell_type": "code", |
| 295 | + "execution_count": 16, |
| 296 | + "id": "57c8282d", |
| 297 | + "metadata": { |
| 298 | + "tags": [] |
| 299 | + }, |
| 300 | + "outputs": [], |
| 301 | + "source": [ |
| 302 | + "random_array = create_random_array(array_size, min_val, max_val)" |
| 303 | + ] |
| 304 | + }, |
| 305 | + { |
| 306 | + "cell_type": "code", |
| 307 | + "execution_count": 17, |
| 308 | + "id": "a2c9d87f", |
| 309 | + "metadata": { |
| 310 | + "tags": [] |
| 311 | + }, |
| 312 | + "outputs": [ |
| 313 | + { |
| 314 | + "name": "stdout", |
| 315 | + "output_type": "stream", |
| 316 | + "text": [ |
| 317 | + "Random array:\n", |
| 318 | + " [[ 0 3 8 -6 5 1 2]\n", |
| 319 | + " [-4 3 9 6 -4 4 -3]\n", |
| 320 | + " [ 1 -3 -9 1 -5 8 7]\n", |
| 321 | + " [ 2 8 7 -9 9 2 -1]\n", |
| 322 | + " [ 6 7 -7 -7 1 -3 -1]]\n" |
| 323 | + ] |
| 324 | + } |
| 325 | + ], |
| 326 | + "source": [ |
| 327 | + "print(\"Random array:\\n\", random_array)" |
| 328 | + ] |
| 329 | + }, |
| 330 | + { |
| 331 | + "cell_type": "code", |
| 332 | + "execution_count": null, |
| 333 | + "id": "0abd2b89-c2e1-4083-9d4a-29da5a2096c3", |
| 334 | + "metadata": {}, |
| 335 | + "outputs": [], |
| 336 | + "source": [] |
| 337 | + } |
| 338 | + ], |
| 339 | + "metadata": { |
| 340 | + "kernelspec": { |
| 341 | + "display_name": "Python 3 (ipykernel)", |
| 342 | + "language": "python", |
| 343 | + "name": "python3" |
| 344 | + }, |
| 345 | + "language_info": { |
| 346 | + "codemirror_mode": { |
| 347 | + "name": "ipython", |
| 348 | + "version": 3 |
| 349 | + }, |
| 350 | + "file_extension": ".py", |
| 351 | + "mimetype": "text/x-python", |
| 352 | + "name": "python", |
| 353 | + "nbconvert_exporter": "python", |
| 354 | + "pygments_lexer": "ipython3", |
| 355 | + "version": "3.9.16" |
| 356 | + } |
| 357 | + }, |
| 358 | + "nbformat": 4, |
| 359 | + "nbformat_minor": 5 |
| 360 | +} |
0 commit comments