data = pd.read_csv("ex1data1.txt", names=['x','y'])
data.insert(0,"Ones",1)
n = data.shape[0]
m = data.shape[1]-1
X = np.array(data.iloc[:,:-1])
y = np.array(data.y).reshape(-1,1)
data
plt.scatter(X[:,-1],y)
<matplotlib.collections.PathCollection at 0x7f3d028c97e0>
导入ML-ex1data2数据
'''
data = pd.read_csv("ex1data2.txt", names=['x1', 'x2', 'y'])
data.insert(0, "Ones", 1)
n = data.shape[0]
m = data.shape[1]-1
X = np.array(data.iloc[:,:-1])
y = np.array(data.y).reshape(-1,1)
n,m,X,y
plt.scatter(X[:,-2],y)
'''
损失函数
defcompterCost(X, theta, y):
delta = X @ theta - y
return delta.T @ delta
compterCost(X, np.zeros([m,1]), y)