机器学习 线性回归 梯度下降 python实现

贴代码,闪人

import pandas as pd
import seaborn as sns
sns.set(context="notebook", style="whitegrid", palette="dark")
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#输入数据可视化
sns.set(context='notebook', style='whitegrid', palette='dark')
#sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
data = pd.read_csv('ex1data1.txt', names=['population','profit'])

data.head()
data.info()
sns.lmplot('population','profit',data,size=10,fit_reg=True)
plt.show()

#从输入得到X矩阵
def getX(df):
    ones = pd.DataFrame({'ones': np.ones(len(df))})
    data = pd.concat([ones, df], axis=1)
    return data.iloc[:, :-1].values

#从输入得到y矩阵
def gety(df):
    return np.array(df.iloc[:, -1])

#代价函数
def cost(theta, X, y):
    m = X.shape[0]
    inner=X@theta-y#@表示向量内积
    cost = (inner.T @ inner)/(2*m)
    return cost

#梯度下降                       
def gradientDescent(theta,X,y):
    m=X.shape[0]
    return (X.T @ ( X @ theta - y))/m

#循环调用
def batchDo(theta, X, y, epoch,learningRate):
    costData=[cost(theta,X,y)]
    for i in range(epoch):
        theta = theta - learningRate * gradientDescent(theta,X,y)
        costData.append(cost(theta,X,y))
    
    return theta,costData


data = pd.read_csv('ex1data1.txt', names=['population', 'profit'])
data.head()
np.random.rand()

#初始化数据
X=getX(data)
print(X.shape,type(X))
y=gety(data)
print(y.shape,type(y))
theta=np.random.rand(X.shape[1])
epoch=400
learningRate=0.01
theta,costData=batchDo(theta,X,y,epoch,learningRate)#调用梯度下降缩小theta

#结果可视化
a=sns.tsplot(costData,time=np.arange(epoch+1))
a.set_xlable('epoch')
a.set_ylable('cost')
plt.show()
b = theta[0]
m = theta[1]

plt.scatter(data.population, data.profit, label="Training data")
plt.plot(data.population, data.population*m + b, label="Prediction")
plt.legend(loc=2)
plt.show()

#正规方程
def normalEquation(X, y):
    theta = np.linalg.inv(X.T@X)@X.T@y
    return theta

print(normalEquation(X,y))
print(cost(normalEquation(X,y),X,y))

 

你可能感兴趣的:(人工智能)