当前位置: 首页 > news >正文

岳阳博物馆网站wordpress 取消边栏

岳阳博物馆网站,wordpress 取消边栏,关于建设网站的通知,湘西网站建设🚀个人主页:为梦而生~ 关注我一起学习吧! 💡专栏:机器学习python实战 欢迎订阅!后面的内容会越来越有意思~ ⭐内容说明:本专栏主要针对机器学习专栏的基础内容进行python的实现,部分…

🚀个人主页:为梦而生~ 关注我一起学习吧!
💡专栏:机器学习python实战 欢迎订阅!后面的内容会越来越有意思~
内容说明:本专栏主要针对机器学习专栏的基础内容进行python的实现,部分基础知识不再讲解,有需要的可以点击专栏自取~
💡往期推荐(机器学习基础专栏)
【机器学习基础】机器学习入门(1)
【机器学习基础】机器学习入门(2)
【机器学习基础】机器学习的基本术语
【机器学习基础】机器学习的模型评估(评估方法及性能度量原理及主要公式)
【机器学习基础】一元线性回归(适合初学者的保姆级文章)
【机器学习基础】多元线性回归(适合初学者的保姆级文章)
本期内容:针对以上的一元和多元线性回归的梯度下降求解方法,进行代码展示


文章目录

  • 一元线性回归
  • 多元线性回归


一元线性回归

  • 设计思路

首先,class LinearRegression(object):定义一个LinearRegression类,继承自object类。
在这个类中,首先def __init__(self):定义类的构造函数

在构造函数中,初始化线性回归模型的参数self.__Mself.__theta0self.__theta1,以及梯度下降中的步长(学习率)self.__alpha

在这里插入图片描述

线性回归模型是要不断计算输出的,所以定义函数def predict(self, x),用于预测给定输入x对应的输出

同时线性回归的目的是通过迭代,不断的修改参数 θ \theta θ,所以需要定义函数用来做这个工作,它是通过梯度下降的方法来求解的,所以def __cost_theta0(self, X, Y)def __cost_theta1(self, X, Y)这两个方法用于计算代价函数关于参数 θ 0 \theta_0 θ0 θ 1 \theta_1 θ1的偏导数

下面,def train(self, features, target)把上面的每个步骤和到了一起,定义了一个训练方法train,用于通过梯度下降算法找到最优的模型参数 θ 0 \theta_0 θ0 θ 1 \theta_1 θ1,使得代价函数的平方误差最小。在训练过程中,通过迭代更新参数,并输出每次迭代后的参数值

while的每一次迭代中,通过更新参数self.__theta0self.__theta1来逐渐最小化代价函数的平方误差。

if "0:o.5f".format(prevt0) == "0:o.5f".format(self.__theta0) and "0:o.5f".format(prevt1) == "0:o.5f".format(self.__theta1):判断是否达到收敛条件,即两次迭代的参数值没有改变,如果满足条件,则退出循环。

最后,输出最终得到的参数值。

在这里插入图片描述

  • 总体代码实现

定义LinearRegression的class

