Day 10 Of Learning Python for Data Science - NumPy Array In Python
NumPy Array in Python is a powerful library for numerical computing in Python. It provides efficient support for multi-dimensional arrays, mathematical functions, and vectorized operations that make computations significantly faster compared to standard Python lists.
Day 9 of Learning Python for Data Science – Queries Related To Functions In Python
NumPy is essential for data analysis, machine learning, and scientific computing because:
import numpy as np
arr = np.array([1,2,3,4,5])
print("NumPy array:", arr)
NumPy provides several ways to create arrays:
arr_2d = np.array([[1, 2], [3, 4]])
print(arr_2d)
# Array of zeros
zeros = np.zeros((2,3))
print(zeros)
# Array of ones
ones = np.ones((3,3))
print(ones)
range_array = np.arange(0, 10, 2) # 0 to 10 with step 2
print(range_array)
range_array = np.arange(0, 24, 2).reshape(3,4)
print(range_array)
zeros = np.zeros((2,3), dtype=int)
print(zeros)
NumPy Array In Python allows easy mathematical operations on arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise addition
print(a + b)
# Element-wise multiplication
print(a * b)
# Scalar multiplication
print(a * 2)
Other operations:
# Addition
ad = np.add(a, b)
print(ad)
# Multiplication
ml = np.multiply(a, b)
print(ml)
# Power
p = np.power(a, 2)
print(p)
arr = np.array([1,2,3,4,5,6])
print(arr[0]) # First element
print(arr[-1]) # Last element
print(arr[1:5])
arr.reshape(2,3)
a = np.array([[1, 2], [3, 4]])
# Sum
print("Sum:", a.sum())
# Mean
print("Mean:", a.mean())
# Min and Max
print("Min:", a.min())
print("Max:", a.max())
# Standard Deviation
print("Std. Dev:", a.std())
# Unique Elements
print("Unique:", np.unique(a))
# Square Root
print("Square Root:", np.sqrt(a))
data = np.array([25, 32, 45, 28, 35, 40, 38, 22, 30, 42, 42, 100])
unique, count = np.unique(data, return_counts=True)
print(unique, count)
data = np.array([25, 32, 45, 28, 35, 40, 38, 22, 30, 42, 42, 100])
filtered_data = data[data > 30]
print(filtered_data)
data = np.array([18,22,np.nan,28,23,np.nan,30])
mean = np.nanmean(data)
data[np.isnan(data)] = mean
print(data)
data = np.array([-5, 12, -9, 45, -23, 30])
data[data < 0] = 0
print(data)
np.where
data = np.array([10,55,25,70,40,80])
r = np.where(data > 50, 100, data)
print(r)
np.where()
.We hope this article was helpful for you and you learned a lot about data analyst interview from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
NumPy Array In Python NumPy Array In Python NumPy Array In Python NumPy Array In Python NumPy Array In Python
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…
Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…
Welcome back to our Data Science Interview Questions series! In the first section, we explored…
Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…
In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…
Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…