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
| Feature | Python List | NumPy Array |
|---|---|---|
| Speed | Slower | Faster |
| Memory Usage | More | Less |
| Data Types | Mixed allowed | Same datatype |
| Mathematical Operations | Requires loops | Vectorized operations |
| Performance | Python-based | Built on C |
Installing and Importing NumPy
Installation
Importing
np is an alias for NumPy.
Dimensions and Shapes in NumPy
Dimensions
- 0D → Scalar
- 1D → Single row/line
- 2D → Rows and columns
Example
Checking Dimensions
Scalar Array
2D Array
Shape
- 2 rows
- 3 columns
Creating NumPy Arrays
Using arange()
Syntax
- Start included
- Stop excluded
Using linspace()
Using logspace()
Arrays Filled with Values
Zeros
2D Zeros
Ones
Full
2D Full Array
Empty Array
- Faster than zeros
- Contains garbage values
- Use only if values will be overwritten
Random Arrays in NumPy
Random Uniform Distribution
2D Random Array
Random Normal Distribution
- Mean = 0
- Standard deviation = 1
Random Integers
2D Random Integers
NumPy Data Types and Type Casting
Integer Array
Automatic Type Conversion
Defining Datatype Manually
Checking Datatypes
Type Casting Using astype()
Type Casting Errors
Important Array Attributes
Datatype
Dimensions
Shape
Size
Item Size
Array Reshaping
Using reshape()
Reshape Again
Ravel vs Flatten
Ravel
Converts array into 1D.Important
ravel() returns a view.
Changes affect original array.
Flatten
Important
flatten() returns a copy.
Original array remains unchanged.
Difference Between Ravel and Flatten
| Feature | Ravel | Flatten |
|---|---|---|
| Returns | View | Copy |
| Memory Efficient | Yes | No |
| Changes Affect Original | Yes | No |
| Speed | Faster | Slightly slower |