# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Answers to Exercises

# **Exercise 1**

np.sum(big_array[big_array % 2 == 1])

# **Exercise 2**

random[1:5, 2:5] + np.eye(3, 3)

# **Exercise 3**

# +
big_part = random[:3, :5].reshape((1, 15))
negative_1 = random[3][5]

small_part = np.array([negative_1]) #this just puts -1 into an array so it can be appended to the vector big_part

combined = np.append(big_part, small_part)
square = combined.reshape((4, 4))
square_mean = np.mean(square)
square_mean
# -

# **Exercise 4**

# +
x = np.array([3.0, 5.0, 8.0])
y = np.array([3.0, 5.0, 8.0])

#Square x here:
x = x ** 2

#Square y here:
for i in range(len(y)):
    y[i] = y[i] ** 2
# -

# **Exercise 5:**

prices.sum(axis=0)

cheapest_store = "C"

# **Exercise 6:**

vector = np.array([3, 1, -3])
an_array = np.array([[-2, -1, 3], [-3, 0, 3], [-3, -1, 4]])
identity = an_array + vector #does this look like np.eye(3, 3)?
identity

# **Exercise 7:**

x = np.array([-7, 7])

# **Exercise 8:**

y[np.sqrt(x) == 2]

# **Exercise 9:**

theta_hat = np.linalg.solve((X.T @ X), (X.T @ Y))

loss = np.dot(np.linalg.norm(Y - (X @theta_hat)), np.linalg.norm(Y - (X @ theta_hat)))

# **Exercise 10:**

# +
male = titanic[titanic["Sex"] == "male"]
male_avg = male["Fare"].mean()
male_avg

female = titanic[titanic["Sex"] == "female"]
female_avg = female["Fare"].mean()
female_avg
# -

# **Exercise 11:**

survived = titanic[titanic["Survived"] == 1]
age_survived = survived["Age"].mean()
age_survived

died = titanic[titanic["Survived"] == 0]
age_died = died["Age"].mean()
age_died

# **Exercise 12:**

class_1 = titanic[titanic["Pclass"] == 1]
survived_1 = len(class_1[class_1["Survived"] == 1])
total_1 = len(class_1)
proportion_1 = survived_1 / total_1

class_2 = titanic[titanic["Pclass"] == 2]
survived_2 = len(class_2[class_2["Survived"] == 1])
total_2 = len(class_2)
proportion_2 = survived_2 / total_2

class_3 = titanic[titanic["Pclass"] == 3]
survived_3 = len(class_3[class_3["Survived"] == 1])
total_3 = len(class_3)
proportion_3 = survived_3 / total_3

StarcoderdataJupyterScriptsDedupFiltered