赞
踩
通过上述两种方式,我们可以将复杂的方程简单化,维度降低。
-2~2
之间。下面使用python
代码实现的归一化:# 特征归一化 减去平均值并除以对应特征的标准差
s_std = np.std(trans_data_x_data,axis=0) #求标准差
average = np.mean(trans_data_x_data,axis=0) # 求均值
trans_data_x = (trans_data_x_data - average) / s_std # 归一化
mutilinerregress.py
from cgi import test import imp from math import ceil, floor from time import process_time_ns from turtle import dot import numpy as np import matplotlib.pyplot as plt from mutigradient import grandientFun origin_data = np.loadtxt(fname='ex1data2.txt',delimiter=',') features_array = origin_data[:,0:2] yvalue_array = origin_data[:,2] print(np.size(features_array)) print(np.size(yvalue_array)) theta=np.array([0.1,0.2,0.1]) # 训练数据与测试数据区分,np.size()计算的是左右特征的和 trans_data_x_data = features_array[0:int(0.7*(np.size(features_array)/2))] test_data_x = features_array[-ceil(0.3*(np.size(features_array)/2)):] trans_data_y = yvalue_array[0:int(0.7*(np.size(yvalue_array)))] test_data_y = yvalue_array[-ceil(0.3*(np.size(yvalue_array))):] # 特征归一化 减去平均值并除以对应特征的标准差 s_std = np.std(trans_data_x_data,axis=0) average = np.mean(trans_data_x_data,axis=0) trans_data_x = (trans_data_x_data - average) / s_std # 2418295.00404542 grandient_trans_data_x = np.array_split(trans_data_x,15) print(trans_data_x) grandient_trans_data_y = np.array_split(trans_data_y,15) j_error_array = np.array([]) i = 0 j = 0 while True: ones = np.ones((1,int(np.size(grandient_trans_data_x[i])/2)),dtype='int') a =np.insert(grandient_trans_data_x[i],0,values=ones,axis=1) y=np.dot(theta,a.T) error_array = y-trans_data_y[i] # j_error=np.sum(np.power(error_array,2))/(np.size(grandient_trans_data_x[i])) theta = grandientFun(a,error_array,theta=theta) if i==14: j+=1 i=0 # 加一列 1 ,与theta0相乘,不改变theta0的值 error_ones = np.ones((1,int(np.size(trans_data_y))),dtype='int') error_x =np.insert(trans_data_x,0,values=error_ones,axis=1) y=np.dot(theta,error_x.T) error_array_y = y-trans_data_y j_error=np.dot(error_array_y,error_array_y.T)/(np.size(trans_data_y)*2) print(np.size(trans_data_y)) j_error_array=np.concatenate((j_error_array,np.array([j_error])),axis=0) if j==70: #6 break i+=1 # 画出优化过程 index = np.arange(0,np.size(j_error_array)).reshape(np.size(j_error_array)) plt.plot(index,j_error_array) plt.show() fig = plt.figure() ax = fig.gca(projection="3d") # 画出三维的点与拟合的模型所对应的平面 feature_s_std = np.std(features_array,axis=0) feature_average = np.mean(features_array,axis=0) feature_data_x = (features_array - feature_average) / feature_s_std ax.scatter(feature_data_x[:,0],feature_data_x[:,1], zs=yvalue_array, zdir="z", c="#00DDAA", marker="x", s=40) ax.set(xlabel="X1", ylabel="X2", zlabel="y") ones = np.ones((1,int(np.size(feature_data_x)/2)),dtype='int') a =np.insert(feature_data_x,0,values=ones,axis=1) y=np.dot(theta,a.T) print(y-yvalue_array) ax.plot_trisurf(feature_data_x[:,0],feature_data_x[:,1], y, color='g', alpha=1 ) plt.show()
mutigradient.py
import numpy as np
def grandientFun(group_x,error_array,theta):
alpha = 0.008
# theta_feature = theta[1:]
theta = theta - alpha*np.dot(error_array,group_x)/(np.size(error_array))
print('损失为{}'.format(error_array))
print('\ntheta的值为{}'.format(theta))
return theta
3.则我们可以得到下面的拟合图与优化过程图
以上若有问题,请大佬指出,感激不尽,共同进步。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。