// 机器学习与深度学习 C语言实现
// 基于归纳学习的股价预测程序
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define NG 0
#define SETSIZE 100
#define CNO 10
#define GENMAX 100000
#define SEED 32767
int calcscore(int data[SETSIZE][CNO], int teacher[SETSIZE], int answer[CNO])
{
int score = 0;
int point ;
int i, j;
for(i = 0; i < SETSIZE ; ++i)
{
point = 0;
for(j = 0 ; j < CNO ; ++j)
{
if(answer[j] == 2)
++point;
else if(answer[j] == data[i][j])
++point;
}
if((point == CNO) && (teacher[i] == 1))
{
++score;
}
else if((point != CNO) && (teacher[i] == 0))
{
++score;
}
}
return score;
}
void readdate(int data[SETSIZE][CNO] , int teacher[SETSIZE])
{
int i, j;
for(int i = 0; i < SETSIZE; ++i)
{
for(j = 0; j < CNO ; ++j)
{
scanf("%d",&data[i][j]);
}
scanf("%d",&teacher[i]);
}
}
int rand012()
{
int rnd;
while((rnd = rand()) == RAND_MAX);
return (double) rnd / RAND_MAX * 3;
}
void readdata(int data[SETSIZE][CNO], int teacher[SETSIZE])
{
int rand012();
int calcscore(int data[SETSIZE][CNO],int teacher[SETSIZE], int answer[CNO]);
}
int main()
{
int i, j;
int score = 0;
int answer[CNO] ;
int data[SETSIZE][CNO];
int teacher[SETSIZE];
int bestscore = 0;
int bestanswer[CNO];
srand(SEED);
readdata( data, teacher);
for( i = 0 ; i < GENMAX; ++i)
{
for(j = 0; j < CNO; ++j)
{
answer[j] = rand012();
}
score = calcscore(data, teacher, answer);
if(score > bestscore)
{
for(j = 0; j < CNO; ++j)
printf("%d ", bestanswer[j]); // 此书原来用的是 %ld
printf(":score = %d\n",bestscore);
}
}
printf("\n最优解\n");
for(j = 0; j < CNO; ++j)
printf("%d\n",bestanswer[j]);
printf(":score = %d\n",bestscore);
return 0;
}
基于归纳学习的股价预测程序
最新推荐文章于 2023-12-18 07:00:00 发布