BCSL606 Program 4

PROGRAM: download csv file click here

import pandas as pd


def find_s_algorithm(file_path):
    data = pd.read_csv(file_path)

    print("Training data:")
    print(data)

    attributes = data.columns[:-1]
    class_label = data.columns[-1]

    hypothesis = ['?' for _ in attributes]

    for index, row in data.iterrows():
        if row[class_label] == 'Yes':
            for i, value in enumerate(row[attributes]):
                if hypothesis[i] == '?' or hypothesis[i] == value:
                    hypothesis[i] = value
                else:
                    hypothesis[i] = '?'

    return hypothesis


file_path = 'training_data.csv'
hypothesis = find_s_algorithm(file_path)
print("\nThe final hypothesis is:", hypothesis)

OUTPUT:

Training data:

     Outlook  Temperature  Humidity  Windy  PlayTennis
0     Sunny         Hot     High     False       No
1     Sunny         Hot     High     True        No
2  Overcast         Hot     High     False       Yes
3      Rain        Cold     High     False       Yes
4      Rain        Cold     High     True        No
5  Overcast         Hot     High     True        Yes
6     Sunny         Hot     High     False       No


The final hypothesis is: ['Overcast', 'Hot', 'High', '?']

One thought on “BCSL606 Program 4

  1. The output is incorrect.

    It should be [‘?’, ‘?’, ‘high’, ‘?’].

    Try doing it manually—some changes need to be made:
    1.Initialize hypothesis = None.
    2.Use a for loop.
    3.Add an if condition:
    If hypothesis == None, set hypothesis = list(row[attributes]).
    Please correct your code, as it has issues.

Leave a Reply

Your email address will not be published. Required fields are marked *