Professional Documents
Culture Documents
Shalvin
Shalvin
NAME : SHALVIN K
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.
# 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.
# 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.
# 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")
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.
# Defining array
a = np.random.randint(0, 10, size=(3, 3))
print("a: ")
print(a)
print("\n")
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.
# 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")
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,
# Defining array
a = np.random.randint(0, 10, size=(3, 3))
Sum of diagonals: 20
7. You have an array a of shape (3, 3) containing random integers between 0 and 9. Using NumPy,
# Defining array
a = np.random.randint(0, 10, size=(3, 3))
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
# 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)
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.
# 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")
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.
# 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.
# 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)
# 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)
In [ ]: