You are on page 1of 9

CONTINOUS ASSESSMENT 2

NAME : SHALVIN K

REGISTER NUMBER: 12106295

ROLL NUMBER: 48

SET : VI

1. You have two arrays a and b of shape (3, 3) containing random integers between 0 and 9. Using NumPy, create a new array c that
contains the element-wise product of a and b, but only for values where a is greater than 4.

In [ ]: import numpy as np

# Defining array
a = np.random.randint(0, 10, size=(3, 3))
b = np.random.randint(0, 10, size=(3, 3))
print("a: ")
print(a)
print("b: ")
print(b)

c = np.where(a > 4, a * b, 0)
c

a:
[[7 8 2]
[7 0 3]
[6 3 9]]
b:
[[3 1 0]
[5 7 2]
[7 2 4]]
Out[ ]: array([[21, 8, 0],
[35, 0, 0],
[42, 0, 36]])

2. 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
inverse of a using the linalg module.

In [ ]: import numpy as np

# Defining array
a = np.random.randint(0, 10, size=(3, 3))
print(a)
print("\n")

print("Inverse: ")
# Finding inverse
b = np.linalg.inv(a)
b

[[7 9 4]
[8 5 2]
[2 2 8]]

Inverse:
Out[ ]: array([[-0.13636364, 0.24242424, 0.00757576],
[ 0.22727273, -0.18181818, -0.06818182],
[-0.02272727, -0.01515152, 0.14015152]])

3. You have an array a of shape (3, 3) and a vector v of shape (3,). Using NumPy, create a new array b that contains the dot product of a
and v.

In [ ]: import numpy as np

# Defining array
a = np.random.randint(0, 10, size=(3, 3))
v = np.random.randint(0, 10, size=(3,))
print("a: ")
print(a)
print("v: ")
print(v)
print("\n")

# Creating an array b with dot product od a and v


print("dot product: ")
b = np.dot(a, v)
b

a:
[[8 6 5]
[8 2 5]
[7 5 9]]
v:
[6 7 6]

dot product:
Out[ ]: array([120, 92, 131])

4. 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 maximum value in each row of a.

In [ ]: import numpy as np

# Defining array
a = np.random.randint(0, 10, size=(3, 3))
print("a: ")
print(a)
print("\n")

# Creating an array b that contains maximum value in each row of a


b = np.amax(a, axis=1)
print("b that contains maximum value in each row of a:", b)
a:
[[7 4 6]
[5 6 7]
[2 0 2]]

b that contains maximum value in each row of a: [7 7 2]

5. You have two arrays a and b of shape (3, 3) containing random integers between 0 and 9. Using

NumPy, create a new array c that contains the element-wise difference between a and b.

In [ ]: import numpy as np

# Defining array
a = np.random.randint(0, 10, size=(3, 3))
b = np.random.randint(0, 10, size=(3, 3))
print("a: ")
print(a)
print("v: ")
print(b)
print("\n")

# Creating an array c with difference of a and b


print("Difference: ")
c = a - b
c

a:
[[1 3 8]
[9 0 5]
[6 7 8]]
v:
[[6 4 0]
[5 2 2]
[7 7 7]]

Difference:
Out[ ]: array([[-5, -1, 8],
[ 4, -2, 3],
[-1, 0, 1]])

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 sum of the diagonal of a.

In [ ]: import numpy as np

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

# Calculating the sum of diagonals of a


b = np.trace(a)
print("Sum of diagonals:",b)

Sum of diagonals: 20

7. 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 exponential of each element in a.

In [ ]: import numpy as np

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

# Creating a new array b containing exponents


b = np.exp(a)

print(a)
print("\n")
print("Exponents of a:")
print(b)

[[2 9 1]
[9 2 2]
[6 0 4]]

Exponents of a:
[[7.38905610e+00 8.10308393e+03 2.71828183e+00]
[8.10308393e+03 7.38905610e+00 7.38905610e+00]
[4.03428793e+02 1.00000000e+00 5.45981500e+01]]

8. You have an array a of shape (3, 3) and a vector v of shape (3,). Using NumPy, create a new array

b that contains the solution to the linear equation a x = v

In [ ]: import numpy as np

# Defining arrays
a = np.array([[1, 2, 3], [4, 5, 2], [7, 1, 9]])
v = np.array([1, 2, 3])

