import numpy as np
from sympy import *
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
A = np.eye(5)
A
单变量线性回归
path = "ex1data1.txt"
data = pd.read_csv(path,header=None,names=["population","profit"])
data.head()
| population | profit |
---|
0 | 6.1101 | 17.5920 |
---|
1 | 5.5277 | 9.1302 |
---|
2 | 8.5186 | 13.6620 |
---|
3 | 7.0032 | 11.8540 |
---|
4 | 5.8598 | 6.8233 |
---|
data.plot(kind="scatter",x='population',y='profit',figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x11b133828>
def computecost(X,y,theta):
inner=np.power((X*theta.T-y),2)
return np.sum(inner)/(2*len(X))
data.insert(0,'Ones',1)
cols = data.shape[1]
X = data.iloc[:,:-1]
y = data.iloc[:,cols-1:cols]
y2 = data.iloc[:,-1]
X=np.matrix(X.values)
y=np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
X.shape,y.shape,theta.shape
((97, 2), (97, 1), (1, 2))
computecost(X,y,theta)
32.072733877455676
def gradientDescent(X,y,theta,alpha,iters):
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.ravel().shape[1])
cost = np.zeros(iters)
for i in range(iters):
error = (X * theta.T) - y
for j in range(parameters):
term = np.multiply(error,X[:,j])
temp[0,j] = theta[0,j] - ((alpha/len(X))*np.sum(term))
theta = temp
cost[i] = computecost(X,y,theta)
return theta,cost
alpha = 0.01
iters=1500
g,cost = gradientDescent(X,y,theta,alpha,iters)
X = list(range(len(cost)))
plt.plot(X,cost)
[<matplotlib.lines.Line2D at 0x11aa4e128>]
g
matrix([[-3.63029144, 1.16636235]])
predict1 = [1,3.5]*g.T
print("predict1:",predict1)
predict2 = [1,7]*g.T
print("predict2:",predict2)
predict1: [[0.45197679]]
predict2: [[4.53424501]]
x = np.linspace(data.population.min(),data.population.max(),100)
f = g[0,0]+g[0,1]*x
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,f,'r',label='prediction')
ax.scatter(data.population,data.profit,label='training data')
ax.legend(loc=2)
ax.set_xlabel('population')
ax.set_ylabel('profit')
ax.set_title('predicted profit vs. population size')
plt.show()
多变量线性回归
path2="ex1data2.txt"
data2 = pd.read_csv(path2,header=None,names=["Size","Bedrooms","Price"])
data2.head()
| Size | Bedrooms | Price |
---|
0 | 2104 | 3 | 399900 |
---|
1 | 1600 | 3 | 329900 |
---|
2 | 2400 | 3 | 369000 |
---|
3 | 1416 | 2 | 232000 |
---|
4 | 3000 | 4 | 539900 |
---|
data2 = (data2 - data2.mean())/data2.std()
data2.head()
| Size | Bedrooms | Price |
---|
0 | 0.130010 | -0.223675 | 0.475747 |
---|
1 | -0.504190 | -0.223675 | -0.084074 |
---|
2 | 0.502476 | -0.223675 | 0.228626 |
---|
3 | -0.735723 | -1.537767 | -0.867025 |
---|
4 | 1.257476 | 1.090417 | 1.595389 |
---|
cols = data2.shape[1]
X2 = data2.iloc[:,0:cols-1]
y2 = data2.iloc[:,cols-1:cols]
X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))
data2.head()
| Ones | Size | Bedrooms | Price |
---|
0 | 1 | 0.130010 | -0.223675 | 0.475747 |
---|
1 | 1 | -0.504190 | -0.223675 | -0.084074 |
---|
2 | 1 | 0.502476 | -0.223675 | 0.228626 |
---|
3 | 1 | -0.735723 | -1.537767 | -0.867025 |
---|
4 | 1 | 1.257476 | 1.090417 | 1.595389 |
---|
g2,cost2 = gradientDescent(X2,y2,theta2,alpha,iters)
g2
matrix([[-1.10856950e-16, 8.84042349e-01, -5.24551809e-02]])
X2 = list(range(len(cost2)))
plt.plot(X2,cost2)
[<matplotlib.lines.Line2D at 0x11ae4cf60>]
data2.head()
| Ones | Size | Bedrooms | Price |
---|
0 | 1 | 0.130010 | -0.223675 | 0.475747 |
---|
1 | 1 | -0.504190 | -0.223675 | -0.084074 |
---|
2 | 1 | 0.502476 | -0.223675 | 0.228626 |
---|
3 | 1 | -0.735723 | -1.537767 | -0.867025 |
---|
4 | 1 | 1.257476 | 1.090417 | 1.595389 |
---|
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.linspace(data2.Size.min(),data2.Size.max(),100)
Y = np.linspace(data2.Bedrooms.min(),data2.Bedrooms.max(),100)
X, Y = np.meshgrid(X, Y)
Z = g2[0,0]+g2[0,1]*X+g2[0,2]*Y
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
ax.contourf(X,Y,Z,zdir='z',offset=-2)
ax.set_zlim(-2,2)
x = data2.iloc[:,1:cols-2]
y = data2.iloc[:,2:cols-1]
z = data2.iloc[:,cols-1:cols]
x = np.matrix(x.values)
y = np.matrix(y.values)
z = np.matrix(z.values)
ax.scatter(x, y, z, marker='.', s=50, label='')
plt.show()
def normalEqn(X,y):
theta = np.linalg.inv(X.T@X)@X.T@y
return theta
final_theta2 = normalEqn(X,y)
final_theta2
matrix([[-3.89578088],
[ 1.19303364]])