11/30/24, 3:54 PM Practical2.
ipynb - Colab
# Import required libraries
import pandas as pd
import numpy as np
# Create a synthetic dataset
data = {
'Sky': ['Sunny', 'Sunny', 'Rainy', 'Sunny', 'Rainy'],
'Temperature': ['Warm', 'Cold', 'Warm', 'Warm', 'Cold'],
'Humidity': ['Normal', 'High', 'High', 'Normal', 'Normal'],
'Wind': ['Strong', 'Strong', 'Weak', 'Strong', 'Weak'],
'Water': ['Warm', 'Warm', 'Cool', 'Warm', 'Cool'],
'Forecast': ['Same', 'Same', 'Change', 'Same', 'Change'],
'Condition': ['Yes', 'No', 'No', 'Yes', 'No'] # Target variable
}
# Convert the dataset to a DataFrame
df = [Link](data)
# Save the dataset to a CSV file
df.to_csv('training_data.csv', index=False)
# Display the dataset
print("Dataset:")
print(df)
Dataset:
Sky Temperature Humidity Wind Water Forecast Condition
0 Sunny Warm Normal Strong Warm Same Yes
1 Sunny Cold High Strong Warm Same No
2 Rainy Warm High Weak Cool Change No
3 Sunny Warm Normal Strong Warm Same Yes
4 Rainy Cold Normal Weak Cool Change No
# Load the dataset from CSV
dataset = pd.read_csv('training_data.csv')
# Display the dataset
print("\nLoaded Dataset:")
print(dataset)
Loaded Dataset:
Sky Temperature Humidity Wind Water Forecast Condition
0 Sunny Warm Normal Strong Warm Same Yes
1 Sunny Cold High Strong Warm Same No
2 Rainy Warm High Weak Cool Change No
3 Sunny Warm Normal Strong Warm Same Yes
4 Rainy Cold Normal Weak Cool Change No
def find_s(training_data):
# Extract the features and target
features = training_data.iloc[:, :-1].values # All columns except the last
target = training_data.iloc[:, -1].values # Last column (target variable)
# Initialize the most specific hypothesis
hypothesis = ['Ø'] * [Link][1]
# Iterate through each example in the dataset
for i, example in enumerate(features):
if target[i] == 'Yes': # Consider only positive examples
for j in range(len(hypothesis)):
if hypothesis[j] == 'Ø': # Update the hypothesis initially
hypothesis[j] = example[j]
elif hypothesis[j] != example[j]: # Generalize if inconsistent
hypothesis[j] = '?'
return hypothesis # Added indentation here
# Apply the FIND-S algorithm
final_hypothesis = find_s(dataset)
# Display the final specific hypothesis
print("\nFinal Specific Hypothesis:")
print(final_hypothesis)
Final Specific Hypothesis:
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
# Apply the FIND-S algorithm
final_hypothesis = find_s(dataset)
# Display the final specific hypothesis
print("\nFinal Specific Hypothesis:")
print(final_hypothesis)
[Link] 1/2
11/30/24, 3:54 PM [Link] - Colab
Final Specific Hypothesis:
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
[Link] 2/2