pgm 4
#pgm4
import pandas as pd
def find_s_algorithm(data):
print("Training data:")
print(data)
attributes = data.columns[:-1]
class_label = data.columns[-1]
hypothesis = None
for index, row in data.iterrows():
if row[class_label] == 'Yes': # only consider positive examples
if hypothesis is None:
hypothesis = list(row[attributes])
else:
for i, value in enumerate(row[attributes]):
if hypothesis[i] != value:
hypothesis[i] = '?' # generalize the hypothesis
if hypothesis is None:
hypothesis = ['?' for _ in attributes]
return hypothesis
# Sample training data
sample_data = {
'Sky': ['Sunny', 'Sunny', 'Rainy', 'Sunny'],
'Temperature': ['Warm', 'Warm', 'Cold', 'Warm'],
'Humidity': ['High', 'High', 'High', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak'],
'PlayTennis': ['Yes', 'Yes', 'No', 'Yes']
}
data = pd.DataFrame(sample_data)
hypothesis = find_s_algorithm(data)
print("\nThe final hypothesis is:", hypothesis)
Comments
Post a Comment