ML

Dis
mail@pastecode.io avatar
unknown
plain_text
a month ago
2.2 kB
2
Indexable
Never
import zipfile
import csv
import os

# Function to read data from a CSV file with specified encoding
def read_data(filename):
    with open(filename, 'r', encoding='utf-8') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',')
        traindata = []
        for row in datareader:
            traindata.append(row)
    return traindata

# Function to check if a hypothesis is consistent with a data instance
def isConsistent(h, d):
    if len(h) != len(d) - 1:
        print('Number of attributes are not the same in hypothesis.')
        return False
    else:
        matched = 0
        for i in range(len(h)):
            if h[i] == d[i] or h[i] == 'any':
                matched += 1
        return matched == len(h)

# Function to make the hypothesis consistent with a data instance
def makeConsistent(h, d):
    for i in range(len(h)):
        if h[i] == 'phi':
            h[i] = d[i]
        elif h[i] != d[i]:
            h[i] = 'any'
    return h

# Path to the zip file
zip_file_path = r'C:\Users\21BCE8157\Downloads\GlobalLandTemperaturesByCity.csv.zip'
# Path where the extracted files should be stored
extract_to_path = r'C:\Users\21BCE8157\Downloads'

# Extract the zip file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    zip_ref.extractall(extract_to_path)

# Assuming the extracted file is named 'GlobalLandTemperaturesByCity.csv'
csv_file_name = 'GlobalLandTemperaturesByCity.csv'
csv_file_path = os.path.join(extract_to_path, csv_file_name)

# Initialize the most specific hypothesis
h = ['phi', 'phi', 'phi', 'phi', 'phi', 'phi']

# Read data
data = read_data(csv_file_path)

print('Begin: Hypothesis:', h)
for d in data:
    if d[-1] == 'Yes':  # Assuming the last column is the class label
        if isConsistent(h, d):
            pass
        else:
            h = makeConsistent(h, d)
    print('Training data:', d)
    print('Updated Hypothesis:', h)
    print()
    print('---')

print('Maximally specific hypothesis: End:', h)
Leave a Comment