# 作业

## 第一章 ex1-线性回归

1 输入一行代码，返回一个5×5的的单位矩阵

``````import numpy as np
print(np.eye(5))#或print(np.identity(5)) 不同之处在于后者只能创建方阵
``````

2 单变量线性回归

2.1 读取 ex1data1.txt 中数据，并根据数据画出散点图。散点图x轴命名为(‘Population of City in 10,000s’)，y轴命名为(‘Profit in \$10,000s’);

``````import numpy as np
import matplotlib.pyplot as plt
X=local_data[:,0]
y=local_data[:,1]#X是一个城市的人口，y是一个城市的流动餐车的利润。
plt.plot(X,y,"o")
plt.xlabel("'Population of City in 10,000s'")
plt.ylabel("Profit in \$10,000s")
plt.show()
``````

• loadtxt函数格式：numpy.loadtxt(fname, dtype=, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0) 其中skiprows是指跳过前“···”行；comment的是指, 如果行的开头为“···”就会跳过该行；delimiter为文件分隔符；

delimiter 加载文件分隔符
comment 指行的开头为“···”时提过改行
skiprows 跳过前“···”行
usecols 加载数据文件中的列索引

2.2 计算代价

computeCost.py

``````import numpy as np

def computeCost(X, y, theta):

# Initialize some useful values
m = len(y) # number of training examples

error = np.dot(X,theta) - y
J = np.dot(error.T, error) / m/2

return J
``````

dot()返回的是两个数组的点积(dot product)

``````import numpy as np
from computeCost import computeCost
def gradientDescent(X, y, theta, alpha, num_iters):

# Initialize some useful values
m = len(y) # number of training examples
J_history = np.zeros((num_iters, 1))

for iter in range(num_iters):

error = np.dot(X,theta) - y
theta = theta - alpha * np.dot(X.T,error) / m

J_history[iter][0] = computeCost(X, y, theta)

return theta, J_history

``````

predict1 = [1, 3.5] * theta
predict2 = [1, 7] * theta

``````predict1 = np.dot(np.array([1, 3.5]),theta)
print('For population = 35,000, we predict a profit of %f\n'%(predict1*10000));
predict2 = np.dot(np.array([1, 7]),theta)
print('For population = 70,000, we predict a profit of %f\n'%(predict2*10000));
``````

ex1.py

``````在这里插入代码片
``````

np.vstack:按垂直方向（行顺序）堆叠数组构成一个新的数组

``````import numpy as np
array_f=np.array([1,2,3])
array_d=np.array([4,5,6])
result=np.vstack((array_f,array_d))
print(result)
>>>[[1 2 3]
[4 5 6]]
``````