0% found this document useful (0 votes)
13 views14 pages

Intro to NumPy: Basics and Arrays

This document is an introductory lecture on NumPy, a Python library for numerical computation, covering its features like ND-arrays, vectorized operations, and linear algebra utilities. It includes sections on importing NumPy, creating arrays using various methods, and understanding array attributes and differences between arrays and lists. The lecture serves as a foundational guide for applications in data science, machine learning, and other scientific computing fields.

Uploaded by

upadhyayyash535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

Intro to NumPy: Basics and Arrays

This document is an introductory lecture on NumPy, a Python library for numerical computation, covering its features like ND-arrays, vectorized operations, and linear algebra utilities. It includes sections on importing NumPy, creating arrays using various methods, and understanding array attributes and differences between arrays and lists. The lecture serves as a foundational guide for applications in data science, machine learning, and other scientific computing fields.

Uploaded by

upadhyayyash535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

{

"cells": [
{
"cell_type": "markdown",
"id": "37f11565",
"metadata": {},
"source": [
"# Lecture 1 — Introduction to NumPy\n",
"This notebook covers the basics of NumPy: array creation, properties, and
simple operations."
]
},
{
"cell_type": "markdown",
"id": "e4a2b782-17f9-471e-a972-e9f5b8c48b47",
"metadata": {},
"source": [
"## 0. What is Numpy?"
]
},
{
"cell_type": "markdown",
"id": "3538591d-2f83-4bf1-846a-86ef7732e583",
"metadata": {},
"source": [
"NumPy (Numerical Python) is a Python library designed for fast numerical
computation.\n",
"It provides:\n",
"1. ND-arrays → powerful, typed, fixed-size arrays\n",
"2. Vectorised operations → operations applied to whole arrays at once\n",
"3. Linear algebra utilities\n",
"4. Random number generation\n",
"5. Tools for manipulating matrices, tensors, and numerical data\n",
"\n",
"NumPy is the foundation for scientific computing in Python and is used in:\n",
"1. Data science\n",
"2. Machine learning\n",
"3. Physics simulations\n",
"4. Image processing\n",
"5. Finance\n",
"6. Signal processing"
]
},
{
"cell_type": "markdown",
"id": "4d0787af",
"metadata": {},
"source": [
"## 1. Importing NumPy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7df77238-ab8b-48a4-b0ce-7b33d45131c3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: numpy in c:\\users\\anany\\anaconda3\\lib\\
site-packages (1.26.4)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"[notice] A new release of pip is available: 25.0.1 -> 25.3\n",
"[notice] To update, run: [Link] -m pip install --upgrade pip\n"
]
}
],
"source": [
"# install numpy\n",
"!pip install numpy"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "892f2c61",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.26.4'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"np.__version__"
]
},
{
"cell_type": "markdown",
"id": "76a14f6e",
"metadata": {},
"source": [
"## 2. Creating Arrays\n",
"### Using `[Link]`"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1aa07dc8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar1 = [Link]([1, 2, 3])\n",
"ar1"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2705dba7-73fb-4588-9d96-9d0d6165db5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar2=[Link]([[1,2,3],[4,5,6]])\n",
"ar2"
]
},
{
"cell_type": "markdown",
"id": "a2a49000-0cbe-44a8-ad57-2e2af1fc0def",
"metadata": {},
"source": [
"### Using Ranges : `[Link]`, `[Link]`"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1f251a98-7ac2-4e9f-9900-87e6e60ca192",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 2, 4, 6, 8])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar3 = [Link](0, 10, 2)\n",
"ar3"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3cf22c41-011e-40b8-b533-8c2ab2dd5fde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 0.25, 0.5 , 0.75, 1. ])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar4 = [Link](0, 1, 5)\n",
"ar4"
]
},
{
"cell_type": "markdown",
"id": "f8622abe-9eee-4e91-8eb1-8e3f33ef549b",
"metadata": {},
"source": [
"### Q. How can we use `[Link]` or `[Link]` to create a 2-dimensional
array?"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "8b1bb92a-0372-40ce-8caa-7e8448f08169",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8],\n",
" [ 9, 10, 11, 12]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar5 = [Link](1,13).reshape(3,4)\n",
"ar5"
]
},
{
"cell_type": "markdown",
"id": "55a263cb-cc92-4e95-b0c2-d70f5af2cbf3",
"metadata": {},
"source": [
"### Constant Matrices: `[Link]`, `[Link]`,`[Link]`"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e3a784e4-b4f0-42b5-8e3a-5177a6540c84",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0.],\n",
" [0., 0., 0.]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar6 = [Link]((2, 3))\n",
"ar6"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "efa20f75-48ae-4d23-981e-a8289f12b11e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar7 = [Link]((3, 3))\n",
"ar7"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "6c4f32f2-7cc6-48a8-8780-9f575f8afe18",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 2, 2, 2],\n",
" [2, 2, 2, 2],\n",
" [2, 2, 2, 2]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar8 = [Link]((3,4),2)\n",
"ar8"
]
},
{
"cell_type": "markdown",
"id": "83169eee-ec70-49af-bf5b-a8031e81a8dd",
"metadata": {},
"source": [
"### Diagonal Matrices: `[Link]`,`[Link]`"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "b38dd8c4-a37f-43ed-b0b8-c38768f79054",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0, 0],\n",
" [0, 2, 0],\n",
" [0, 0, 3]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar9 = [Link]([1,2,3])\n",
"ar9"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "b15da746-2934-4362-9fce-b711013dcc8c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0.],\n",
" [0., 1., 0.],\n",
" [0., 0., 1.]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar10 = [Link](3)\n",
"ar10"
]
},
{
"cell_type": "markdown",
"id": "23b5f829-a980-43ff-a69b-29388f3fa826",
"metadata": {},
"source": [
"### Random Values: `[Link]`"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "eb65bac3-f95a-4b73-812c-f7d6038b4832",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.96048417, 0.55922243, 0.56404585, 0.56890757],\n",
" [0.6575177 , 0.04949507, 0.64259727, 0.96954384],\n",
" [0.2166371 , 0.64701256, 0.92923656, 0.4649236 ]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar11 = [Link](3,4)\n",
"ar11"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "fcbc48f3-3caf-4fa5-a165-a8c3f88140e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 8, 33, 18, 98],\n",
" [90, 96, 62, 12],\n",
" [27, 44, 83, 48]])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar12 = [Link](1,100,size=(3,4))\n",
"ar12"
]
},
{
"cell_type": "markdown",
"id": "9d1f456a-ac76-4382-9ffc-9b8678bf4222",
"metadata": {},
"source": [
"## 3. Array Attributes"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d3d76d3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((2, 3), 2, 6, dtype('int32'))"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = [Link]([[1,2,3],[4,5,6]])\n",
"\n",
"[Link], [Link], [Link], [Link]"
]
},
{
"cell_type": "markdown",
"id": "17b80fbe-fe01-427f-bf3e-7d4e808e735d",
"metadata": {},
"source": [
"## 4. Arrays vs Lists"
]
},
{
"cell_type": "markdown",
"id": "2bbae9a9-c707-4f6d-947e-ab63b9d69e0d",
"metadata": {},
"source": [
"### Better for Mathematical/Vectorized Operations"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4fb19799-2404-4d4f-8d6c-42b2a7eb88ed",
"metadata": {},
"outputs": [],
"source": [
"#Add elements of two lists componentwise\n",
"\n",
"list1=[1,2,3,4]\n",
"list2=[8,7,6,5]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "1b3840f1-a165-4518-991f-4c3137c12104",
"metadata": {},
"outputs": [],
"source": [
"#Add elements of two arrays componentwise\n",
"ar1=[Link]([1,2,3,4])\n",
"ar2=[Link]([8,7,6,5])"
]
},
{
"cell_type": "markdown",
"id": "5f655b52-b8b4-4444-bc6e-358cb02453ae",
"metadata": {},
"source": [
"### Broadcasting (Automatic Shape Expansion)"
]
},
{
"cell_type": "markdown",
"id": "dc7d571b-9599-4d04-91ab-a5cfe01acc66",
"metadata": {},
"source": [
"Broadcasting allows NumPy to apply operations between arrays of different
shapes without creating copies."
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "600fb7f3-0b0f-45f9-8e78-581a498b7af1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 8 2 5 16]\n",
" [ 4 6 14 12]\n",
" [ 5 16 16 11]]\n"
]
}
],
"source": [
"ar4 = [Link](1,20,size=(3, 4)) \n",
"print(ar4)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "82872a94-34f3-4db8-bc59-52437248f554",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[13, 7, 10, 21],\n",
" [ 9, 11, 19, 17],\n",
" [10, 21, 21, 16]])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar4+5"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "621ccf2e-2f46-42e3-b98e-bd5476a10fbf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[7],\n",
" [6],\n",
" [8]])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar5=[Link](1,10,size=(3,1))\n",
"ar5"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "04bd930a-632d-47e4-bb35-f625359950b8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[15, 9, 12, 23],\n",
" [10, 12, 20, 18],\n",
" [13, 24, 24, 19]])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar4+ar5"
]
},
{
"cell_type": "markdown",
"id": "b9f01506-493e-4aec-8193-2910b1a6aaed",
"metadata": {},
"source": [
"### Contiguous Memory (Efficient Storage)"
]
},
{
"cell_type": "markdown",
"id": "78893b00-7480-472a-8bd1-89478a8e6a09",
"metadata": {},
"source": [
"Python lists store values separately in memory, and each element is a full
Python object.\n",
"This means:\n",
"1. Elements are scattered in memory\n",
"2. Store pointers to Python objects rather than values itself\n",
"3. Accessing them requires pointer chasing → slow\n",
"\n",
"NumPy arrays store data in a single, continuous block of memory using a fixed
datatype (int32, float64, etc.)."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "a6f47e4d-09b8-47e1-bfbc-f1c3ae563964",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"140728008846040\n",
"2404473928944\n",
"140728008845720\n",
"2404617468480\n"
]
}
],
"source": [
"list3=[10,'python',0,[1,2]]\n",
"for i in list3:\n",
" print(id(i))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "4de25a72-fd9f-4f3b-87db-5bea9e45fb97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2404471720016, False)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar3=[Link]([1,2,3,4])\n",
"ar3.__array_interface__['data']"
]
},
{
"cell_type": "markdown",
"id": "3f9edcf0",
"metadata": {},
"source": [
"## Practice Problems\n",
"Try solving these on your own below."
]
},
{
"cell_type": "markdown",
"id": "ca7e7162",
"metadata": {},
"source": [
"### Problem 1: Create a 1D array of numbers from 10 to 50."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ecd484a",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here\n"
]
},
{
"cell_type": "markdown",
"id": "4768e010",
"metadata": {},
"source": [
"### Problem 2: Create a 3×3 matrix of zeros."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76fda98d",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here\n"
]
},
{
"cell_type": "markdown",
"id": "f291548e",
"metadata": {},
"source": [
"### Problem 3: Create an array of 20 equally spaced numbers between (and not
including) 0 and 1."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "528bdc7e",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here\n"
]
},
{
"cell_type": "markdown",
"id": "2d9b3251",
"metadata": {},
"source": [
"### Problem 4: Given `a = [Link]([1, 2, 3])`, print shape, ndim and dtype."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a30dbfe1",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here\n",
"a = [Link]([1, 2, 3])\n"
]
},
{
"cell_type": "markdown",
"id": "663eeded",
"metadata": {},
"source": [
"### Problem 5: Convert a Python list of lists into a NumPy array and print its
shape."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37806af0",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here\n"
]
},
{
"cell_type": "markdown",
"id": "968ab530-a623-4808-9442-09fd58a64fc2",
"metadata": {},
"source": [
"### Problem 6: Use `[Link]` to create a 5x3 array, with numbers starting
from 100."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "b7e152f0-3f27-4c56-a40f-a3aebdb59026",
"metadata": {},
"outputs": [],
"source": [
"# Your solution here"
]
}
],
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like