Find-S:寻找极大特殊假设
算法流程:1. 将h初始化为H中最特殊的假设;2. 对每个正例x(对于h中的每个属性约束ai),如果x满足ai,那么不做任何处理,否则将h中ai替换为x满足的下一个更一般约束3.输出假设h.
/***************************************************************************
**Mail:zhjkobe2013@live.com
**Find-S.cpp
**@Function: Find appropriate hypthoesis
**Machine learning: chapter 2
**Author:zhangjie
**Create Date: 2014-12-07
**Last Modify:
***************************************************************************/
/*--------------------------------------------------------------------------
Positive Sample & Negative Sample
---------------------------------------------------------------------------
Example Sky Airtemp Humidity Wind Water Forcast EnjoySports
1 Sunny Warm Normal Strong Warm Same Yes
2 Sunny Warm High Strong Warm Same Yes
3 Rainy Cold High Strong Warm Change No
4 Sunny Warm High Strong Cool Change Yes
---------------------------------------------------------------------------
--------------------------------------------------------------------------*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define NATTRIBUTE 6
#define NSAMPLE 4
void Input_Set(vector< vector<string> >& set)
{
string s;
int count;
char* chs;
chs=(char*)malloc(255*sizeof(char));
memset(chs,'0',255*sizeof(char));
cout<<"Start input"<<endl;
/*for(int i=0;i<NSAMPLE+1;i++)
{
count=0;
while(cin>>s,s!="end")
{
if(count>=(NATTRIBUTE+2))
{
break;
}
else
{
set[i][count]=s;
count=count+1;
}
}
}*/
FILE* fp;
int index,start,len;
fp=fopen("data.txt","r");
for(int i=0;i<NSAMPLE+1;i++)
{
fscanf(fp,"%s",chs);
//fgets((char*)&s,SCHAR_MAX,fp);
s=chs;
cout<<s<<endl;
start=0;
count=0;
len=s.length();
cout<<"len="<<len<<endl;
while(count<(NATTRIBUTE+2))
{
index=s.find_first_of("|",start);
cout<<"index="<<index<<endl;
set[i][count]=s.substr(start,index-start);
start=index+1;
count=count+1;
}
cout<<endl;
}
fclose(fp);
cout<<endl<<"Finish input"<<endl;
if(chs) free(chs);
}
void Print_Set(vector< vector<string> >& set)
{
cout<<"Start output"<<endl;
for(int i=0;i<NSAMPLE+1;i++)
{
for(int j=0;j<NATTRIBUTE+2;j++)
cout<<set[i][j]<<" ";
cout<<endl;
}
cout<<"Finish output"<<endl;
}
bool Find_S(vector< vector<string> >&set,string* h)
{
int* count=(int*)malloc(NATTRIBUTE*sizeof(int));
memset(count,0,NATTRIBUTE*sizeof(int));
for(int i=1;i<NSAMPLE+1;i++)
{
for(int j=1;j<NATTRIBUTE+1;j++)
{
if(set[i][NATTRIBUTE+1]=="Yes")
{
if(set[i][j]!=h[j-1])
{
if(count[j-1]==0)
{
h[j-1]=set[i][j];
count[j-1]+=1;
}
else
count[j-1]+=1;
}
}
else
continue;
}
}
for(int i=0;i<NATTRIBUTE;i++)
if(count[i]>=2) h[i]="?";
if(count) free(count);
return true;
}
int main(int argc,char* argv[])
{
vector< vector<string> > m_Set(NSAMPLE+1,vector<string>(NATTRIBUTE+2));
Input_Set(m_Set);
Print_Set(m_Set);
string h[NSAMPLE+2]={"#","#","#","#","#","#"};
Find_S(m_Set,h);
cout<<"The hypthoesis is: ";
for(int i=0;i<NSAMPLE+2;i++)
cout<<h[i]<<' ';
cout<<endl;
return 0;
}
//data.txt的格式为
Example|Sky|Airtemp|Humidity|Wind|Water|Forcast|EnjoySports|
1|Sunny|Warm|Normal|Strong|Warm|Same|Yes|
2|Sunny|Warm|High|Strong|Warm|Same|Yes|
3|Rainy|Cold|High|Strong|Warm|Change|No|
4|Sunny|Warm|High|Strong|Cool|Change|Yes|