0% found this document useful (0 votes)
18 views5 pages

IP - NumPy

Chapter 6 covers NumPy, a library for numerical computing in Python that supports multi-dimensional arrays and mathematical functions. It explains the differences between NumPy arrays and Python lists, introduces the concept of array rank, and provides examples of creating and manipulating arrays. The chapter also includes commands for various operations such as reshaping, arithmetic operations, and statistical calculations.

Uploaded by

jevin884k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

IP - NumPy

Chapter 6 covers NumPy, a library for numerical computing in Python that supports multi-dimensional arrays and mathematical functions. It explains the differences between NumPy arrays and Python lists, introduces the concept of array rank, and provides examples of creating and manipulating arrays. The chapter also includes commands for various operations such as reshaping, arithmetic operations, and statistical calculations.

Uploaded by

jevin884k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 6: NumPy

1. What is NumPy? How to install it?

NumPy (Numerical Python) is a powerful library used for numerical computing in Python. It
provides support for large, multi-dimensional arrays and matrices, along with a collection of
mathematical functions to operate on these arrays. NumPy is widely used in data science,
machine learning, and scientific computing.

To install NumPy, you can use pip:

pip install numpy

2. What is an array and how is it different from a list? What is the name of the
built-in array class in NumPy?

 Array: An array is a data structure that can store multiple elements of the same type,
typically of a fixed size. In NumPy, arrays are implemented as ndarray (n-
dimensional array).
 List vs Array:
o List (Python list): Lists are flexible data structures that can store elements of
different data types, like integers, strings, and floats.
o Array (NumPy ndarray): Arrays are more efficient for numerical
computations and are homogeneous, meaning all elements must be of the same
data type (e.g., all integers or all floats). Operations on arrays are also faster
and can be performed element-wise.
 The built-in array class in NumPy is ndarray.

3. What do you understand by the rank of an ndarray?

The rank of an ndarray refers to the number of dimensions (or axes) it has. For example:

 A 1D array (like a list of numbers) has a rank of 1.


 A 2D array (like a matrix) has a rank of 2.
 A 3D array (like a tensor) has a rank of 3, and so on.

4. Create the following NumPy arrays:

a) A 1-D array called zeros having 10 elements and all the elements are set to zero.

import numpy as np
zeros = np.zeros(10)
print(zeros)

b) A 1-D array called vowels having the elements ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
vowels = np.array(['a', 'e', 'i', 'o', 'u'])
print(vowels)

c) A 2-D array called ones having 2 rows and 5 columns and all the elements are set to 1
and dtype as int.

ones = np.ones((2, 5), dtype=int)


print(ones)

d) Use nested Python lists to create a 2-D array called myarray1 having 3 rows and 3
columns and store the following data:

2.7, -2, -19


0, 3.4, 99.9
10.6, 0, 13
myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]])
print(myarray1)

e) A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start
value = 4, step size = 4, and dtype as float.

myarray2 = np.arange(4, 4 + 4*5, 4, dtype=float).reshape(3, 5)


print(myarray2)

5. Using the arrays created in Question 4 above, write NumPy commands for
the following:

a) Find the dimensions, shape, size, data type of the items, and itemsize of arrays zeros,
vowels, ones, myarray1, and myarray2.

arrays = [zeros, vowels, ones, myarray1, myarray2]

for arr in arrays:


print(f"Array: {arr}")
print(f"Dimensions: {arr.ndim}")
print(f"Shape: {arr.shape}")
print(f"Size: {arr.size}")
print(f"Data Type: {arr.dtype}")
print(f"Item Size: {arr.itemsize} bytes\n")

b) Reshape the array ones to have all the 10 elements in a single row.

ones_reshaped = ones.reshape(1, -1)


print(ones_reshaped)

c) Display the 2nd and 3rd element of the array vowels.

print(vowels[1:3]) # Indexing starts from 0

d) Display all elements in the 2nd and 3rd row of the array myarray1.

print(myarray1[1:3, :]) # Rows 2 and 3, all columns

e) Display the elements in the 1st and 2nd column of the array myarray1.

print(myarray1[:, 0:2]) # All rows, columns 1 and 2


f) Display the elements in the 1st column of the 2nd and 3rd row of the array myarray1.

print(myarray1[1:3, 0]) # Rows 2 and 3, column 1

g) Reverse the array of vowels.