#!/usr/bin/env python3
# 这是一个Shebang,指定了此脚本要使用的解释器为python3。
import numpyclass LinearRegression(object):# Constructor. Initailize Constants.def __init__(self):super(LinearRegression, self).__init__()self.__M = 0self.__theta0 = 2self.__theta1 = 2# defining Alpha I.E length of steps in gradient descent Or learning Rate.self.__alpha = 0.01def predict(self,x):return (self.__theta0 + x * self.__theta1)# Cost Function fot theta0.def __cost_theta0(self,X,Y):sqrerror = 0.0for i in range(0,X.__len__()):sqrerror += (self.predict(X[i]) - Y[i])return (1/self.__M * sqrerror)# Cost Function fot theta1.def __cost_theta1(self,X,Y):sqrerror = 0.0for i in range(0,X.__len__()):sqrerror += (self.predict(X[i]) - Y[i]) * X[i]return (1/self.__M * sqrerror)# training Data :# Finding Best __theta0 and __theta1 for data such that the Squared  Error is Minimized.def train(self,features,target):# Validate Dataif not features.__len__() == target.__len__():raise Exception("features and target should be of same length")# Initailize M with Size of X and Yself.__M = features.__len__()# gradient descentprevt0, prevt1 = self.__theta0 , self.__theta1while True:tmp0 = self.__theta0 - self.__alpha * (self.__cost_theta0(features,target))tmp1 = self.__theta1 - self.__alpha * (self.__cost_theta1(features,target))self.__theta0, self.__theta1 = tmp0, tmp1print("theta0(b) :", self.__theta0)print("theta1(m) :", self.__theta1)if "0:o.5f".format(prevt0) == "0:o.5f".format(self.__theta0) and "0:o.5f".format(prevt1) == "0:o.5f".format(self.__theta1):breakprevt0, prevt1 = self.__theta0 , self.__theta1# Training Completed.# log __theta0 __theta1print("theta0(b) :", self.__theta0)print("theta1(m) :", self.__theta1)

样例测试

from LinearRegression_OneVariables import LinearRegression
import numpy as npX = np.array([1,2,3,4,5,6,7,8,9,10])# Y = 0 + 1X
Y = np.array([1,2,3,4,5,6,7,8,9,10])modal = LinearRegression.LinearRegression()modal.train(X,Y)print(modal.predict(14))

多元线性回归

  • 设计思路

首先,将文件导入,打乱顺序并选择训练集。

data=pd.read_csv("c:\\windquality.csv")data_array=data.values#shuffling for train test spplit
np.random.shuffle(data_array)train,test=data_array[:1200,:],data_array[1200:,:]
x_train=train[:,:-1]
x_test=test[:,:-1]
y_train=train[:,-1]
y_test=test[:,-1]

在这里插入图片描述

然后初始化参数,注意这里是多元的,所以有多个参数需要初始化。包括迭代次数和学习率

coef1=0
coef2=0
coef3=0
coef4=0
coef5=0
coef6=0
coef7=0
coef8=0
coef9=0
coef10=0
coef11=0
epoch=1000
alpha=.0001

在这里插入图片描述

然后使用梯度下降算法进行计算

总体代码实现

