数据挖掘实验报告(一)
数据预处理
姓名:***
班级:计算机1304
学号:***
一、实验目的
1.学习均值平滑,中值平滑,边界值平滑的基本原理
2.掌握链表的使用方法
3.掌握文件读取的方法
二、实验设备
PC一台,dev-c++5.11
三、实验内容
数据平滑
假定用于分析的数据包含属性age。数据元组中age的值如下(按递增序):13, 15, 16, 16, 19, 20, 20, 21, 22, 22, 25, 25, 25, 25, 30, 33, 33, 35, 35, 35, 35, 36, 40, 45, 46, 52, 70。使用你所熟悉的程序设计语言进行编程,实现如下功能(要求程序具有通用性):
(a) 使用按箱平均值平滑法对以上数据进行平滑,箱的深度为3。
(b) 使用按箱中值平滑法对以上数据进行平滑,箱的深度为3。
(c) 使用按箱边界值平滑法对以上数据进行平滑,箱的深度为3。
四、实验原理
使用c语言,对数据文件进行读取,存入带头节点的指针链表中,同时计数,均值求三个数的平均值,中值求中间的一个数的值,边界值将中间的数转换为离边界较近的边界值
五、实验步骤
代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DEEP 3
#define DATAFILE "data.txt"
#define VPT 10
//定义结构体
typedef struct chain{
int num;
struct chain *next;
}* data;
//定义全局变量
data head,p,q;
FILE *fp;
int num,sum,count=0;
int i,j;
int *box;
void mean();
void medain();
void boundary();
int main ()
{
//定义头指针
head=(data)malloc(sizeof(struct chain));
head->next=NULL;
/*打开文件*/
fp=fopen(DATAFILE,"r");
if(!fp)
exit(0);
p=head;
while(!feof(fp)){
q=(data)malloc(sizeof(struct chain));
q->next=NULL;
fscanf(fp,"%d",&q->num); /*读一个数据*/
p->next=q;
p=q;
count++;
}
/* 关闭文件 */
fclose(fp);
//输出
printf("源数据为:\n");
printf("共%d箱%d个数据\n",count/DEEP,count);
p=head->next;
count=1;
num=1;
while(p!=NULL){
if(count==1)printf("箱%d:",num);
if(count==DEEP){
printf("%d\n",p->num);
num++;
count=1;
}
else{
printf("%d ",p->num);
count++;
}
p=p->next;
}
mean();
medain();
boundary();
scanf("%d",&i);
return 0;
}
//均值
void mean(){
printf("均值平滑后为:");
box=(int *)malloc(sizeof(int)*num);
p=head->next;
count=1;
num=0;
sum=0;
while(p!=NULL){
if(count==DEEP){
count=1;
sum=sum+p->num;
box[num]=sum/DEEP;
sum=0;
num++;
}
else{
sum=sum+p->num;
count++;
}
p=p->next;
}
for (i=0;i<num;i++){
printf("\n箱%d:",i+1);
for (j=0;j<DEEP;j++)
printf("%d ",box[i]);
}
p=head->next;
printf("\n离群值为:");
while(p!=NULL){
for(i=0;i<num;i++)
{
for (j=0;j<DEEP;j++)
{
if(abs(p->num-box[i])>(int)VPT)
{
printf("\n箱%d:",i+1);
printf("%d ",p->num);
}
p=p->next;
}
}
}
}
//中值
void medain(){
printf("\n中值平滑后为:");
p=head->next;
count=1;
num=0;
int mid;
while(p!=NULL){
if(count==DEEP){
box[num]=sum;
count=1;
num++;
}
else {
if(count==DEEP/2||count==DEEP/2+1)
if(DEEP%2){
if(count==DEEP/2+1)
sum=p->num;
}
else{
if(count==DEEP/2+1)
sum=(p->num+mid)/2;
else
mid=p->num;
}
count++;
}
p=p->next;
}
for (i=0;i<num;i++){
printf("\n箱%d:",i+1);
for (j=0;j<DEEP;j++)
printf("%d ",box[i]);
}
}
//边界值
void boundary(){
printf("\n边界值平滑后为:\n");
p=head->next;
count=1;
box=(int *)malloc(sizeof(int)*num*2);
num=0;
while(p!=NULL){
if(count==DEEP){
box[2*num+1]=p->num;
count=1;
num++;
}
else{
if(count==1) {
box[2*num]=p->num;
}
count++;
}
p=p->next;
}
p=head->next;
count=1;
num=0;
while(p!=NULL){
if(count==1)
printf("箱%d:",num);
if((p->num-box[2*num])>(box[2*num+1]-p->num)){
printf("%d ",box[2*num+1]);
}
else
printf("%d ",box[2*num]);
if(count==DEEP){
printf("\n");
count=0;
num++;
}
count++;
p=p->next;
}
}
实验数据文件:data.txt用空格分开
13 15 16 16 19 20 20 21 22 22 25 25 25 25 30 33 33 35 35 35 35 36 40 45 46 52 70
六、结果截图
第二篇:实验报告二
天津商学院学生实验报告
开课实验室:5号实训楼608 开课时间 20## 年10 月 11 日 实验报告2013 年 11 月13 日
注1.每个实验项目一份实验报告。2.实验报告第一页学生必须使用规定的实验报告纸书写,附页用实验报告附页纸或A4纸书写,字迹工整,曲线要画在坐标纸上,线路图要整齐、清楚(不得徒手画)。3.实验教师必须对每份实验报告进行批改,用红笔指出实验报告中的错、漏之处,并给出评语、成绩,签全名、注明日期。4.待实验课程结束以后,要求学生把实验报告整理好,交给实验指导教师,加上实验课学生考勤及成绩登记表(见附件2)、目录和学院统一的封面(见附件3)后,统一装订成册存档。
共 页 第 页 制表单位:设备处