C++ LinearRegression代码实现
这里基本完全参考网络资源完成,有疑问欢迎留言!
LinearRegression.h
#pragma once
#ifndef ML_LINEAEEEGRESSION_H
#define ML_LINEARREGRESSION_H
class LinearRegression {
public:
/*特征*/
double *x;
/*预测值*/
double *y;
/*样本数量*/
int m;
/*系数*/
double *theta;
/*创建实例*/
LinearRegression(double x[], double y[], int m);
/*训练 */
void train(double alpha, int iterations);
/*预测*/
double predict(double x);
private:
/*计算损失模型*/
static double compute_cost(double x[], double y[], double theta[], int m);
/*计算单个预测值*/
static double h(double x, double theta[]);
/*预测*/
static double *calculate_predictions(double x[], double theta[], int m);
/*梯度下降*/
static double *gradient_descent(double x[], double y[], double alpha, int iter, double *j, int m); };
#endif // !ML_LINEAEEEGRESSION_H
LinearRegression.cpp
#include "iostream"
#include "linearRegression.h"
#include "Utils.h"
using namespace std; /*初始化*/
LinearRegression::LinearRegression(double x[], double y[], int m)
{
this->x = x;
this->y = y;
this->m = m;
} /*
alpha:learn rate
iterations:iterators
*/
void LinearRegression::train(double alpha, int iterations)
{
double *J = new double[iterations];
this->theta = gradient_descent(x, y, alpha, iterations, J, m);
cout << "J=";
for (int i = ; i < iterations; ++i)
{
cout << J[i] << " " << endl;;
}
cout << "\n" << "Theta: " << theta[] << " " << theta[] << endl;
}
/*预测*/
double LinearRegression::predict(double x)
{
cout << "y':" << h(x, theta) << endl;
return h(x, theta);
} /*计算损失模型*/
double LinearRegression::compute_cost(double x[], double y[], double theta[], int m)
{
double *predictions = calculate_predictions(x, theta, m);
double *diff = Utils::array_diff(predictions, y, m);
double *sq_errors = Utils::array_pow(diff, m, );
return (1.0 / ( * m))*Utils::array_sum(sq_errors, m);
}
/*计算单个预测值*/
double LinearRegression::h(double x, double theta[])
{
return theta[] + theta[] * x;
}
/*预测*/
double *LinearRegression::calculate_predictions(double x[], double theta[], int m)
{
double *predictions = new double[m];
for (int i = ; i < m; i++)
{
predictions[i] = h(x[i], theta);
}
return predictions;
}
/*梯度下降*/
double *LinearRegression::gradient_descent(double x[], double y[], double alpha, int iter, double *J, int m)
{
double *theta = new double[];
theta[] = ;
theta[] = ;
for (int i = ; i < iter; i++)
{
double *predictions = calculate_predictions(x, theta, m);
double *diff = Utils::array_diff(predictions, y, m);
double *error_x1 = diff;
double *error_x2 = Utils::array_multiplication(diff, x, m);
/*这里可以设定J损失函数的阈值,也可以设定梯度变化量的阈值*/
theta[] = theta[] - alpha*(1.0 / m) * Utils::array_sum(error_x1, m);
theta[] = theta[] - alpha*(1.0 / m)*Utils::array_sum(error_x2, m);
J[i] = compute_cost(x, y, theta, m);
}
return theta;
}
Test.cpp
#include "iostream"
#include "linearRegression.h" using namespace std; int main()
{
double x[] = {,,,,};
double y[] = {,,,,}; LinearRegression test(x,y,);
test.train(0.1, );
test.predict();
system("pause");
return ;
}
C++ LinearRegression代码实现的更多相关文章
- 代码-Weka的LinearRegression类
package kit.weka; import weka.classifiers.Evaluation; import weka.classifiers.functions.LinearRegres ...
- TensorFlow——LinearRegression简单模型代码
代码函数详解 tf.random.truncated_normal()函数 tf.truncated_normal函数随机生成正态分布的数据,生成的数据是截断的正态分布,截断的标准是2倍的stddev ...
- 建模分析之机器学习算法(附python&R代码)
0序 随着移动互联和大数据的拓展越发觉得算法以及模型在设计和开发中的重要性.不管是现在接触比较多的安全产品还是大互联网公司经常提到的人工智能产品(甚至人类2045的的智能拐点时代).都基于算法及建模来 ...
- TensorFlow实现线性回归模型代码
模型构建 1.示例代码linear_regression_model.py #!/usr/bin/python # -*- coding: utf-8 -* import tensorflow as ...
- Spark MLlib线性回归代码实现及结果展示
线性回归(Linear Regression)是利用称为线性回归方程的最小平方函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析. 这种函数是一个或多个称为回归系数的模型参数的线性组合.只有 ...
- 10 种机器学习算法的要点(附 Python 和 R 代码)
本文由 伯乐在线 - Agatha 翻译,唐尤华 校稿.未经许可,禁止转载!英文出处:SUNIL RAY.欢迎加入翻译组. 前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关 ...
- Python机器学习/LinearRegression(线性回归模型)(附源码)
LinearRegression(线性回归) 2019-02-20 20:25:47 1.线性回归简介 线性回归定义: 百科中解释 我个人的理解就是:线性回归算法就是一个使用线性函数作为模型框架($ ...
- spark LinearRegression 预测缺失字段的值
最近在做金融科技建模的时候,字段里面很多缺少值得时候,模型对于新用户的预测会出现很大的不稳定,即PSI较大的情况. 虽然我们依据字段IV值得大小不断的在调整字段且开发新变量,但是很多IV值很大的字段直 ...
- <转>机器学习系列(9)_机器学习算法一览(附Python和R代码)
转自http://blog.csdn.net/han_xiaoyang/article/details/51191386 – 谷歌的无人车和机器人得到了很多关注,但我们真正的未来却在于能够使电脑变得更 ...
随机推荐
- java 中的运算符
Java的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符. 算数运算符():+ - * / % ++ -- 关系运算符():== != > >= < <= 逻 ...
- LintCode之两两交换链表中的节点
题目描述: 我的思路: 由题目描述可知,题目是要求将第一个与第二个节点,第三个与第四节点....进行交换,而进行交换时只用将节点的值进行交换即可.需要注意的是:当链表为null或者当链表只有一个节点时 ...
- cmath模块——复数域数学函数模块
cmath——复数域数学函数模块 转自:https://blog.csdn.net/zhtysw/article/category/7511293 该模块属于内置模块,随时可以调用.它提供了数学函数在 ...
- $_POST,$_GET,$_REQUEST区分
PHP $_REQUEST PHP $_REQUEST 用于收集 HTML 表单提交的数据. 下面的例子展示了一个包含输入字段及提交按钮的表单.当用户通过点击提交按钮来提交表单数据时, 表单数据将发送 ...
- springboot异步任务、定时任务
打开浏览器 http://localhost:8080/hello ,连续刷新可以看到不会 等待 3秒时间了,pom.xml controller service 代码如下. -----------S ...
- 在LIPS表追加拣配数量PIKMG字段(转)
原文地址:https://blog.csdn.net/zhongguomao/article/details/43451127 最近比较忙,此方案出后测试了很多种情况都存在问题,只能留待以后处理了.. ...
- sed查找实例:mysql_process.sh
标准 #!/bin/bash # FILE_NAME=/home/roo/Desktop/shell_code/day6/my.cnf # 获取所有的片段 function get_all_segme ...
- 数据库之Query Builder
Yii的查询构造器提供了一个用面向对象的方法来构造SQL语句.他让开发人员可以用类的方法,属性来作为SQL语句的一部分.然后把不同部分组装到一个正确的SQL语句中,调用DAO的方法来执行.下面的例子演 ...
- go 学习之io/ioutil包
// Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情// 并且始终成功返回.var Discard io.Writer = devNull(0) // Re ...
- Python : Data Encapsulation
Python : Data Encapsulation The following table shows the different behaviour: Name Notation Behavio ...