0% found this document useful (0 votes)
8 views4 pages

NumPy Arrays

NumPy, or Numerical Python, is a foundational library for numerical computations in Python, providing a high-performance N-dimensional array object called ndarray. It offers advantages such as faster execution, less memory usage, and support for vectorized operations, making it ideal for numerical calculations, data analysis, and machine learning. Key features include array creation methods, built-in mathematical functions, and important attributes like dimensions, shape, and data type.

Uploaded by

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

NumPy Arrays

NumPy, or Numerical Python, is a foundational library for numerical computations in Python, providing a high-performance N-dimensional array object called ndarray. It offers advantages such as faster execution, less memory usage, and support for vectorized operations, making it ideal for numerical calculations, data analysis, and machine learning. Key features include array creation methods, built-in mathematical functions, and important attributes like dimensions, shape, and data type.

Uploaded by

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

What is NumPy?

 NumPy stands for Numerical Python.


 It is the foundation library for numerical computations in Python.
 Provides a high-performance N-dimensional array object called ndarray.
 Used for:
o Numerical calculations
o Working with large datasets
o Linear algebra
o Statistics
o Data preprocessing (used in Pandas, ML, AI, Data Science)

🔹 Why NumPy? – Advantages


✔ Faster execution
 NumPy arrays are stored in contiguous memory blocks, making them faster than Python lists.
✔ Less memory
 Stores data of the same type → consumes less memory.
✔ Supports vectorized operations
 Operations can be done on entire arrays without loops
Example:
 c = a + b # vectorized
✔ Built-in mathematical functions
 sum(), mean(), sqrt(), std(), etc.
✔ Integration
 Works with Pandas, Matplotlib, Scikit-learn, TensorFlow, etc.

⭐ Importing NumPy
import numpy as np
 np is the standard alias used universally.

⭐ Creating NumPy Arrays from Python Lists


✔ 1. Using a Python List
import numpy as np
a = [Link]([1, 2, 3, 4])
→ Creates a 1-D array
✔ 2. Using a List of Lists (Nested List)
b = [Link]([[1, 2, 3],
[4, 5, 6]])
→ Creates a 2-D array
✔ 3. Mixed list types
NumPy automatically converts elements to a common data type:
c = [Link]([1, 2.5, 7])
Result: float array

⭐ Other Array Creation Methods


✔ [Link]()
[Link]((3,4)) # 3x4 array of zeros
✔ [Link]()
[Link]((2,2))
✔ [Link]()
Like Python range() but returns a NumPy array.
1
[Link](1, 11, 2) # 1 to 10 step 2
✔ [Link]()
Generates numbers with equal spacing.
[Link](0, 1, 5) # 5 values 0 to 1
✔ [Link]()
Identity matrix:
[Link](4)

⭐ Data Types (dtypes)


NumPy arrays have a single fixed datatype such as:
 int32, int64
 float32, float64
 bool
 str (unicode)

⭐ Important Attributes of NumPy Arrays


Let arr be a NumPy array:
arr = [Link]([[1,2,3],
[4,5,6]])

🔹 1. ndim — Number of Dimensions


[Link] # Output: 2

🔹 2. shape — Dimensions of Array (Rows, Columns)


[Link] # Output: (2, 3)

🔹 3. size — Total Number of Elements


[Link] # Output: 6

🔹 4. dtype — Data Type of Elements


[Link]

🔹 5. itemsize — Memory (bytes) per element


[Link] # e.g., 8 bytes for float64

🔹 6. nbytes — Total Memory Occupied by Array


[Link] # itemsize * size

🔹 7. T — Transpose (Rows ↔ Columns)


arr.T

🔹 8. [Link] — 1-D iterator


for x in [Link]:
print(x)

⭐ Basic Operations
✔ Element-wise arithmetic
a+b
a-b
a*b
2
a/b
✔ Mathematical functions
 [Link](), [Link](),[Link](),[Link]()
 [Link](),[Link](),[Link]()
 [Link]()
 [Link](), [Link]()
 [Link]()
 [Link]()
 [Link]()

⭐ Indexing and Slicing


✔ 1-D arrays
a[0], a[-1]
a[1:4]
✔ 2-D arrays
arr[0, 1] # row 0 column 1
arr[:, 1] # entire column 1
arr[1, :] # entire row 1

⭐ Reshaping Arrays
[Link](3,2)

⭐ Summary (Last-minute revision)


✔ NumPy = Numerical Python
✔ ndarray is the core object
✔ Faster, memory efficient, vectorized operations
✔ Create arrays from lists using [Link]()
✔ Key attributes: ndim, shape, size, dtype, itemsize, nbytes, T
✔ Fast mathematical functions available
✔ Supports indexing, slicing, reshaping, broadcasting

⭐ Difference Between NumPy Arrays and Python Lists


Feature NumPy Array Python List
Stores homogeneous data (all elements same Stores heterogeneous data (mixed types
1. Data Type
type). allowed).
Much faster (uses C-optimized code + contiguous
2. Speed Slower, especially for large data.
memory).
Requires less memory because of fixed data Uses more memory because each
3. Memory Usage
type. element is a Python object.
Supports vectorized operations (element-wise Does not support vectorized operations;
4. Operations Support
operations without loops). needs explicit loops.
5. Mathematical Has built-in functions like sum(), mean(), sqrt(), No built-in mathematical operations for
Functions std(). whole lists.
6. Performance on Not suitable for heavy numerical
Highly optimized for large numerical datasets.
Large Data computations.
7. Reshaping Supports reshaping (e.g., 1D → 2D). Cannot reshape.
3
Feature NumPy Array Python List
Supports broadcasting, i.e., operations between
8. Broadcasting No broadcasting support.
arrays of different shapes.
Stored in contiguous memory, improving Elements are stored separately in
9. Memory Layout
caching. memory.
10. Convenience Provides array creation methods like zeros(),
Lists must be manually created and filled.
Functions ones(), arange(), linspace().
Numerical computing, data analysis, ML, General-purpose storage of mixed data
11. Suitable For
scientific applications. types.

You might also like