import numpy as np
import pandas as pd
import scipy.io as sio
import matplotlib.pyplot as plt
from sklearn.svm import SVC
data=pd.read_csv('ex1data1.txt',names=['Population','Profit'])
data.head()
data=pd.DataFrame(data)
data=(data-data.mean())/data.std()
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]
X=np.matrix(X.values)
y=np.matrix(y.values)
theta=np.matrix(np.array([0,0]))
print("初试theta值的代价为:")
print(computeCost(X,y,theta))
def gradientDecent(X,y,theta,alpha,iters):
tempt=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])
tempt[0,j]=theta[0,j]-((alpha/len(X))*(np.sum(term)))
cost[i]=computeCost(X,y,tempt)
theta=tempt
return theta,cost
alpha=0.01
iters=1500
g,cost=gradientDecent(X,y,theta,alpha,iters)
print("theta:")
print(g)
print(cost)
predict1=(1,7)*g.T
print("预测值1:")
print(predict1)
x=np.linspace(data.Population.min(),data.Population.max(),100)
f=g[0,0]+(g[0,1]*x)
fig,ax=plt.subplots(figsize=(8,6))
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()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data=pd.read_csv('ex1data2.txt',names=['size','bedrooms','price'])
data.head()
data=(data-data.mean())/data.std()
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]
X=np.matrix(X.values)
y=np.matrix(y.values)
theta=np.matrix(np.array([0,0,0]))
def gradientDecent(X,y,theta,alpha,iters):
tempt=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])
tempt[0,j]=theta[0,j]-((alpha/len(X))*(np.sum(term)))
cost[i]=computeCost(X,y,tempt)
theta=tempt
return theta,cost
alpha=0.01
iters=1500
g,cost=gradientDecent(X,y,theta,alpha,iters)
print("theta变量与最小代价")
print(g)
print(cost)
fig=plt.figure()
x=np.linspace(data.size.min(),data.size.max(),100)
y=np.linspace(data.bedrooms.min(),data.bedrooms.max(),100)
f=g[0,0]+(g[0,1]*x)+(g[0,2]*y)
ax=Axes3D(fig)
ax.scatter3D(x,y,f)
ax.plot3D(x,y,f,'gray')
plt.show()