import math
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv('ex1data1.txt',header=None,names = ['Population','Profit'])
data.plot(kind = 'scatter',x = 'Population',y = 'Profit',figsize = (12,8))
'''
绘制散点图
种类:scatter,定义x与y轴所代表数值.
plot.show()
'''
def computeCost(X,y,theta):
inner = np.power(((X*theta.T)-y),2)
return np.sum(inner)/(2*len(X))
data.insert(0,'all_1',1)
cols = data.shape[1]
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]
'''
由于代价函数为numpy类型下的矩阵也就是matrix
我们需要转换xy将他变成matrix
'''
X = np.matrix(X.values)
y = np.matrix(y.values)
print(X)
print(type(y))
'''
theta是一个shape为(1,2)的矩阵
也就是一维二阶矩阵,对应的就是两个变化参数值
'''
theta = np.matrix(np.array([0,0]))
print(computeCost(X,y,theta))
'''
batch gradient decent
'''
print(int(theta.ravel().shape[0]))
'''
def ravel(order='C')
Return a flattened matrix.
order : {'C', 'F', 'A', 'K'}, optional
The elements of `m` are read using this index order. 'C' means to
index the elements in C-like order, with the last axis index
changing fastest, back to the first axis index changing slowest.
'F' means to index the elements in Fortran-like index order, with
the first index changing fastest, and the last index changing
slowest. Note that the 'C' and 'F' options take no account of the
memory layout of the underlying array, and only refer to the order
of axis indexing. 'A' means to read the elements in Fortran-like
index order if `m` is Fortran *contiguous* in memory, C-like order
otherwise. 'K' means to read the elements in the order they occur
in memory, except for reversing the data when strides are negative.
By default, 'C' index order is used.
Returns
ravel()
降维函数,对应参数'F',按列降维.
shape(1)代表获取第一维长度
shape(0)代表获取第二维长度
'''
def gradient_decent(X,y,theta,alpha,iters):
temp = np.matrix(np.zeros(theta.shape))
#创建theta容器,以存放theta
parameters = int(theta.ravel().shape[1])
#创建迭代器,以迭代theta
cost = np.zeros(iters)
#创建代价矩阵,储存errors
for i in range(iters):#1000次迭代
error = (X * theta.T) - y#计算
print('error:',error)
for j in range(parameters):
term = np.multiply(error,X[:,j])#矩阵相乘
temp[0,j] = theta[0,j] - ((alpha/len(X))*np.sum(term))#求cost并且求和,后半部分就是针对各自的theta求偏导
theta = temp#通过矩阵更新,来同时更新对应值.
cost[i] = computeCost(X,y,theta)#对应计算不同的cost值,并返回给对象
return theta,cost
alpha = 0.01
iters = 1000
g,cost = gradient_decent(X,y,theta,alpha,iters)
print(g)
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='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()