日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线性回归 c语言实现_C ++中的线性回归实现

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线性回归 c语言实现_C ++中的线性回归实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

線性回歸 c語言實現

Linear regression models the relation between an explanatory (independent) variable and a scalar response (dependent) variable by fitting a linear equation.

線性回歸通過擬合線性方程來對解釋性(獨立)變量和標量響應(因變量)之間的關系進行建模。

For example, Modeling the weights of Individuals with their heights using a linear equation.

例如,使用線性方程式對個人的體重及其身高進行建模。

Before trying to model the relationship on the observed data, you should first determine whether there is a linear relation between them or not, usually, the scatter plot can be a helpful tool to view the relation between the data.

在嘗試對觀察到的數據建立關系模型之前,首先應確定它們之間是否存在線性關系,通常,散點圖可以成為查看數據之間關系的有用工具。

https://commons.wikimedia.org/w/index.php?curid=11967659https: //commons.wikimedia.org/w/index.php?curid = 11967659

A linear regression line has an equation of the form Y = a + bX, where X is the explanatory variable and Y is the dependent variable. The slope of the line is, and a is the intercept (the value of y when x = 0).

線性回歸線的方程式為Y = a + bX ,其中X為解釋變量, Y為因變量。 線的斜率是, a是截距( x = 0時y的值)。

In this article, We will implement the Simple Linear Regression model. Simple linear regression concerns two-dimensional sample points with one independent variable and one dependent variable and finds a linear function that predicts the dependent variable values as a function of the independent variable.

在本文中,我們將實現簡單線性回歸模型。 簡單線性回歸涉及具有一個自變量和一個因變量的二維樣本點,并找到一個線性函數,該線性函數可預測因變量作為自變量的函數。

When you perform a simple linear regression (or any other type of regression analysis), you get a line of best fit. The data points usually don’t fall on this regression equation line; they are scattered around.

當執行簡單的線性回歸(或任何其他類型的回歸分析)時,您會得到一條最合適的線。 數據點通常不會掉落 在這個回歸方程線上; 他們四處散落。

A residual is a vertical distance between a data point and the regression line. Each data point has one residual. It is positive if it is above the regression line and negative if it is below the regression line. If the regression line passes through the point, the residual at that point is zero.

殘差是數據點和回歸線之間的垂直距離。 每個數據點都有一個殘差。 如果它高于回歸線,則為正;如果它低于回歸線,則為負。 如果回歸線通過該點,則該點的殘差為零。

The main problem here is to minimize the total residual error to find the line of best fit, if you need more explanation on the theory behind the following equations, I recommend reading this article:

這里的主要問題是最小化總殘留誤差以找到最佳擬合線,如果您需要以下方程背后的理論更多解釋,我建議您閱讀本文:

Without going into details, the equations that we should use are:

在不贅述的情況下,我們應使用的公式為:

here這里找到

Simply we can divide it into the following for simplicity:

為了簡單起見,我們可以將其分為以下幾類:

Now we can start going through the implementation of Linear Regression

現在我們可以開始執行線性回歸

1-計算系數: (1- Calculate the coefficients:)

The first step is to implement the function that calculates the coefficients

第一步是實現計算系數的功能

as the expected format for the equation is Y = a + bX, we need to calculate a and b, according to the mentioned relations.

由于方程的期望格式為Y = a + bX,因此我們需要根據上述關系式計算a和b。

1- calculate the mean for the dependent variable and the mean value for the independent variable.

1-計算因變量的平均值和自變量的平均值。

2- Calculate the SS_XY is the sum of the element-wise multiplication of the dependent variable vector with the independent variable vector.

2-計算SS_XY是因變量矢量與自變量矢量的逐元素相乘之和。

3- Calculate the SS_XX is the sum of the element-wise multiplication of the independent variable vector with itself.

3-計算SS_XX是自變量矢量與其自身的元素相乘的總和。

4- Calculate the B_1 coefficient by dividing the SS_XY over the SS_XX value.

4-通過將SS_XY除以SS_XX值來計算B_1系數。

5- Calculate the B_0 coefficient.

5-計算B_0系數。

Estimate coefficient API估算系數API

2-實施課程: (2- Implementing the Class:)

We need to train only two private variables, which are the coefficients.

我們只需要訓練兩個私有變量,即系數。

For the Fit API, we need it to take the dataset as a vector of the dependent and independent variables, and then estimate the coefficient based on these vectors and store the learned coefficients into our private variables.

對于Fit API,我們需要它將數據集作為因變量和自變量的向量,然后根據這些向量估計系數并將學習到的系數存儲到我們的私有變量中。

The remaining part is to implement the Predict API to take the independent variable value and return the estimated value after applying the Linear regression equation.

剩下的部分是實現Predict API,以采用獨立變量值并在應用線性回歸方程后返回估計值。

Linear regression Class線性回歸類

3-示例: (3- Example:)

An example of the usage of the Linear Model, we just implemented.We instantiated a class instance with types of float, fit this model to the independent variable and the dependent variable vectors.

我們剛剛實現了一個使用線性模型的示例,我們實例化了一個類型為float的類實例,使該模型適合自變量和因變量向量。

Then we test the model by predicting the values and showing the result after the model fitting.

然后,我們通過預測值并在模型擬合后顯示結果來測試模型。

Please note that for debugging purposes, I moved the b_0 and b_1 to be public.

請注意,出于調試目的,我將b_0和b_1公開。

the coefficients printing系數打印

I have also used the matplotlibcpp to plot the output and compare the predicted values against the original data.

我還使用了matplotlibcpp來繪制輸出,并將預測值與原始數據進行比較。

orange line is the predicted value after applying the linear regression橙色線是應用線性回歸后的預測值

You can find an introduction to how to use the matplotlibcpp in the following article.

您可以在以下文章中找到有關如何使用matplotlibcpp的介紹。

The implementation of Linear regression is simple. Linear Regression is a powerful statistical technique and can be used to generate insights on consumer behavior, understanding business, and factors influencing profitability. Linear regressions can also be used in business to evaluate trends and make estimates or forecasts.

線性回歸的實現很簡單。 線性回歸是一種強大的統計技術,可用于生成有關消費者行為,了解業務以及影響盈利能力的因素的見解。 線性回歸還可以用于業務中以評估趨勢并做出估計或預測。

This article is part of a series that address the implementation of Machine learning algorithms in C++, throughout this series, We will be using the Iris data set available here.

本文是該系列的一部分,該系列解決了C ++中機器學習算法的實現,在整個系列中,我們將使用此處提供的Iris數據集。

  • When Should You Learn Machine Learning using C++?

    什么時候應該使用C ++學習機器學習?

  • The 8 Books Each C++ Developer Must Read.

    每個C ++開發人員必須閱讀的8本書。

  • Data Preprocessing And Visualization In C++.

    C ++中的數據預處理和可視化。

  • Machine Learning Data Manipulation Using C++.

    使用C ++進行機器學習數據操作。

  • Naive Bayes From Scratch using C++

    使用C ++從零開始的樸素貝葉斯

Hope you find this article useful, Please follow to get notified when a new article in this series is released.

希望本文對您有用,請在發布本系列的新文章時關注以得到通知。

翻譯自: https://medium.com/swlh/linear-regression-implementation-in-c-acdfb621e56

線性回歸 c語言實現

總結

以上是生活随笔為你收集整理的线性回归 c语言实现_C ++中的线性回归实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。