print(vowels[::-1]) # Reversing the array

6. Using the arrays created in Question 4, write NumPy commands for the
following:

a) Divide all elements of array ones by 3.

ones_divided = ones / 3
print(ones_divided)

b) Add the arrays myarray1 and myarray2.

sum_arrays = myarray1 + myarray2


print(sum_arrays)

c) Subtract myarray1 from myarray2 and store the result in a new array.

difference = myarray2 - myarray1


print(difference)

d) Multiply myarray1 and myarray2 elementwise.

elementwise_multiplication = myarray1 * myarray2


print(elementwise_multiplication)

e) Do the matrix multiplication of myarray1 and myarray2 and store the result in a new
array myarray3.

myarray3 = np.dot(myarray1, myarray2.T) # Using transpose of myarray2 for


matrix multiplication
print(myarray3)

f) Divide myarray1 by myarray2.

division = myarray1 / myarray2


print(division)

g) Find the cube of all elements of myarray1 and divide the resulting array by 2.

cube_and_divide = (myarray1 ** 3) / 2
print(cube_and_divide)

h) Find the square root of all elements of myarray2 and divide the resulting array by 2.
The result should be rounded to two places of decimals.

sqrt_and_divide = np.sqrt(myarray2) / 2
sqrt_and_divide_rounded = np.round(sqrt_and_divide, 2)
print(sqrt_and_divide_rounded)
7. Using the arrays created in Question 4 above, write NumPy commands for
the following:

a) Find the transpose of ones and myarray2.

ones_transposed = ones.T
myarray2_transposed = myarray2.T
print(ones_transposed)
print(myarray2_transposed)

b) Sort the array vowels in reverse.

vowels_sorted_reverse = np.sort(vowels)[::-1]
print(vowels_sorted_reverse)

c) Sort the array myarray1 such that it brings the lowest value of the column in the first
row and so on.

myarray1_sorted_columns = np.sort(myarray1, axis=0)


print(myarray1_sorted_columns)

8. Using the arrays created in Question 4 above, write NumPy commands for
the following:

a) Use np.split() to split the array myarray2 into 5 arrays column-wise. Store the
resulting arrays in myarray2A, myarray2B, myarray2C, myarray2D, and myarray2E. Print
the arrays.

myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.split(myarray2,


5, axis=1)
print(myarray2A)
print(myarray2B)
print(myarray2C)
print(myarray2D)
print(myarray2E)

b) Split the array zeros at array index 2, 5, 7, 8 and store the resulting arrays in
zerosA, zerosB, zerosC, and zerosD and print them.

zerosA, zerosB, zerosC, zerosD = np.split(zeros, [2, 5, 7, 8])


print(zerosA)
print(zerosB)
print(zerosC)
print(zerosD)

c) Concatenate the arrays myarray2A, myarray2B, and myarray2C into an array having 3
rows and 3 columns.

myarray2_combined = np.concatenate((myarray2A, myarray2B, myarray2C),


axis=1)
print(myarray2_combined)
9. Create a 2-D array called myarray4 using arange() having 14 rows and 3
columns with start value = -1, step size 0.25. Split this array row-wise into 3
equal parts and print the result.
myarray4 = np.arange(-1, -1 + 0.25*42, 0.25).reshape(14, 3) # 14 rows, 3
columns
split_myarray4 = np.split(myarray4, 3, axis=0) # Split row-wise
for part in split_myarray4:
print(part)

10. Using the myarray4 created in the above questions, write commands for the
following:

a) Find the sum of all elements.

sum_all_elements = np.sum(myarray4)
print(sum_all_elements)

b) Find the sum of all elements row-wise.

sum_row_wise = np.sum(myarray4, axis=1)


print(sum_row_wise)

c) Find the sum of all elements column-wise.

sum_column_wise = np.sum(myarray4, axis=0)


print(sum_column_wise)

d) Find the max of all elements.

max_value = np.max(myarray4)
print(max_value)

e) Find the min of all elements in each row.

min_value_row_wise = np.min(myarray4, axis=1)


print(min_value_row_wise)

f) Find the mean of all elements in each row.

mean_value_row_wise = np.mean(myarray4, axis=1)


print(mean_value_row_wise)

g) Find the standard deviation column-wise.

std_deviation_column_wise = np.std(myarray4, axis=0)


print(std_deviation_column_wise)

You might also like