{ "cells": [ { "cell_type": "markdown", "id": "9b01ce99", "metadata": {}, "source": [ "# Creating Random Arrays with Numpy" ] }, { "cell_type": "markdown", "id": "3b359024", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "id": "47be2c26", "metadata": {}, "source": [ "This notebook was created by [Jupyter AI](https://github.com/jupyterlab/jupyter-ai) with the following prompt:\n", "\n", "> /generate Create a Jupyter notebook that shows how to create a random array using numpy." ] }, { "cell_type": "markdown", "id": "e07ea6ff", "metadata": {}, "source": [ "This Jupyter notebook demonstrates how to create a random array using the numpy library. The necessary libraries are imported and a random array is generated using the specified dimensions. The random seed is set to ensure reproducibility, and the generated random array is displayed." ] }, { "cell_type": "markdown", "id": "580a72ac", "metadata": {}, "source": [ "## Generating a random array" ] }, { "cell_type": "code", "execution_count": 1, "id": "9c2c0e55", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "ea8b1850", "metadata": {}, "outputs": [], "source": [ "# Set the seed for reproducibility\n", "np.random.seed(0)" ] }, { "cell_type": "code", "execution_count": 3, "id": "2cfc81f2", "metadata": {}, "outputs": [], "source": [ "# Generate a random array of shape (3, 4) with values between 0 and 1\n", "random_array = np.random.rand(3, 4)" ] }, { "cell_type": "code", "execution_count": 4, "id": "f023ef21", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", " [0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", " [0.96366276, 0.38344152, 0.79172504, 0.52889492]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Display the random array\n", "random_array" ] }, { "cell_type": "markdown", "id": "1c5aa839", "metadata": {}, "source": [ "## Specifying the array dimensions" ] }, { "cell_type": "code", "execution_count": 5, "id": "39e02490", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 6, "id": "6e04320e", "metadata": {}, "outputs": [], "source": [ "# Specify the dimensions of the random array\n", "rows = 5\n", "columns = 3" ] }, { "cell_type": "code", "execution_count": 7, "id": "de911781", "metadata": {}, "outputs": [], "source": [ "# Create a random array with the specified dimensions\n", "random_array = np.random.rand(rows, columns)" ] }, { "cell_type": "code", "execution_count": 8, "id": "d0230c69", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.56804456, 0.92559664, 0.07103606],\n", " [0.0871293 , 0.0202184 , 0.83261985],\n", " [0.77815675, 0.87001215, 0.97861834],\n", " [0.79915856, 0.46147936, 0.78052918],\n", " [0.11827443, 0.63992102, 0.14335329]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Return the random array\n", "random_array" ] }, { "cell_type": "markdown", "id": "c8926d56", "metadata": {}, "source": [ "## Setting the random seed" ] }, { "cell_type": "code", "execution_count": 9, "id": "5b1e7ef8", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(42)" ] }, { "cell_type": "markdown", "id": "6439245f", "metadata": {}, "source": [ "## Creating the random array" ] }, { "cell_type": "code", "execution_count": 10, "id": "ea1612ad", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 11, "id": "3e3f0238", "metadata": {}, "outputs": [], "source": [ "# Set the dimensions of the random array\n", "rows = 5\n", "cols = 3" ] }, { "cell_type": "code", "execution_count": 12, "id": "aadd67e5", "metadata": {}, "outputs": [], "source": [ "# Create the random array\n", "random_array = np.random.rand(rows, cols)" ] }, { "cell_type": "code", "execution_count": 13, "id": "e697b0ef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.37454012, 0.95071431, 0.73199394],\n", " [0.59865848, 0.15601864, 0.15599452],\n", " [0.05808361, 0.86617615, 0.60111501],\n", " [0.70807258, 0.02058449, 0.96990985],\n", " [0.83244264, 0.21233911, 0.18182497]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Return the random array\n", "random_array" ] }, { "cell_type": "markdown", "id": "e0cb34ee", "metadata": {}, "source": [ "## Displaying the random array" ] }, { "cell_type": "code", "execution_count": 14, "id": "dccf8357", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 15, "id": "5f80dcbb", "metadata": {}, "outputs": [], "source": [ "random_array = np.random.random((3, 4))" ] }, { "cell_type": "code", "execution_count": 16, "id": "4d4923b3", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "array([[0.18340451, 0.30424224, 0.52475643, 0.43194502],\n", " [0.29122914, 0.61185289, 0.13949386, 0.29214465],\n", " [0.36636184, 0.45606998, 0.78517596, 0.19967378]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random_array" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }