Mastering Data Science with NumPy — A Mini Guide
NumPy is the fundamental package for scientific computing in Python, and it’s an absolute game-changer in data science! Let’s dive in and explore some of its powerful capabilities. Let’s together learn about some NumPy usages briefly.
ARRAY CREATION WITH NUMPY
The first step is to collect the data. Here, we’re loading data from a CSV file using Python’s pandas library.
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
print(my_array)
GENERATING RANDOM NUMBERS
NumPy makes it easy to generate random numbers with just a few lines of code. Check out this example:
import numpy as np
random_nums = np.random.randint(1, 100, size=5)
print(random_nums)
ARRAY SHAPE MANIPULATION
NumPy allows you to reshape arrays effortlessly! Take a look at this code snippet:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
reshaped_array = my_array.reshape(1, 5)
print(reshaped_array)
ARRAY INDEXING
Accessing elements in a NumPy array is intuitive! Here’s a simple example:
import numpy as np
my_array = np.array([10, 20, 30, 40, 50])
print(my_array[2]) # Output: 30
ARRAY OPERATIONS
NumPy makes array operations a breeze! Check out this example of element-wise addition:
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array1 + array2
print(result)
FILTERING DATA WITH NUMPY
NumPy enables you to filter data based on specific conditions easily! Take a look at this code:
import numpy as np
data = np.array([10, 20, 30, 40, 50])
filtered_data = data[data > 30]
print(filtered_data)
BROADCASTING IN NUMPY
NumPy’s broadcasting simplifies array operations on arrays of different shapes! Check it out:
import numpy as np
array1 = np.array([1, 2, 3])
scalar = 10
result = array1 * scalar
print(result)
AGGREGATION WITH NUMPY
NumPy lets you perform aggregation operations effortlessly! Have a look at this example:
import numpy as np
data = np.array([10, 20, 30, 40, 50])
mean_value = np.mean(data)
print(mean_value)
Thanks for reading
- 📰 View more content on Programming Tutorials and Guides