- 要理解梯度下降和牛顿迭代法的区别

#include<stdio.h>
// 1. 线性多维函数原型是 y = f(x1,x2,x3) = a * x1 + b * x2 + c * x3
//
// 2. 用牛顿迭代法(或者梯度下降) 对 f(x1, x2, x3) 做求解。
// 即找到一组[a,b,c], 使得: argMin E[(f(x1,x2,x3) - y)^2]。 其中E为sigma。这个loss func就是最小二乘法 int main() { float arr_x1[] = {0.21181724817,0.860486757342,0.956633419758,0.85786906396,0.404354018005,0.738141237944,0.335462707932,0.855141891858,0.176675723263,0.907416965072,0.0647332687491,0.915688359157,0.388378883996,0.551590696253,0.252475234136,0.324609487448,0.591531727445,0.759460513689,0.455169604119,0.0114764793446,0.616362135632,0.520994743026,0.763576588722,0.252922367859,0.818731143784,0.201276597938,0.409109798669,0.837302929696,0.540137128835,0.640703160538,0.261071730719,0.60358418799,0.030230766662,0.375696128756,0.617101348229,0.99638041503,0.453397032368,0.697677962738,0.242718172939,0.694031285076,0.884499352369,0.1246164663,0.906583423555,0.610205156141,0.285249324615,0.443180950782,0.596761799846,0.634037833318,0.719341876615,0.899475824374};
float arr_x2[] = {0.949950993199,0.00348950593906,0.0995940827871,0.181585781352,0.245298785684,0.829835408054,0.46656568642,0.442514659946,0.218085519565,0.175017901553,0.137705361599,0.312323402349,0.570600016776,0.731451385259,0.469180900687,0.91530892778,0.153035033536,0.346216813834,0.469174138374,0.79429539697,0.0755359387891,0.461955557514,0.914175817629,0.208472593729,0.468003630737,0.626639369858,0.253670292305,0.7493809477,0.322969279989,0.0305604977161,0.414165510912,0.450261998842,0.332274387177,0.560607807568,0.947039157859,0.366802949708,0.961887248758,0.13159797726,0.16114436169,0.552001416778,0.820164333967,0.354051649783,0.203087374238,0.573542775689,0.763082181161,0.47376657072,0.727405079739,0.967446971274,0.00134569831871,0.345040334393};
float arr_x3[] = {0.66411293783,0.0107546377332,0.0548870004442,0.720490687668,0.571440908761,0.433915862985,0.884647342299,0.639299914284,0.603025573277,0.419795660433,0.153465995146,0.503899737771,0.286040546608,0.940436883787,0.651074548109,0.745058907863,0.0356272620881,0.894616931387,0.726118880129,0.518993827948,0.786363964595,0.222242295981,0.0338350110881,0.448694787879,0.472714446765,0.266573405515,0.754216764327,0.333970079798,0.950908128673,0.433706942368,0.650219610743,0.0619899568179,0.20517216498,0.183507724831,0.179897813532,0.88001043946,0.935991402921,0.929004108888,0.700231732841,0.326770142533,0.516633979936,0.685462636021,0.88460477795,0.8850575409,0.0368877767664,0.0791417202543,0.970113022457,0.158824081205,0.318974735222,0.504788407121};
float arr_y[] = {3.19986397889,2.59951370428,3.12490306905,3.65818074077,2.27578741609,4.30852373796,2.8246394015,4.09022421593,1.56985189029,3.4930805541,0.624040036155,3.87635573087,2.59304482554,4.05881562909,2.34757909085,3.54990861588,2.11692801283,3.86581396674,3.03088945846,2.14247671464,2.78700502684,2.70984886589,4.15389074944,1.62535051756,3.8650212131,2.12406010007,2.48954342068,4.3446533577,3.2173108431,2.41765575668,2.26257330317,2.77411897078,0.960613946438,2.43186832632,3.92609440302,4.60315121046,4.21998299775,3.28535511407,1.75098266657,3.51380638512,4.81098457321,1.76833993158,4.0108419056,3.86281812345,2.4193713593,2.35659224015,4.21597033336,3.99665270207,2.48028794333,3.89358362917}; double a=1.0,b=1.0,c=1.0;
double lr=0.0005;
int i,j=; while(j<1e6)
{
double temp=0.0,sum=0.0;
double da=0.0,db=0.0,dc=0.0;
for( i=;i<;i++)
{
temp=a*arr_x1[i]+b*arr_x2[i]+c*arr_x3[i]-arr_y[i];
sum+=temp*temp;
da+=temp**arr_x1[i];
db+=temp**arr_x2[i];
dc+=temp**arr_x3[i];
}
a=a-lr*da;
b=b-lr*db;
c=c-lr*dc; j++;
if(j%==)
printf("iter: %d ;sum :%f\n",j,sum); }
printf("%f,%f,%f\n",a,b,c);
}

