0% found this document useful (0 votes)
18 views

Code 2

The document is a student assignment response that includes programs in Python using the NumPy library to perform various data analysis and visualization tasks on random integer arrays. The programs compute statistics like mean, standard deviation, and variance on arrays; manipulate array shapes, types, and data types; find zero, non-zero and NaN elements; perform operations between arrays; and calculate covariance and correlation between arrays. The assignment includes well-commented code blocks for each question and prints the outputs of computations and array values.

Uploaded by

I am Nobody
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Code 2

The document is a student assignment response that includes programs in Python using the NumPy library to perform various data analysis and visualization tasks on random integer arrays. The programs compute statistics like mean, standard deviation, and variance on arrays; manipulate array shapes, types, and data types; find zero, non-zero and NaN elements; perform operations between arrays; and calculate covariance and correlation between arrays. The assignment includes well-commented code blocks for each question and prints the outputs of computations and array values.

Uploaded by

I am Nobody
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

\(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \

(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \


(~~~~~~~~~~~~~~~~~~~~\)

Name: Yash Gulati


Roll No: 6323
Subject: Data Visualization and
Analysis using Python
\(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \
(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \(~~~~~~~~~~~~~~~~~~~~\) \
(~~~~~~~~~~~~~~~~~~~~\)

Ques_1. Write programs in Python using NumPy library to


do the following:
import numpy as np

# a. Compute the mean, standard deviation, and variance of a two-dimensional random integer array along the second axis.
array_a = np.random.randint(0, 10, size=(3, 5))
mean_a = np.mean(array_a, axis=1)
std_dev_a = np.std(array_a, axis=1)
variance_a = np.var(array_a, axis=1)

print("a. Mean, Standard Deviation, and Variance:")


print("Array:")
print(array_a)
print("Mean along second axis:")
print(mean_a)
print("Standard Deviation along second axis:")
print(std_dev_a)
print("Variance along second axis:")
print(variance_a)

# b. Create a 2-dimensional array of size m x n integer elements, also print the shape, type and data type of the array,
# and then reshape it into an n x m array, where n and m are user inputs given at the run time.
m = int(input("Enter the number of rows (m): "))
n = int(input("Enter the number of columns (n): "))
array_b = np.random.randint(0, 10, size=(m, n))
shape_b = array_b.shape
type_b = type(array_b)
dtype_b = array_b.dtype

array_b_reshaped = array_b.reshape((n, m))

print("\nb. Array Shape, Type, and Data Type:")


print("Array:")
print(array_b)
print("Shape of the Array:")
print(shape_b)
print("Type of the Array:")
print(type_b)
print("Data Type of the Array:")
print(dtype_b)
print("Reshaped Array (n x m):")
print(array_b_reshaped)

# c. Test whether the elements of a given 1D array are zero, non-zero, and NaN.
# Record the indices of these elements in three separate arrays.
array_c = np.array([0, 1, 0, 3, np.nan, 5])
zero_indices = np.where(array_c == 0)[0]
nonzero_indices = np.where(array_c != 0)[0]
nan_indices = np.where(np.isnan(array_c))[0]

print("\nc. Zero, Non-zero, and NaN Indices:")


print("Array:")
print(array_c)
print("Indices of Zero elements:")
print(zero_indices)
print("Indices of Non-zero elements:")
print(nonzero_indices)
print("Indices of NaN elements:")
print(nan_indices)

# d. Create three random arrays of the same size: Array1, Array2, and Array3.
# Subtract Array2 from Array3 and store in Array4.
# Create another array Array5 having two times the values in Array1.
# Find Covariance and Correlation of Array1 with Array4 and Array5 respectively.
array_d1 = np.random.randint(0, 10, size=(4, 4))
array_d2 = np.random.randint(0, 10, size=(4, 4))
array_d3 = np.random.randint(0, 10, size=(4, 4))

array_d4 = array_d3 - array_d2


array_d5 = 2 * array_d1

covariance = np.cov(array_d1.flatten(), array_d4.flatten())[0, 1]


correlation = np.corrcoef(array_d1.flatten(), array_d5.flatten())[0, 1]

print("\nd. Arrays and Covariance/Correlation:")


print("Array1:")
print(array_d1)
print("Array2:")
print(array_d2)
print("Array3:")
print(array_d3)
print("Array4 (Array3 - Array2):")
print(array_d4)
print("Array5 (2 * Array1):")
print(array_d5)
print("Covariance between Array1 and Array4:")
print(covariance)
print("Correlation between Array1 and Array5:")
print(correlation)

# e. Create two random arrays of the same size 10: Array1 and Array2.
array_e1 = np.random.randint(0, 10, size=10)
array_e2 = np.random.randint(0, 10, size=10)

print("\ne. Array1 and Array2:")


print("Array1:")
print(array_e1)
print("Array2:")
print(array_e2)

Output

You might also like