Aishwarya Boobalan
2 min readApr 1, 2024

Title: Comprehending the Machine Learning Candidate Elimination Algorithm

The Candidate Elimination technique is a straightforward but effective technique in the field of machine learning. To understand it, let’s reduce it to its most basic form.

## The Issue

Consider that you have a dataset of different cases in which a person prefers to play a specific sport or not, depending on variables such as temperature, weather, and so forth. Our job is to figure out a hypothesis that, given these characteristics, can forecast whether or not a person will enjoy a sport.

## The Code

```python
import numpy as np
import pandas as pd

# Loading the dataset
data = pd.DataFrame(data=pd.read_csv(‘enjoysport.csv’))
concepts = np.array(data.iloc[:,0:-1])
target = np.array(data.iloc[:,-1])

# Candidate Elimination Algorithm
def learn(concepts, target):
specific_h = concepts[0].copy()
general_h = [[“?” for _ in range(len(specific_h))] for _ in range(len(specific_h))]

for i, h in enumerate(concepts):
if target[i] == “yes”:
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = ‘?’
general_h[x][x] = ‘?’
if target[i] == “no”:
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = ‘?’

# Remove general hypotheses with all ‘?’
indices = [i for i, val in enumerate(general_h) if val == [‘?’] * len(specific_h)]
for i in indices:
general_h.remove([‘?’] * len(specific_h))

return specific_h, general_h

s_final, g_final = learn(concepts, target)

print(“Final Specific_h:”, s_final, sep=”\n”)
print(“Final General_h:”, g_final, sep=”\n”)
```

## Clarification

Every instance in the dataset is iterated through by this algorithm. It modifies the specific and general hypotheses for every instance according to whether it is categorized as positive (yes) or negative (no).

- If the instance is positive, the general hypothesis is updated in accordance with the particular hypothesis, which is updated by replacing characteristics that don’t match with ‘?’.
- Should the instance be negative, the general hypothesis is updated by substituting attributes in the specific hypothesis that don’t match with ‘?’, or the equivalent attribute if it does match already.