Skip to main content

Documentation Index

Fetch the complete documentation index at: https://ai.tharung.in/llms.txt

Use this file to discover all available pages before exploring further.

What is NumPy?

NumPy stands for Numerical Python. It is an open-source Python library used for numerical computing and handling arrays efficiently.

Why NumPy?

  • Faster than Python lists
  • Memory efficient
  • Supports mathematical operations directly
  • Widely used in:
    • Data Science
    • Machine Learning
    • AI
    • Scientific Computing

Difference Between Python Lists and NumPy Arrays

FeaturePython ListNumPy Array
SpeedSlowerFaster
Memory UsageMoreLess
Data TypesMixed allowedSame datatype
Mathematical OperationsRequires loopsVectorized operations
PerformancePython-basedBuilt on C

Installing and Importing NumPy

Installation

pip install numpy

Importing

import numpy as np
np is an alias for NumPy.

Dimensions and Shapes in NumPy

Dimensions

  • 0D → Scalar
  • 1D → Single row/line
  • 2D → Rows and columns

Example

import numpy as np

arr = np.array([1,2,4])
print(arr)

Checking Dimensions

arr.ndim

Scalar Array

arr2 = np.array(34)
arr2.ndim

2D Array

arr3 = np.array([[1,3,4],[2,34,5]])

print(arr3)
arr3.ndim

Shape

arr3.shape
Output:
(2, 3)
Meaning:
  • 2 rows
  • 3 columns

Creating NumPy Arrays

Using arange()

range = np.arange(1,10,2)
print(range)

Syntax

np.arange(start, stop, step)
  • Start included
  • Stop excluded

Using linspace()

arr4 = np.linspace(0,1,5)
print(arr4)
Creates evenly spaced numbers.

Using logspace()

arr5 = np.logspace(1,3,3)
print(arr5)
Output:
[10. 100. 1000.]

Arrays Filled with Values

Zeros

arr6 = np.zeros(4)
print(arr6)

2D Zeros

arr6 = np.zeros([3,2])
print(arr6)

Ones

arr7 = np.ones([3,2])
print(arr7)

Full

arr8 = np.full(10,2)
arr8

2D Full Array

arr9 = np.full([2,4],5)
arr9

Empty Array

arr11 = np.empty([2,3])
arr11
  • Faster than zeros
  • Contains garbage values
  • Use only if values will be overwritten

Random Arrays in NumPy

Random Uniform Distribution

np.random.rand(10)

2D Random Array

arr12 = np.random.rand(2,3)
arr12
Values between:
0 and 1

Random Normal Distribution

arr13 = np.random.randn(2,3)
arr13
  • Mean = 0
  • Standard deviation = 1

Random Integers

arr14 = np.random.randint(10,100, size=10)
print(arr14)

2D Random Integers

arr14 = np.random.randint(10,100, size=[2,3])
print(arr14)

NumPy Data Types and Type Casting

Integer Array

arr = np.array([1,3,45,3,4])

print(arr)
type(arr)

Automatic Type Conversion

arr = np.array([1.1,3,45,3,4])

print(arr)
type(arr)
If one value is float, entire array becomes float.

Defining Datatype Manually

arr = np.array([1.3,3,45,3,4], dtype=int)

arr.dtype

Checking Datatypes

arr.dtype

Type Casting Using astype()

arr = np.array([1,23,4])

new_arr = arr.astype(np.float64)

new_arr
new_arr.dtype
Converts datatype without modifying original array.

Type Casting Errors

arr=["1",'2',"hello"]

arr2 = arr.astype(np.int64)
arr2
This causes error because:
"hello"
cannot be converted to integer.

Important Array Attributes

arr  = np.array([[1,2,4],
                [4,5,6]])

Datatype

print(arr.dtype)

Dimensions

print(arr.ndim)

Shape

print(arr.shape)

Size

print(arr.size)
Total elements:
rows × columns

Item Size

print(arr.itemsize)
Returns memory size of one element in bytes.

Array Reshaping

Using reshape()

arr = np.array([1,2,3,4,5,6])

reshaped = arr.reshape(2,3)

print(reshaped)

Reshape Again

reshape2 = reshaped.reshape(3,2)

print(reshape2)
Changes layout without changing data.

Ravel vs Flatten


Ravel

Converts array into 1D.
ravel = reshape2.ravel()

ravel

Important

ravel() returns a view. Changes affect original array.
ravel[0] = 100

print(ravel)
print(reshape2)

Flatten

flat = reshape2.flatten()

flat

Important

flatten() returns a copy. Original array remains unchanged.
flat[0] = 200

print(flat)
print(reshape2)

Difference Between Ravel and Flatten

FeatureRavelFlatten
ReturnsViewCopy
Memory EfficientYesNo
Changes Affect OriginalYesNo
SpeedFasterSlightly slower