0% found this document useful (0 votes)
15 views9 pages

Assignment AI 1 D1 20233563 SHOAIB SAMIM

Uploaded by

samimshoaib01
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)
15 views9 pages

Assignment AI 1 D1 20233563 SHOAIB SAMIM

Uploaded by

samimshoaib01
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/ 9

AI ASSIGNMENT:1

Registration no. : 20233563


Name:Shoaib samim
Section:D

import numpy as np

# 1. Create a 1D NumPy array of integers from 10 to 50


arr1 = np.arange(10, 51)
print(arr1)

Output:

# 2. Create the following:


a = np.zeros((3, 4))
b = np.ones((4, 3))
c = np.eye(3)
print(a)
print(b)
print(c)

Output:

# 3. Create a NumPy array of 15 evenly spaced values between 0 and 5


arr2 = np.linspace(0, 5, 15)
print(arr2)
Output:

# 4. Extract the second and third rows of the array


arr3 = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
print(arr3[1:])
Output:

# 5. Reverse an array using slicing


arr4 = np.arange(10)
print(arr4[::-1])
Output:

# 6. Reshape the array into a 3x4 matrix


arr5 = np.arange(12).reshape(3, 4)
print(arr5)
Output:

# 7. Flatten a 2D array into a 1D array


arr6 = arr5.flatten()
print(arr6)
Output:
# 8. Concatenate two arrays row-wise and column-wise
arr7 = np.array([1, 2, 3])
arr8 = np.array([4, 5, 6])
print(np.concatenate((arr7, arr8)))
print(np.stack((arr7, arr8), axis=1))
Output:

# 9. Element-wise operations
print(arr7 + arr8, arr7 - arr8, arr7 * arr8, arr7 / arr8)
Output:

# 10. Dot product


a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.dot(a, b))
# 11. Mean, median, and standard deviation
arr9 = np.array([10, 20, 30, 40, 50])
print(np.mean(arr9), np.median(arr9), np.std(arr9))
Output:

# 12. Sum of all elements, rows, and columns


arr10 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.sum(arr10), np.sum(arr10, axis=1), np.sum(arr10, axis=0))
Output:

# 13. Filter elements greater than 25


arr11 = np.array([10, 20, 30, 40, 50])
print(arr11[arr11 > 25])
Output:
# 14. Indices of non-zero elements
arr12 = np.array([0, 3, 0, 5, 6, 0, 7])
print(np.nonzero(arr12)[0])
Output:

# 15. Generate random arrays


print(np.random.randint(0, 100, 10))
print(np.random.rand(4, 4))
Output:

# 16. Set random seed and generate array


np.random.seed(42)
arr13 = np.random.randint(1, 10, 5)
print(arr13)
np.random.seed(42)
print(np.random.randint(1, 10, 5))
Output:
# 17. Stack arrays
arr14 = np.array([1, 2, 3])
arr15 = np.array([4, 5, 6])
print(np.vstack((arr14, arr15)))
print(np.hstack((arr14, arr15)))
Output:

# 18. Generate a histogram


import numpy as np
import matplotlib.pyplot as plt

data = np.random.randint(1, 101, size=50)

plt.hist(data, bins=10, edgecolor='black')

plt.xlabel('Value Range')
plt.ylabel('Frequency')
plt.title('Histogram with 10 Bins')

plt.show()

Output:
# 19. Simulate coin toss experiments
print(np.random.binomial(10, 0.5, 20))
Output:

# 20. Create array using zeros, ones, and concatenation


arr16 = np.concatenate((-np.ones((1, 4)), np.zeros((1, 4)), 2 * np.ones((1, 4))), axis=0)
print(arr16)
Output:
# 21. Create array of even numbers
arr17 = np.arange(2, 41, 2).reshape(4, 5)
print(arr17)
Output:

# 22. Create matrix and access rows and columns


arr18 = np.arange(10, 50, 2).reshape(4, 5)
print(arr18[:, 2])
arr18[3] = np.array([1, 2, 3, 4, 5])
print(arr18)
Output:

# 23. Sequence of -1 and 1 for coin toss


print(2 * np.random.binomial(1, 0.5, 10) - 1)
Output:
# 24. Vectorized operations
names = np.array(["Roxana", "Statira", "Roxana", "Statira", "Roxana"])
score = np.array([126, 115, 130, 141, 132])
print(score[score < 130])
print(score[names == "Statira"])
score[names == "Roxana"] += 10
print(score)
Output:

You might also like