-  第一题就二维网格上A->B的路径数量,其中有mask限制!

2018.08.28 ali 梯度下降法实现最小二乘的更多相关文章

  1. 2018.08.28 洛谷P4556 [Vani有约会]雨天的尾巴(树上差分+线段树合并)

    传送门 要求维护每个点上出现次数最多的颜色. 对于每次修改,我们用树上差分的思想,然后线段树合并统计答案就行了. 注意颜色很大需要离散化. 代码: #include<bits/stdc++.h& ...

  2. 2018.08.28 codeforces600E(dsu on tree)

    传送门 一道烂大街的dsu on tree板题. 感觉挺有趣的^_^ 代码真心简单啊! 就是先处理轻儿子,然后处理重儿子,其中处理轻儿子后需要手动消除影响. 代码: #include<bits/ ...

  3. 2018.08.28 洛谷P3803 【模板】多项式乘法(FFT)

    传送门 fft模板题. 终于学会fft了. 这个方法真是神奇! 经过试验发现手写的complex快得多啊! 代码: #include<iostream> #include<cstdi ...

  4. 2018.08.28 洛谷P4360 [CEOI2004]锯木厂选址(斜率优化dp)

    传送门 一道斜率优化dp入门题. 是这样的没错... 我们用dis[i]表示i到第三个锯木厂的距离,sum[i]表示前i棵树的总重量,w[i]为第i棵树的重量,于是发现如果令第一个锯木厂地址为i,第二 ...

  5. 2018.08.28 洛谷P3345 [ZJOI2015]幻想乡战略游戏(点分树)

    传送门 题目就是要求维护带权重心. 因此破题的关键点自然就是带权重心的性质. 这时发现直接找带权重心是O(n)的,考虑优化方案. 发现点分树的树高是logn级别的,并且对于以u为根的树,带权重心要么就 ...

  6. 2018.08.28 集合堆栈机(模拟+STL)

    描述 中学数学里集合的元素往往是具体的数字,比如A = {1,2,3},B = {}(空集)等等.但是要特别注意,集合的元素也可以是另一个集合,比如说C = {{}},即说明C有且仅有一个元素--空集 ...

  7. 机器学习---用python实现最小二乘线性回归算法并用随机梯度下降法求解 (Machine Learning Least Squares Linear Regression Application SGD)

    在<机器学习---线性回归(Machine Learning Linear Regression)>一文中,我们主要介绍了最小二乘线性回归算法以及简单地介绍了梯度下降法.现在,让我们来实践 ...

  8. 最小二乘法 及 梯度下降法 运行结果对比(Python版)

    上周在实验室里师姐说了这么一个问题,对于线性回归问题,最小二乘法和梯度下降方法所求得的权重值是一致的,对此我颇有不同观点.如果说这两个解决问题的方法的等价性的确可以根据数学公式来证明,但是很明显的这个 ...

  9. 简单线性回归(梯度下降法) python实现

    grad_desc .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bord ...

随机推荐

  1. leetcode 两数之和 python

      两数之和     给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 1 ...

  2. [BZOJ3757]苹果树(树上莫队)

    树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...

  3. Manthan, Codefest 18 (Div 1 + Div 2) (A~E)

    目录 Codeforces 1037 A.Packets B.Reach Median C.Equalize D.Valid BFS E.Trips(正难则反) Codeforces 1037 比赛链 ...

  4. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造

    E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...

  5. hdu 5783 Divide the Sequence 贪心

    Divide the Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  6. mongodb用mongoose查库的对象,不能增加属性

    node + koa2 + mongodb 写了一个给前端的接口 如果不是写这个接口,这辈子都发现不了mongodb里这个大坑 mongoose 是个ODM(Object Document Mappe ...

  7. MikroTik RouterOS 5.x使用HunterTik 2.3.1进行破解

    一.加载光驱: 二.一路回车: 三.说明: 1.可以不安装Debian内核,但如果在无缝升级到6.6的版本,此项就一定要选择. 2.6版本的破解必须小于等于1G的空间,不然无法破解成功,亲测有效,如果 ...

  8. PHP上传文件大小限制的问题(转)

      在用PHP进行文件上传的操作中,需要知道怎么控制上传文件大小的设置,而文件可传大小是受到多种因素制约的,现总结如下:1.php.ini:upload_max_filesize 所上传的文件的最大大 ...

  9. C#快速找出磁盘内的所有文件

    本文只针对NTFS格式化的磁盘文件快速检索,速度不是非常快,是让你震惊. 一般用文件遍历的方法检索一个50G的文件夹需要几十分钟甚至一个小时的时间,而用本方法只需几秒. using System; u ...

  10. 使用CefSharp在.Net程序中嵌入Chrome浏览器(八)——Cookie

    CEF中的Cookie是通过CookieManager来管理的,可以用它来设置发送的Cookie. 发送Cookie 发送Cookie的一个基本示例如下: var cookieManager = _c ...