{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MACHINE LEARNING LAB - 1 ( FIND-S Algorithm )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1. Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis based on a given set of training data samples. Read the training data from a .CSV file.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<_csv.reader object at 0x0000001781862BA8>\n", "\n", "The given training examples are:\n", "['sky', 'airTemp', 'humidity', 'wind', 'water', 'forecast', 'enjoySport']\n", "['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes']\n", "['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes']\n", "['Rainy', 'Cold', 'High', 'Strong', 'Warm', 'Change', 'No']\n", "['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change', 'Yes']\n" ] } ], "source": [ "import csv\n", "hypo = ['%','%','%','%','%','%'];\n", "\n", "with open('trainingdata.csv') as csv_file:\n", " readcsv = csv.reader(csv_file, delimiter=',')\n", " print(readcsv)\n", " \n", " data = []\n", " print(\"\\nThe given training examples are:\")\n", " for row in readcsv:\n", " print(row)\n", " if row[len(row)-1].upper() == \"YES\":\n", " data.append(row)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "The positive examples are:\n", "['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes']\n", "['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes']\n", "['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change', 'Yes']\n", "\n", "\n" ] } ], "source": [ "print(\"\\nThe positive examples are:\");\n", "for x in data:\n", " print(x);\n", "print(\"\\n\");" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The steps of the Find-s algorithm are :\n", " ['%', '%', '%', '%', '%', '%']\n", "['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']\n", "['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']\n", "['Sunny', 'Warm', '?', 'Strong', '?', '?']\n" ] } ], "source": [ "TotalExamples = len(data);\n", "i=0;\n", "j=0;\n", "k=0;\n", "print(\"The steps of the Find-s algorithm are :\\n\",hypo);\n", "list = [];\n", "p=0;\n", "d=len(data[p])-1;\n", "for j in range(d):\n", " list.append(data[i][j]);\n", "hypo=list;\n", "i=1;\n", "for i in range(TotalExamples):\n", " for k in range(d):\n", " if hypo[k]!=data[i][k]:\n", " hypo[k]='?';\n", " k=k+1; \n", " else:\n", " hypo[k];\n", " print(hypo);\n", "i=i+1;" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "The maximally specific Find-s hypothesis for the given training examples is :\n", "['Sunny', 'Warm', '?', 'Strong', '?', '?']\n" ] } ], "source": [ "print(\"\\nThe maximally specific Find-s hypothesis for the given training examples is :\");\n", "list=[];\n", "for i in range(d):\n", " list.append(hypo[i]);\n", "print(list);" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }