from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def cost_function(X,Y,weight,bias): #损失函数 均方差
y_hat=weight*X+bias
l=y_hat-Y
cost=np.sum(l**2)/2*len(X)
return cost
def s(train): #归一化
min=train.min(axis=0)
max=train.max(axis=0)
gap=max-min
train-=min
train/=gap
return train
def gradient_descent(X, y, w, b, lr, iterations): # 定义一个实现梯度下降的函数\n
l_history = np.zeros(iterations) # 初始化记录梯度下降过程中损失的数组\n
w_history = np.zeros(iterations) # 初始化记录梯度下降过程中权重的数组\n
b_history = np.zeros(iterations) # 初始化记录梯度下降过程中偏置的数组
for iter in range(iterations): # 进行梯度下降的迭代,就是下多少级台阶\n
y_hat = w*X + b # 这个是向量化运行实现的假设函数\n
loss = y_hat-y # 这是中间过程,求得的是假设函数预测的y和真正的y值之间的差值\n
derivative_weight = X.T.dot(loss)/len(X)*2 # 对权重求导,len(X)就是数据集样本数N\n
derivative_bias = sum(loss)*1/len(X)*2 # 对偏置求导,len(X)就是数据集样本数N\n
w = w - lr*derivative_weight # 结合下降速率alpha更新权重\n
b = b - lr*derivative_bias # 结合下降速率alpha更新偏置\n
l_history[iter] = cost_function(X, y, w,b) # 梯度下降过程中损失的历史 \n
w_history[iter] = w # 梯度下降过程中权重的历史\n
b_history[iter] = b # 梯度下降过程中偏置的历史\n
return l_history, w_history, b_history # 返回梯度下降过程数据",
f="/home/aistudio/data/data20465/advertising.csv"
df=pd.read_csv(f)
df.head()
sns.heatmap(df.corr(),annot=True)
df.corr()
sns.pairplot(df,
x_vars=['wechat','weibo','others'],
y_vars='sales',
kind='scatter')
plt.show()
X=np.array(df.wechat)
Y=np.array(df.sales)
print("张量X的阶%d"%X.ndim)
print("张量X的形状%d"%X.shape)
X=X.reshape(len(X),1)
Y=Y.reshape(len(Y),1)
X_train,X_test,Y_train,Y_text=train_test_split(X,Y,test_size=0.2)
X_train=s(X_train)
Y_train=s(Y_train)
plt.plot(X_train,Y_train,'r.',label='trainning data')
plt.xlabel('weichat')
plt.ylabel('sales')
plt.legend()
plt.show()
w=5
b=3
lr=0.01
iter=1000
l_history, w_history, b_history=gradient_descent(X_train,Y_train,w,b,lr,iter)
#print(l_history)
plt.plot(l_history,'g--',label='loss curve')
#plt.xlabel('lter')
#plt.ylabel('loss')
plt.legend()
plt.show()
线性回归
最新推荐文章于 2024-06-06 23:15:00 发布