admin管理员组文章数量:1279015
I am trying to implement the Candidate Elimination Algorithm to learn a concept from a dataset. However, my code is not producing the expected results. Below is my implementation and the dataset I am using.
My Code:
import pandas as pd
import numpy as np
import os, sys
df = pd.read_csv("chumma.csv")
val_dict = {
"0":["Some","Many"],
"1":['Small','Big','Medium'],
'2':['No'],
'3':['Affordable','Expensive'],
'4':['One','Many','Few']
}
X = df.iloc[:,:-1].values
Y = df.iloc[:,-1].values
S = [['$']*5]
for i in range(len(Y)):
if Y[i] == ' Yes':
S = X[i].copy()
break
G = [["?"]*len(S)]
def is_consistent(x_data, y_data, g_hyp):
out_hyp = []
for hyp in g_hyp:
is_valid = True
for i in range(len(x_data)):
matches_hyp = True
for j in range(len(hyp)):
if hyp[j]!="?" and hyp[j]!=x_data[i][j]:
matches_hyp = False
break
if (y_data[i] == ' Yes' and not matches_hyp) or (y_data[i] == ' No' and matches_hyp):
is_valid = False
break
if is_valid and hyp not in out_hyp:
out_hyp.append(hyp)
return out_hyp
for i,instance in enumerate(X):
if Y[i] == ' Yes':
for j in range(len(S)):
if S[j]!=instance[j]:
S[j] = "?"
else:
g_hyp = []
for g in G:
for j in range(len(g)):
if g[j] == "?":
d_vals = val_dict[str(j)]
for k in range(0,len(d_vals)):
if d_vals[k] != instance[j]:
new_g = g.copy()
new_g[j] = d_vals[k]
print(new_g)
g_hyp.append(new_g)
print()
break
G = is_consistent(X[:i+1], Y[:i+1], g_hyp)
print("Final Specific Hypothesis:",S)
print("Final General Hypothesis:",G)
Dataset (chumma.csv):
Citations, Size, InLibrary, Price, Editions, Buy
Some, Small, No, Affordable, One, No
Many, Big, No, Expensive, Many, Yes
Many, Medium, No, Expensive, Few, Yes
Some, Small, No, Affordable, One, No
Many, Big, No, Expensive, Many, Yes
Many, Medium, No, Expensive, Few, Yes
Expected Output The algorithm should output the specific hypothesis (S) and general hypothesis (G) that represent the concept learned from the dataset
Issue The code does not seem to correctly update the specific hypothesis (S) and general hypothesis (G). Specifically:
- S is not being generalized correctly for positive examples.
- G is not being specialized correctly for negative examples.
What is wrong with my implementation of the Candidate Elimination Algorithm and how can I fix the code to correctly update S and G?
本文标签: pythonCandidate Elimination Algorithm Implementation Not Working as ExpectedStack Overflow
版权声明:本文标题:python - Candidate Elimination Algorithm Implementation Not Working as Expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277979a2369841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论