Categories: Blog

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

Why Learn NumPy?

NumPy is essential for data analysis, machine learning, and scientific computing because:

  • It enables fast handling of large datasets.
  • It forms the foundation for libraries like pandas, scikit-learn, TensorFlow, and PyTorch.
  • It supports vectorized operations, making calculations more efficient than Python loops.

Getting Started with NumPy Array In Python

Importing NumPy

import numpy as np

Creating a 1D Array

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

Creating NumPy Array In Python

NumPy provides several ways to create arrays:

2D Array using Nested Lists

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

Creating Arrays with Default Values

# Array of zeros
zeros = np.zeros((2,3))
print(zeros)

# Array of ones
ones = np.ones((3,3))
print(ones)

Creating Arrays with a Range of Numbers

range_array = np.arange(0, 10, 2)  # 0 to 10 with step 2
print(range_array)

Reshaping Arrays

range_array = np.arange(0, 24, 2).reshape(3,4)
print(range_array)

Defining Data Type while Creating an Array

zeros = np.zeros((2,3), dtype=int)
print(zeros)

Array Operations

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)

Indexing, Slicing, and Reshaping

Accessing Elements

arr = np.array([1,2,3,4,5,6])
print(arr[0])  # First element
print(arr[-1]) # Last element

Slicing

print(arr[1:5])

Reshaping

arr.reshape(2,3)

Mathematical and Statistical Functions

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 Manipulation and Filtering

Finding Unique Values and Their Frequencies

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)

Filtering Data Based on Conditions

data = np.array([25, 32, 45, 28, 35, 40, 38, 22, 30, 42, 42, 100])
filtered_data = data[data > 30]
print(filtered_data)

Replacing Missing Values

data = np.array([18,22,np.nan,28,23,np.nan,30])
mean = np.nanmean(data)
data[np.isnan(data)] = mean
print(data)

Replacing Negative Values with Zero

data = np.array([-5, 12, -9, 45, -23, 30])
data[data < 0] = 0
print(data)

Conditional Replacement using np.where

data = np.array([10,55,25,70,40,80])
r = np.where(data > 50, 100, data)
print(r)

7. Practice Questions

Beginner Level

  1. Create a 1D array with numbers from 10 to 50.
  2. Create a 3×3 matrix with values ranging from 0 to 8.
  3. Reverse an array of numbers from 1 to 10.
  4. Find the mean and standard deviation of an array.
  5. Replace all even numbers in an array with -1.
  6. Find the unique elements and their frequencies in a dataset.
  7. Extract all numbers greater than 25 from an array.
  8. Reshape an array of size 16 into a 4×4 matrix.
  9. Replace NaN values in an array with the column mean.
  10. Find the cumulative sum of a given array.

Intermediate Level

  1. Multiply two matrices using NumPy.
  2. Normalize an array between 0 and 1.
  3. Stack two arrays vertically and horizontally.
  4. Compute the dot product of two vectors.
  5. Create an identity matrix of size 5×5.
  6. Replace values in an array based on a condition using np.where().
  7. Create an array of 20 random numbers and find the top 5 highest values.
  8. Perform element-wise division of two arrays and handle division by zero.
  9. Extract the diagonal elements of a square matrix.
  10. Convert a NumPy array to a Python list.

Advanced Level

  1. Find the determinant and inverse of a matrix.
  2. Create a random matrix and sort its rows by the second column.
  3. Implement a moving average on a 1D NumPy array.
  4. Create a function that removes outliers from an array using standard deviation.
  5. Perform one-hot encoding on a categorical array.
  6. Generate a 10×10 matrix with random values and normalize its columns.
  7. Compute the Pearson correlation coefficient between two arrays.
  8. Perform matrix multiplication without using NumPy’s built-in function.
  9. Create a NumPy-based simple linear regression model.
  10. Implement a function to find the Euclidean distance between two points using NumPy.

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

Spread the love

Recent Posts

Mastering Pivot Table in Python: A Comprehensive Guide

Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…

2 weeks ago

Data Science Interview Questions Section 3: SQL, Data Warehousing, and General Analytics Concepts

Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…

2 weeks ago

Data Science Interview Questions Section 2: 25 Questions Designed To Deepen Your Understanding

Welcome back to our Data Science Interview Questions series! In the first section, we explored…

2 weeks ago

Data Science Questions Section 1: Data Visualization & BI Tools (Power BI, Tableau, etc.)

Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…

3 weeks ago

Optum Interview Questions: 30 Multiple Choice Questions (MCQs) with Answers

In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…

3 weeks ago

Day 15 of Learning Python for Data Science: Exploring Matplotlib Visualizations and EDA

Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…

3 weeks ago