DEV Community

Cover image for NumPy in Python
likhitha manikonda
likhitha manikonda

Posted on

NumPy in Python

If you're new to Python and want to learn how to work with numbers and data efficiently, NumPy is your best friend. This guide will walk you through the basics of NumPy in the simplest way possibleโ€”with clear explanations and code examples.


๐Ÿ“Œ What is NumPy?

NumPy stands for Numerical Python. Itโ€™s a Python library that makes it easy to work with arrays (like lists of numbers) and perform mathematical operations on them quickly and efficiently.

๐Ÿ› ๏ธ How to Install NumPy

Before using NumPy, you need to install it. Open your terminal or command prompt and type:

pip install numpy
Enter fullscreen mode Exit fullscreen mode

If you're using Anaconda (a Python distribution), you can use:

conda install numpy
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฑ Step-by-Step Concepts with Examples

1. Importing NumPy

import numpy as np
Enter fullscreen mode Exit fullscreen mode

โœ… This line tells Python to load the NumPy library and give it a nickname np so we can use it easily later.


2. Creating Arrays

import numpy as np

# Creating a 1D array (like a list)
arr1 = np.array([1, 2, 3])
print(arr1)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This creates a NumPy array with the numbers 1, 2, and 3. Arrays are like lists but more powerful for math operations.


# Creating a 2D array (like a table)
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This is a 2D array with 2 rows and 3 columns. Think of it like a spreadsheet.


# Arrays filled with zeros
zeros = np.zeros((2, 3))
print(zeros)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This creates a 2-row, 3-column array filled with zeros.


# Arrays filled with ones
ones = np.ones((3, 2))
print(ones)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This creates a 3-row, 2-column array filled with ones.


# Array with a range of numbers
range_arr = np.arange(0, 10, 2)
print(range_arr)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This creates an array starting from 0 to 10 (not including 10), with steps of 2. So the output will be [0 2 4 6 8].


3. Array Properties

print(arr2.shape)  # (2, 3)
print(arr2.ndim)   # 2
print(arr2.dtype)  # int64
print(arr2.size)   # 6
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation:

  • shape: tells you the size (rows, columns).
  • ndim: number of dimensions (1D, 2D, etc.).
  • dtype: data type of elements (e.g., integers).
  • size: total number of elements in the array.

4. Indexing and Slicing

print(arr2[0, 1])  # Output: 2
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This gets the value in the 1st row and 2nd column (remember, Python starts counting from 0).


print(arr2[:, 1])  # Output: [2 5]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This gets the 2nd column from all rows.


print(arr2[1])  # Output: [4 5 6]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This gets the entire 2nd row.


5. Math with Arrays

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

print(a + b)  # Output: [5 7 9]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: Adds each element from a and b together: 1+4, 2+5, 3+6.


print(a * 2)  # Output: [2 4 6]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: Multiplies each element in a by 2.


print(np.dot(a, b))  # Output: 32
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: This is the dot product: 1*4 + 2*5 + 3*6 = 32.


6. Useful Functions

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

print(np.sum(arr))       # Output: 10
print(np.mean(arr))      # Output: 2.5
print(np.max(arr))       # Output: 4
print(np.min(arr))       # Output: 1
print(np.std(arr))       # Output: 1.118...
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation:

  • sum: adds all numbers.
  • mean: average.
  • max: largest number.
  • min: smallest number.
  • std: standard deviation (how spread out the numbers are).

7. Reshaping Arrays

arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: arange(12) creates an array from 0 to 11 as [0,1,2,3,4,5,6,7,8,9,10]. reshape(3, 4) turns it into a 3-row, 4-column array.

Output:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Enter fullscreen mode Exit fullscreen mode

8. Random Numbers

rand_arr = np.random.rand(2, 3)
print(rand_arr)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Explanation: Creates a 2x3 array with random numbers
between 0 and 1.


๐Ÿง  Final Tips

  • NumPy is great for data analysis, machine learning, and scientific computing.
  • Arrays are faster and more efficient than regular Python lists.
  • Practice is key! Try changing the numbers and shapes to see what happens.

If you have any questions, suggestions, or corrections, please feel free to leave a comment. Your feedback helps me improve and create more accurate content.

Top comments (0)