line_fit
来源:互联网 发布:佳能正品服务软件下载 编辑:程序博客网 时间:2024/06/16 10:37
#include <stdio.h>#include <stdlib.h>#include <math.h>#define POINTS_NUM 6typedef struct {double x;double y;}typePoints;typePoints points[POINTS_NUM] = {{0 , 0.002},{0.1 , 0.082},{0.2 , 0.162},{0.5 , 0.407},{1 , 0.805},{1.5 , 1.185}};/* * 最小二乘法直线拟合(不是常见的一元线性回归算法) * 将离散点拟合为 a x + b y + c = 0 型直线 * 假设每个点的 X Y 坐标的误差都是符合 0 均值的正态分布的。 * 与一元线性回归算法的区别:一元线性回归算法假定 X 是无误差的,只有 Y 有误差。 */void lineFit(double *a , double *b , double *c){ int i; double x_mean = 0; double y_mean = 0; for(i = 0; i < POINTS_NUM; i++) { x_mean += points[i].x; y_mean += points[i].y; } x_mean /= POINTS_NUM; y_mean /= POINTS_NUM; //至此,计算出了 x y 的均值 double Dxx = 0, Dxy = 0, Dyy = 0; for(i = 0; i < POINTS_NUM; i++) { Dxx += (points[i].x - x_mean) * (points[i].x - x_mean); Dxy += (points[i].x - x_mean) * (points[i].y - y_mean); Dyy += (points[i].y - y_mean) * (points[i].y - y_mean); } double lambda = ( (Dxx + Dyy) - sqrt( (Dxx - Dyy) * (Dxx - Dyy) + 4 * Dxy * Dxy) ) / 2.0; double den = sqrt( Dxy * Dxy + (lambda - Dxx) * (lambda - Dxx) ); *a = Dxy / den; *b = (lambda - Dxx) / den; *c = - (*a) * x_mean - (*b) * y_mean;} int main(int argc, char *argv[]) {double a , b , c;lineFit(&a,&b,&c);printf("slope = %f;intercept = %f",(-a/b),(-c/b));system("pause");return 0;}
0 0
- line_fit
- 231. Power of Two
- spring请求数据与返回数据日志记录
- Linux软件包管理
- 获取系统正在运行的应用-iOS
- OpenGL -- 实现走动的时钟
- line_fit
- 借船过河:一个据说能看穿你的人性和欲望的心理测试
- caffe的mnist格式数据生成和mnist格式数据转npy
- 配置什么都没有的精简ubuntu系统(修改apt-get默认源)
- 【URAL 1513】Lemon Tale(DP+高精度)
- php使用socket获取远程图片
- Android stdio-编译 卡在Refreshing Gradle project
- xml、json、protobuf、thrift与avro
- ssh隧道