gpt4 book ai didi

c - 选择和分析数组中点的窗口

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:46 25 4
gpt4 key购买 nike

有人可以告诉我如何解决这个问题。

我有一个函数可以对数组中包含的一组点执行简单的回归分析。我有一个数组 (pval),其中包含我要对其执行回归分析的所有数据。这就是我想要实现的方式。

  1. 我得到数组前 7 个元素的平均值。这就是我在程序中所说的“ref_avg”。

  2. 我想对数组的每五个元素执行回归分析,并将该数组的第一个元素作为“ref_avg”。也就是说,在回归分析的每一步中,我都会在数组中有 6 个点。

    例如对于第一步,下面计算的 ref_avg 是 70.78。所以简单回归的第一步将包含这些点

    第一 = {70.78,76.26,69.17,68.68,71.49,73.08},

    第二步将包含 ref_avg 作为第一个元素以及从原始数组中的第二个元素开始的其他元素

    第二 = {70.78,69.17,68.68,71.49,73.08,72.99},

    第三 = {70.78,68.68,71.49,73.08,72.99,70.36},

    第 4 = {70.78,71.49,73.08,72.99,70.36,57.82} 依此类推直到结束。

  3. 回归函数也如下图所示。

我不明白为什么“calcul”数组的前 3 个元素在回归的第一步中的值为 0.00,在第二步中有 2 个元素,在第三步中有 1 个元素。回归函数的最后一步也打印了 3 次。

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

float pval[]={76.26,69.17,68.68,71.49,73.08,72.99,70.36,57.82,58.98,69.71,70.43,77.53,80.77,70.30,70.5,70.79,75.58,76.88,80.20,77.69,80.80,70.5,85.27,75.25};


int count,Nhour;
const int MAX_HOUR = 24;
float *calcul=NULL;
float *tab_time =NULL;
float ref_avg;
int size_hour=7;
float sum=0;
int length = Nhour+1;
float m;
float b;
calcul=(float*)calloc(MAX_HOUR,sizeof(calcul));
if (calcul==NULL)
{
printf(" error in buffer\n");
exit(EXIT_FAILURE);
}

tab_time= calloc(MAX_HOUR,sizeof(float));

/* Get the average of the first seven elements */
int i;
for (i=0;i<size_hour;i++)
{
sum += pval[i];
}
ref_avg = sum / size_hour;

count=0;
/* perform the regression analysis on 5 hours increment */

while(count<=MAX_HOUR)
{
++count;
Nhour=5;

int pass = -(Nhour-1);
int i=0;

for(i=0;i<Nhour+1;i++)
{
if(count<MAX_HOUR)
{

calcul[0]=ref_avg;
calcul[i] =pval[count+pass];
pass++;
}

printf("calc=%.2f\n",calcul[i]); // For debug only
tab_time[i]=i+1;

if(i==Nhour)
{

linear_regression(tab_time, calcul, length, &m, &b);
printf("Slope= %.2f\n", m);

}
}
}

free(calcul);
calcul=NULL;
free(tab_time);
tab_time=NULL;
return 0;
}
/* end of the main function */


/* This function is used to calculate the linear
regression as it was called above in the main function.
It compiles and runs very well, was just included for the
compilation and execution of the main function above where I have a problem. */


int linear_regression(const float *x, const float *y, const int n, float *beta1, float *beta0)
{

float sumx = 0,
sumy = 0,
sumx2 = 0,
sumxy = 0;

int i;
if (n <= 1) {
*beta1 = 0;
*beta0= 0;
printf("Not enough data for regression \n");
}
else
{
float variance;

for (i = 0; i < n; i++)
{
sumx += x[i];
sumy += y[i];

sumx2 += (x[i] * x[i]);

sumxy += (x[i] * y[i]);
}
variance = (sumx2 - ((sumx * sumx) / n));
if ( variance != 0) {
*beta1 = (sumxy - ((sumx * sumy) / n)) / variance;
*beta0 = (sumy - ((*beta1) * sumx)) / n;
}
else
{
*beta1 = 0;
*beta0 = 0;

}

}
return 0;
}

最佳答案

我认为这段代码会产生理智的答案。问题中引用的引用平均值似乎是错误的。不需要内存分配。 MAX_HOUR 的值为 24,但数组中只有 23 个数据值。构建待回归数组的索引是伪造的,引用了 pval 数组中的负索引(因此导致错误结果)。变量Nhour在初始化前被引用;变量长度设置不正确。没有很好的诊断打印。

main() 的主体在这里被大量重写; linear_regression() 上的编辑几乎是最小的。代码的布局更加一致,并使用了空白以使其更易于阅读。当不再有足够的数据来用 5 个值填充数组时,此版本终止回归 - 目前尚不清楚预期的终止条件是什么。

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void linear_regression(const float *x, const float *y, const int n,
float *beta1, float *beta0);

int main(void)
{
float pval[]={
76.26, 68.68, 71.49, 73.08, 72.99, 70.36, 57.82, 58.98,
69.71, 70.43, 77.53, 80.77, 70.30, 70.50, 70.79, 75.58,
76.88, 80.20, 77.69, 80.80, 70.50, 85.27, 75.25,
};
const int Nhour = 5;
const int MAX_HOUR = sizeof(pval)/sizeof(pval[0]);
const int size_hour = 7;
float ref_avg;
float sum = 0.0;
float m;
float b;
float calc_y[6];
float calc_x[6];

/* Get the average of the first seven elements */
for (int i = 0; i < size_hour; i++)
sum += pval[i];
ref_avg = sum / size_hour;
printf("ref avg = %5.2f\n", ref_avg); // JL

/* perform the regression analysis on 5 hours increment */
for (int pass = 0; pass <= MAX_HOUR - Nhour; pass++) // JL
{
calc_y[0] = ref_avg;
calc_x[0] = pass + 1;
printf("pass %d\ncalc_y[0] = %5.2f, calc_x[0] = %5.2f\n",
pass, calc_y[0], calc_x[0]);
for (int i = 1; i <= Nhour; i++)
{
int n = pass + i - 1;
calc_y[i] = pval[n];
calc_x[i] = pass + i + 1;
printf("calc_y[%d] = %5.2f, calc_x[%d] = %5.2f, n = %2d\n",
i, calc_y[i], i, calc_x[i], n);
}

linear_regression(calc_x, calc_y, Nhour+1, &m, &b);
printf("Slope= %5.2f, intercept = %5.2f\n", m, b);
}

return 0;
}

void linear_regression(const float *x, const float *y, const int n, float *beta1, float *beta0)
{
float sumx1 = 0.0;
float sumy1 = 0.0;
float sumx2 = 0.0;
float sumxy = 0.0;

assert(n > 1);

for (int i = 0; i < n; i++)
{
sumx1 += x[i];
sumy1 += y[i];
sumx2 += (x[i] * x[i]);
sumxy += (x[i] * y[i]);
}
float variance = (sumx2 - ((sumx1 * sumx1) / n));
if (variance != 0.0)
{
*beta1 = (sumxy - ((sumx1 * sumy1) / n)) / variance;
*beta0 = (sumy1 - ((*beta1) * sumx1)) / n;
}
else
{
*beta1 = 0.0;
*beta0 = 0.0;
}
}

关于c - 选择和分析数组中点的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com