吴恩达机器学习作业答案(Python版)

作者:rousong2024.01.29 16:22浏览量:18

简介:本文将提供吴恩达机器学习课程中Python版本的答案,帮助你更好地理解和学习机器学习。

在吴恩达的机器学习课程中,Python作为一种强大的编程语言被广泛使用。这里我们为你提供了作业答案的Python版。注意,为了简洁明了,我们将仅提供主要代码,并未包含完整的注释和详细解释。在实际操作中,请确保理解每行代码的含义。
作业一:线性回归
作业要求:使用Python实现线性回归,并使用梯度下降法进行参数优化。
以下是Python代码示例:

  1. import numpy as np
  2. # 定义模型函数
  3. def model(X, w, b):
  4. return np.dot(X, w) + b
  5. # 定义损失函数
  6. def loss(X, y, w, b):
  7. predictions = model(X, w, b)
  8. return np.sum((predictions - y) ** 2) / len(y)
  9. # 定义梯度下降函数
  10. def gradient_descent(X, y, w, b, learning_rate=0.01, num_iterations=1000):
  11. m = len(y)
  12. for i in range(num_iterations)::
  13. predictions = model(X, w, b)
  14. dw = (2 / m) * np.dot(X.T, (predictions - y))
  15. db = (2 / m) * np.sum(predictions - y)
  16. w = w - learning_rate * dw
  17. b = b - learning_rate * db
  18. return w, b
  19. # 训练模型
  20. X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) # 输入数据
  21. Y = np.array([3, 3, 4, 4]) # 输出数据
  22. w, b = gradient_descent(X, Y, np.zeros(2), 0)
  23. print('w:', w, 'b:', b)

作业二:逻辑回归
作业要求:使用Python实现逻辑回归,并使用梯度下降法进行参数优化。
以下是Python代码示例:
```python
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

定义sigmoid函数

def sigmoid(z):
return 1 / (1 + np.exp(-z))

定义模型函数

def model(X, w):
return sigmoid(np.dot(X, w))

定义损失函数

def loss(X, y, w):
predictions = model(X, w)
cost = np.sum((y np.log(predictions) + (1 - y) np.log(1 - predictions)) / len(y))
return -cost

加载数据集并进行预处理

titanicdata = np.genfromtxt(‘titanic.csv’, delimiter=’,’)
x = titanic_data[:, :-1]
y = titanic_data[:, -1]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
x_train = np.c
[np.ones(len(xtrain)), x_train]
x_test = np.c
[np.ones(len(x_test)), x_test]
y_train = np.array([0 if i <= 0.5 else 1 for i in y_train])
y_test = np.array([0 if i <= 0.5 else 1 for i in y_test])

使用梯度下降法训练模型并优化参数

best_params = minimize(loss, [0, 0], args=(x_train, y_train), method=’CG’, jac=True)
best_w = best_params.x[1:]
best_b = best_params.x[0]
predictions = model(