import pandas as pd
import numpy as np
import matplotlib.pyplot as pltdata=pd.read_csv("c:\\windquality.csv")data_array=data.values#shuffling for train test spplit
np.random.shuffle(data_array)train,test=data_array[:1200,:],data_array[1200:,:]
x_train=train[:,:-1]
x_test=test[:,:-1]
y_train=train[:,-1]
y_test=test[:,-1]coef1=0
coef2=0
coef3=0
coef4=0
coef5=0
coef6=0
coef7=0
coef8=0
coef9=0
coef10=0
coef11=0
epoch=1000
alpha=.0001
c=0
n=len(y_train)
for i in range(epoch):y_pred=((coef1*x_train[:,0])+(coef2*x_train[:,1])+(coef3*x_train[:,2])+(coef4*x_train[:,3])+(coef5*x_train[:,4])+(coef6*x_train[:,5])+(coef7*x_train[:,6])+(coef8*x_train[:,7])+(coef9*x_train[:,8])+(coef10*x_train[:,9])+(coef11*x_train[:,10])+c)#to predict drivativeintercept=(-1/n)*sum(y_train-y_pred)dev1=(-1/n)*sum(x_train[:,0]*(y_train-y_pred))dev2=(-1/n)*sum(x_train[:,1]*(y_train-y_pred))dev3=(-1/n)*sum(x_train[:,2]*(y_train-y_pred))dev4=(-1/n)*sum(x_train[:,3]*(y_train-y_pred))dev5=(-1/n)*sum(x_train[:,4]*(y_train-y_pred))dev6=(-1/n)*sum(x_train[:,5]*(y_train-y_pred))dev7=(-1/n)*sum(x_train[:,6]*(y_train-y_pred))dev8=(-1/n)*sum(x_train[:,7]*(y_train-y_pred))dev9=(-1/n)*sum(x_train[:,8]*(y_train-y_pred))dev10=-1/n*sum(x_train[:,9]*(y_train-y_pred))dev11=-1/n*sum(x_train[:,10]*(y_train-y_pred))#linec=c-alpha*interceptcoef1=coef1-alpha*dev1coef2=coef2-alpha*dev2coef3=coef3-alpha*dev3coef4=coef4-alpha*dev4coef5=coef5-alpha*dev5coef6=coef6-alpha*dev6coef7=coef7-alpha*dev7coef8=coef8-alpha*dev8coef9=coef9-alpha*dev9coef10=coef10-alpha*dev10coef11=coef11-alpha*dev11print("\nintercept:",c,"\ncoefficient1:",coef1,"\ncoefficient2:",coef2,"\ncoefficient3:",coef3,"\ncoefficient4:",coef4,"\ncoefficient5:",coef5,"\ncoefficient6:",coef6,"\ncoefficient7:",coef7,"\ncoefficient8:",coef8,"\ncoefficient9:",coef9,"\ncoefficient10",coef10,   "\ncoefficient11",coef11)#Calculating the predicted values
predicted_values = []
for i in range(0,399):y_pred = ((coef1 * x_test[i,0]) + (coef2 * x_test[i,1]) + (coef3 * x_test[i,2]) + (coef4 * x_test[i,3]) + (coef5 * x_test[i,4]) + (coef6 * x_test[i,5]) + (coef7 * x_test[i,6]) + (coef8 * x_test[i,7]) + (coef9 * x_test[i,8]) + (coef10 * x_test[i,9]) + (coef11 * x_test[i,10]) + intercept)predicted_values.append(y_pred)for i in range(len(predicted_values)):print(predicted_values[i])
http://www.yayakq.cn/news/319792/

相关文章:

  • 衡水制作网站重庆免费建站公司地址
  • 昆明软件开发公司做门户网站的广东省网站集约化建设方案
  • 做网站1000以下哪家好天眼通查公司查询入口
  • 运输 织梦网站模板域名购买一般多少钱
  • 安阳给商家做网站推广教务管理系统登录入口官网
  • 微信企业号可以做微网站吗遵义网站建设gzyhg
  • 有没有可以做游戏的网站吗哪些网站是做外贸生意的
  • 网站添加地图导航网站设置为应用程序
  • 巩义网站优化培训无锡找做网站公司
  • 做网站要钱嘛videopro wordpress
  • 网站开发费摊销多少年rsd wordpress
  • 台山网站设计wap蓝天建站
  • win7本地做网站网络工程师需要学哪些知识
  • 创建个人网站怎么做门户网站网站制作
  • 网站建设预算描述域名提供商
  • 萧山做网站的公司软件开发公司介绍
  • h5个人网站模板梧州论坛 肇庆
  • 智联招聘网站怎么做两份简历山东前网站建设
  • 怎么做赛事直播网站wordpress默认的h1标签放在哪里
  • 天津seo网站推广江西建设城乡网站查询
  • 电子商务网站开发实例手把手制作公司网站
  • 长春网站建设同信峰聘网360建筑网
  • 网站设计教科书接洽网页设计公司
  • 百度霸屏推广做网站与全网营销搜索推广排名优化
  • 坑梓网站建设代理商wordpress 常用的钩子
  • wordpress 整站移植做网站专用素材
  • 太原网站如何制作网站安全架构
  • 做网站备负责人风险大吗广西网络推广公司
  • 网站空间服务商网站建议反馈应该怎么做
  • 网上商店网站设计南阳建设重要区域中心城市网站