# Creating a new array b that contains the solution to the linear equation a x = v
b = np.linalg.solve(a, v)
print("Solution of the line: ",b)

Solution of the line: [0.21276596 0.17021277 0.14893617]

9. You have two arrays a and b of shape (3, 3) containing random integers between 0 and 9. Using

NumPy, create a new array c that contains the matrix product of a and b.

In [ ]: import numpy as np

# Defining arrays
a = np.random.randint(0, 10, size=(3, 3))
b = np.random.randint(0, 10, size=(3, 3))

print("a: ")
print(a)
print("b: ")
print(b)
print("\n")

# Creating a new array c that contains the matrix product of a and b


c = np.dot(a, b)

print("Dot product: ")


print(c)

a:
[[9 3 5]
[4 4 9]
[3 9 4]]
b:
[[6 4 0]
[0 4 4]
[9 1 6]]

Dot product:
[[ 99 53 42]
[105 41 70]
[ 54 52 60]]

10. You have a data frame containing the names, ages and salaries of employees. Using Pandas,

create a new data frame that contains the mean and median salary for each age group.

In [ ]: import pandas as pd

# Defining Dataframe
data = {'Name': ['Kend', 'Bob', 'Pup', 'Dave', 'Eve', "Charlie"],
'Age': [25, 30, 35, 40, 45, 25],
'Salary': [50000, 60000, 70000, 80000, 90000, 65000]}
df = pd.DataFrame(data)

# Creating a new data frame that contains the mean and median salary for each age group
grouped = df.groupby('Age')['Salary'].agg(['mean', 'median'])

print(grouped)
mean median
Age
25 57500.0 57500.0
30 60000.0 60000.0
35 70000.0 70000.0
40 80000.0 80000.0
45 90000.0 90000.0

11. You have a data frame containing the names, genders and salaries of employees. Using Pandas,

create a new data frame that contains the mean salary for male and female employees separately, but only for employees whose salary is
above the overall mean salary.

In [ ]: import pandas as pd

# Defining Dataframe
data = {'Name': ['kinder', 'Thar', 'Poppy', 'leo', 'vinder'],
'Gender': ['Female', 'Male', 'Male', 'Male', 'Female'],
'Salary': [50000, 60000, 70000, 80000, 90000]}
df = pd.DataFrame(data)

# Calculate the overall mean salary


mean_salary = df['Salary'].mean()

# Creating a new data frame that contains the mean salary for male and female employees separately,
# but only for employees whose salary is above the overall mean salary
grouped = df[df['Salary'] > mean_salary].groupby('Gender')['Salary'].mean()

print(grouped)

Gender
Female 90000.0
Male 80000.0
Name: Salary, dtype: float64

12. You have a data frame containing the names, ages and occupations of individuals. Using Pandas,

create a new data frame that contains the percentage of individuals in each age group for each occupation.
In [ ]: import pandas as pd

# Defining Dataframe
data = {'Name': ['Pippo', 'sind', 'ploa', 'bino', 'veatel'],
'Age': [25, 30, 35, 40, 45],
'Occupation': ['Doctor', 'Lawyer', 'Engineer', 'Teacher', 'Nurse']}
df = pd.DataFrame(data)

# Create a new data frame that contains the percentage of individuals in each age group for each occupation
grouped = df.groupby(['Occupation', 'Age']).size().groupby(level=0).apply(lambda x: 100 * x / x.sum()).reset_index(

print(grouped)

Occupation Age Percentage


0 Doctor 25 100.0
1 Engineer 35 100.0
2 Lawyer 30 100.0
3 Nurse 45 100.0
4 Teacher 40 100.0
C:\Users\shalv\AppData\Local\Temp\ipykernel_11908\4016886966.py:10: FutureWarning: Not prepending group keys to the
result index of transform-like apply. In the future, the group keys will be included in the index, regardless of wh
ether the applied function returns a like-indexed object.
To preserve the previous behavior, use

>>> .groupby(..., group_keys=False)

To adopt the future behavior and silence this warning, use

>>> .groupby(..., group_keys=True)


grouped = df.groupby(['Occupation', 'Age']).size().groupby(level=0).apply(lambda x: 100 * x / x.sum()).reset_inde
x(name='Percentage')

In [ ]:

You might also like