NumPy Introduction

๐Ÿง  NumPy Introduction

NumPy stands for Numerical Python and is a popular open-source Python library used for:

โœ” Scientific Computing
โœ” Working with arrays
โœ” Mathematical operations
โœ” Data analysis
โœ” Machine learning and AI
โœ” Linear algebra & Fourier transform
โœ” Working with large datasets efficiently

NumPy is the foundation of libraries like Pandas, SciPy, TensorFlow, and Scikit-Learn.


๐Ÿ”ฅ Why NumPy?

Pythonโ€™s built-in lists are slow and inefficient for numeric processing. NumPy solves this by offering:

Feature Python List NumPy Array
Speed โŒ Slow โœ… Very Fast
Memory Usage โŒ High โœ… Low
Supports Vectorized Operations โŒ No โœ… Yes
Suitable for ML/Data Science โŒ No โœ… Yes

๐Ÿ“ฆ Installing NumPy

If not installed, use:

Or for Anaconda users:


๐Ÿงฉ Importing NumPy

Standard practice:

import numpy as np

๐Ÿ“Œ NumPy Array

NumPy works with a special data type called ndarray (n-dimensional array).

๐Ÿ‘‰ Creating an array:

import numpy as np

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

Output:

[1 2 3 4 5]
<class 'numpy.ndarray'>

๐ŸŽฏ Multidimensional Array

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

Output:

[[1 2 3]
[4 5 6]]


๐Ÿ“ Array Properties

print(arr.ndim) # dimensions
print(arr.size) # number of elements
print(arr.shape) # rows and columns

โš™ Creating Special Arrays

np.zeros((2,3)) # 2x3 matrix of 0s
np.ones((3,3)) # 3x3 of 1s
np.arange(1,10) # 1 to 9
np.linspace(0,1,5) # evenly spaced values

๐Ÿ”ฅ Fast Vectorized Operations

a = np.array([1,2,3])
b = np.array([4,5,6])
print(a + b)
print(a * b)
print(a ** 2)

โž— Mathematical Functions

np.sqrt(a)
np.mean(a)
np.sum(a)
np.max(a)
np.min(a)

๐Ÿ”„ Array Indexing & Slicing

arr = np.array([10,20,30,40,50])

print(arr[0]) # first element
print(arr[1:4]) # slicing


๐Ÿงฎ Reshaping Arrays

arr = np.arange(1,10)
new = arr.reshape(3,3)
print(new)

๐Ÿ“Š Conclusion

NumPy is a powerful and essential library for:

  • Data Science

  • Machine Learning

  • Deep Learning

  • Scientific and mathematical calculations

It makes handling numeric data faster, easier, and more memory-efficient.

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *