1. Write a code for different types of array creation using numpy.
Ans:- NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
Example :-
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
A dimension in arrays is one level of array depth (nested arrays). nested array: are
arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example:- arr = np.array(42)
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These
are the most common and basic arrays.
Example:- arr = np.array([1, 2, 3, 4, 5])
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to
represent matrix or 2nd order tensors.
Example :- arr = np.array([[1, 2, 3], [4, 5, 6]])
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are
often used to represent a 3rd order tensor.
Example :- arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
2. Create a Numpy array filled with all ones.
Ans:-
import numpy as np
a = np.ones(3, dtype = int)
print("Matrix a : \n", a)
b = np.ones([3, 3], dtype = int)
print("\nMatrix b : \n", b)
3. Convert an array into zig-zag fashion in Python.
Ans:-
def zigZag(arr, n):
# use sort function to sort the array
arr.sort()
# traverse the array from 1 to n-1
for i in range(1, n-1, 2):
# swap value of current element with next element
arr[i], arr[i+1] = arr[i+1], arr[i]
# print the array
print(arr)
if __name__ == "__main__":
arr = [4, 3, 7, 8, 6, 2, 1]
n = len(arr)
zigZag(arr, n)
4. Compare and contrast the differences between indexing and slicing.
Ans:-
Indexing and Slicing:
In python sequence data types, we can access elements by indexing and slicing.
Sequence data types are strings, list, tuple, range objects.
Indexing: Indexing is used to obtain individual elements. Indexing starts from 0. Index 0
represents the first element in the sequence.
Negative indexing starts from -1. Index -1 represents the last element in the sequence.
Slicing: Slicing is used to obtain a sequence of elements. In slicing, we specify the start
index and end index.
Indexing and Slicing can be be done in Python Sequences types like list, string, tuple,
range objects.
5. Calculate the number of occurrences of a sequence [6, 5] in a given NumPy array
[[2,4,6,5], [3,4,6,5], [6,5,6,5],[6,5,7,8]].
# importing package
import numpy
# create numpy array
arr = numpy.array([[2, 4,6,5],
[3, 4, 6, 5],
[6, 5, 6, 5],
[6, 5, 7, 8]])
# Counting sequence
output = repr(arr).count("6, 5")
# view output
print(output)
6. Calculate the sum of the diagonal elements of a NumPy array
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
[30, 44, 2],
[11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 3X3 matrix:")
print(trace)
7. Generate a 4 * 4 Square identity matrix by using Numpy.
Ans:-
import numpy as np
dimension = 4
identity_matrix = np.identity(dimension, dtype="int")
print(identity_matrix)
8. Develop a program to generate 200 random numbers between 1 and 1000 by using
numpy.
Ans:-
import random
def Rand(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
num = 200
start = 1
end = 1000
print(Rand(start, end, num))
9. Create a NumPy 2-D array named "MyArray" having 2 rows and 5 columns and the
elements are set to 1 - 10 and data-type set as int.
Ans:--
import numpy as np
MyArray=np.array([[1,2,3,4,5],[6,7,8,9,10]],dtype='i')
print(MyArray)
print(type(MyArray))
10. Create a NumPy array named "zeros" having 10 elements and all the elements are
set to zero.
Ans:-
import numpy as np
zeros=np.zeros(10)
print(zeros)
print(type(zeros))