Introduction to NumPy part 2/3

Introduction to NumPy part 2/3

Multi-dimensional arrays

One of the key advantages of NumPy is its ability to efficiently work with multi-dimensional arrays. These arrays can have any number of dimensions, from one-dimensional arrays (vectors) to two-dimensional arrays (matrices) to higher-dimensional arrays.

To create a multi-dimensional array in NumPy, you can use the numpy.array() function and pass in a nested list or tuple of values.

import numpy as np
 
# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)        

NumPy provides many functions and methods for manipulating and accessing elements of multi-dimensional arrays. You can perform mathematical operations on arrays, such as addition, subtraction, multiplication, and division, using the standard arithmetic operators or the corresponding NumPy functions. For example:

import numpy as np
 
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
 
# Element-wise addition
result = arr1 + arr2
print(result)
# Output:
# [[ 6  8]
# [10 12]]
 
# Element-wise multiplication
result = arr1 * arr2
print(result)
# Output:
# [[ 5 12]
#  [21 32]]        

In addition to mathematical operations, you can also apply functions to arrays. NumPy provides a wide range of built-in functions that can be applied element-wise to multi-dimensional arrays. For example, you can calculate the sum, mean, maximum, minimum, or standard deviation of an array using the respective functions:

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
 
# Calculate the sum of all elements
sum_val = np.sum(arr)
print(sum_val)
# Output:
# 21
 
# Calculate the mean of each column
mean_val = np.mean(arr, axis=0)
print(mean_val)
# Output:
# [2.5 3.5 4.5]        

These are just a few examples of the operations and functions that can be performed on multi-dimensional arrays in NumPy. By leveraging the power of NumPy, you can efficiently manipulate and process large amounts of numerical data. Stay tuned for the next section where we will explore more advanced features of NumPy.

# creates an array with 5 rows and 2 columns, filled with zeros
np.zeros((5, 2))
# Output:
#array([[0., 0.],
#       [0., 0.],
#       [0., 0.],
#       [0., 0.],
#       [0., 0.]])
 
# creates an array from a python list
# here we have a list of lists
np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
# Output:
# array([[1, 2, 3],
#       [4, 5, 6],
#       [7, 8, 9]])        

Accessing elements in a multi-dimensional array

To access elements in a multi-dimensional array, you can use indexing. In NumPy, indexing works similarly to regular Python lists or arrays. You can use square brackets [ ] and provide the index or indices for the desired element or elements.

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
 
# Accessing a single element
element = arr[0, 2]
print(element)
# Output: 3        

Accessing only rows

To access only rows of a multi-dimensional array in NumPy, you can use indexing with a colon : to specify all columns and provide the indices for the desired rows.

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
 
# Accessing a single row
row = arr[1, :]
print(row)
# Output: [4 5 6]        

You can also access multiple rows by providing a list of indices. For instance:

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
 
# Accessing multiple rows
rows = arr[[0, 2], :]
print(rows)
# Output:
# [[1 2 3]
#  [7 8 9]]        

In this example, we access the first and third rows of the array arr by providing a list of indices [0, 2]. The output will be [[1 2 3] [7 8 9]], which represents the selected rows.

By using indexing with the colon : and providing the appropriate indices, you can easily access specific rows or subsets of rows from a multi-dimensional array in NumPy.

n = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 
# outputs the first row
n[0]
# Output:
# array([ 1, 20,  3])
 
n[0] = [12, 13, 14]
n
# Output:
# array([[12, 13, 14],
#       [ 4,  5,  6],
#       [ 7,  8,  9]])        

Accessing only columns

To access only columns of a multi-dimensional array in NumPy, you can use indexing with a colon : to specify all rows and provide the indices for the desired columns. For example:

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
 
# Accessing a single column
column = arr[:, 1]
print(column)
# Output: [2 5]        

In this example, we access a single column from the array arr using the index 1 for the column and : to indicate all rows. This will give us the values [2 5] which represent the second column of the array.

You can also access multiple columns by providing a list of indices. For instance:

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
 
# Accessing multiple columns
columns = arr[:, [0, 2]]
print(columns)
# Output:
# [[1 3]
#  [4 6]
#  [7 9]]        

By using indexing with the colon : and providing the appropriate indices, you can easily access specific columns or subsets of columns from a multi-dimensional array in NumPy.

n = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 
# outputs the first column
n[:, 0]
# Output:
# array([1, 4, 7])
 
n[:, 0] = [12, 13, 14]
n
# Output:
# array([[12,  2,  3],
#        [13,  5,  6],
#        [14,  8,  9]])        

In this example, we access and modify the first column of the array n. The output will be array([[12, 2, 3], [13, 5, 6], [14, 8, 9]]), where the first column has been replaced with the values [12, 13, 14].

Accessing specific columns or modifying columns in a multi-dimensional array can be done easily using the indexing capabilities provided by NumPy.

n = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 
# outputs the first column
n[:,0]
# Output: 
# array([1,  4,  7])
 
# outputs all the columns
n[:]
# Output:
# array([[1, 2, 3],
#       [ 4,  5,  6],
#       [ 7,  8,  9]])
 
n[:, 2] = [0, 1, 2]
n
# Output:
# array([[1, 2,  0],
#       [ 4,  5,  1],
#       [ 7,  8,  2]])        

To view or add a comment, sign in

More articles by Alka kumari

  • DATA ANALYTICS

    Data analytics is the art and science of drawing actionable insights from data. Data Analytics + Business Knowledge =…

  • Why Statistics is Important

    Statistics is the discipline that concerns the collection, organization, analysis, interpretation, and presentation of…

  • Data Analytics Techniques

    Part 1: What is Data Analysis and What Does a Data Analyst Do? What is Data Analysis? 1. There is no one-size-fits-all…

    1 Comment
  • The Epic 2024 Guide to Data Mastery

    In today’s data-driven world, the role of a Data Scientist is not just lucrative but also transformative. These highly…

  • The data analytics project life cycle

    Here are the Important stages: Identifying the right problem statements for your Business Problem Designing the right…

  • 📊 Introduction to Descriptive Statistics 📊

    Descriptive statistics are a set of techniques and methods used in data analysis to provide a clear and concise summary…

    1 Comment
  • Special matrix types

    Identity matrix An identity matrix is a special type of square matrix in linear algebra. It is denoted as I and has…

  • Vector vector multiplication (dot product) 2

    The dot product, also known as the scalar product, is a key operation in linear algebra. It involves multiplying the…

  • Introduction to NumPy part 3/3

    Randomly generated arrays In addition to creating and manipulating multi-dimensional arrays, NumPy also provides the…

  • Introduction to NumPy part 1

    Numpy is a powerful Python library that provides support for large, multi-dimensional arrays and matrices, along with a…

    4 Comments

Insights from the community

Others also viewed

Explore topics