You are on page 1of 5

akshat12107597ca2

April 15, 2023

1 CA -2
NAME- AKSHAT KESHARI SECTION- K21UP ROLLNO. RK21UPA05 SET- 5
1. You have an array a of shape (5, 5) containing random values between 0 and 1 Using
NumPy,create a new array b that contains the same values as a, but with all values be-
low 0.5 set to 0 and all values above or equal to 0.5 set to 1.
[1]: import numpy as np

a = np.random.rand(5, 5)
print("Original array:")
print(a)

b = np.where(a >= 0.5, 1, 0)


print("New array:")
print(b)

Original array:
[[0.95433128 0.39393158 0.46737198 0.72349899 0.68826703]
[0.03553819 0.69264876 0.44958678 0.44951954 0.22428856]
[0.20818681 0.93821598 0.73401102 0.61053147 0.0351608 ]
[0.78394366 0.40604173 0.13904611 0.26412298 0.47912239]
[0.51086216 0.02590215 0.61709563 0.37454472 0.99492379]]
New array:
[[1 0 0 1 1]
[0 1 0 0 0]
[0 1 1 1 0]
[1 0 0 0 0]
[1 0 1 0 1]]
2. You have an array a of shape (4, 4) containing random integers between 0 and 9. Using
NumPy,create a new array b that contains the product of the even values in each row of a.
[8]: import numpy as np

a = np.random.randint(1, 10 , size=(4, 4))


print("original array : ")
print(a)

1
even_values = np.where(a % 2 == 0, a, 1)

b = np.prod(even_values, axis=1)
print("New Array:")
print(b

File "C:\Users\AKSHAT KESHARI\AppData\Local\Temp\ipykernel_25436\929746564.


py", line 11

print(b
^
SyntaxError: unexpected EOF while parsing

4. You have an array a of shape (3, 3) and an array b of shape (3, 1). Using NumPy, create a
newarray c that contains the sum of a and b using broadcasting.
[7]: import numpy as np

a = np.random.randint(0, 50, size= (3, 3))

b = np.random.randint(0, 50, size= (3, 1))

c = a + b

print("Array a:")
print(a)
print("Array b:")
print(b)
print("Array c:")
print(c)

Array a:
[[25 24 39]
[17 12 12]
[47 6 31]]
Array b:
[[39]
[44]
[21]]
Array c:
[[64 63 78]
[61 56 56]
[68 27 52]]
5. You have an array a of shape (2, 2) and an array b of shape (2, 2). Using NumPy, create a
new array c that contains the element-wise maximum of a and b.

2
[6]: import numpy as np

a = np.random.randint(0, 50, size= (2, 2))

b = np.random.randint(0, 50, size= (2, 2))

c = np.maximum(a, b)

print("Array a:")
print(a)
print("Array b:")
print(b)
print("Array c:")
print(c)

Array a:
[[15 25]
[30 42]]
Array b:
[[32 48]
[48 29]]
Array c:
[[32 48]
[48 42]]
6. You have an array a of shape (3, 3) containing random integers between 0 and 9. Using
NumPy,create a new array b that contains the indices of the maximum value in each row of
a.
[5]: import numpy as np

a = np.random.randint(0, 10, size=(3, 3))

b = np.argmax(a, axis=1)

print("Array a:")
print(a)
print("Array b:")
print(b)

Array a:
[[7 3 3]
[1 6 7]
[5 3 1]]
Array b:
[0 2 0]
10. You have a data frame containing the names, ages and occupations of individuals. Using
Pandas,create a new data frame that contains the top 5 most common occupations and their

3
respectivefrequencies.
[4]: import pandas as pd

df = pd.DataFrame({
'Name': ['John', 'sam', 'Bobby', 'Jane', 'Mike', 'Kate', 'Chris', 'Linda',␣
↪'David', 'Maggi'],

'Age': [27, 34, 42, 25, 37, 41, 29, 45, 31, 38],
'Occupation': ['Engineer', 'Teacher', 'Doctor', 'Lawyer', 'Engineer',␣
↪'Doctor', 'Teacher', 'Doctor', 'Engineer', 'Lawyer']

})

top_occupations = df['Occupation'].value_counts().nlargest(5).reset_index()
top_occupations.columns = ['Occupation', 'Frequency']

print(top_occupations)

Occupation Frequency
0 Engineer 3
1 Doctor 3
2 Teacher 2
3 Lawyer 2
11. You have a data frame containing the names, heights and weights of individuals. Using
Pandas,create a new data frame that contains the correlation between height and weight.
[2]: import pandas as pd

df = pd.DataFrame({
'Name': ['John', 'sam', 'Bobby', 'Jane', 'Mike', 'Kate', 'Chris', 'Linda',␣
↪'David', 'Maggi'],

'Height (in)': [70, 64, 72, 62, 68, 71, 69, 66, 73, 67],
'Weight (lb)': [165, 123, 195, 115, 155, 170, 160, 140, 180, 150]
})

corr = df['Height (in)'].corr(df['Weight (lb)'])


corr_df = pd.DataFrame({'Correlation': [corr]})

print(corr_df)

Correlation
0 0.971339
12. You have a data frame containing the names, ages and genders of students. Using Pan-
das,calculate the average age and standard deviation for male and female students separately.
[3]: import pandas as pd

4
df = pd.DataFrame({
'Name': ['John', 'sam', 'Bobby', 'jane', 'Mike', 'Kate', 'Chris', 'Linda',␣
↪'David', 'Maggi'],

'Age': [18, 18, 19, 24, 22, 20, 16, 21, 21, 18],
'Gender': ['M', 'M', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'F']
})

male_df = df[df['Gender'] == 'M']


male_avg_age = male_df['Age'].mean()
male_std_age = male_df['Age'].std()

female_df = df[df['Gender'] == 'F']


female_avg_age = female_df['Age'].mean()
female_std_age = female_df['Age'].std()

result_df = pd.DataFrame({
'Gender': ['M', 'F'],
'Average Age': [male_avg_age, female_avg_age],
'Standard Deviation': [male_std_age, female_std_age]
})
print(result_df)

Gender Average Age Standard Deviation


0 M 19.00 2.19089
1 F 20.75 2.50000

You might also like