Page 49 - Data Science Algorithms in a Week
P. 49

Naive Bayes


                        return int(dic[key])
            Input:

            We save the data from the table in example Playing chess in the following CSV file:

                source_code/2/naive_bayes/chess.csv
                Temperature,Wind,Sunshine,Play
                Cold,Strong,Cloudy,No
                Warm,Strong,Cloudy,No
                Warm,None,Sunny,Yes
                Hot,None,Sunny,No
                Hot,Breeze,Cloudy,Yes
                Warm,Breeze,Sunny,Yes
                Cold,Breeze,Cloudy,No
                Cold,None,Sunny,Yes
                Hot,Strong,Cloudy,Yes
                Warm,None,Cloudy,Yes
                Warm,Strong,Sunny,?
            Output:

            We provide the file chess.csv as the input to the Python program calculating the
            probabilities of the data item (Temperature=Warm,Wind=Strong, Sunshine=Sunny) belonging
            to the classes present in the file: Play=Yes and Play=No. As we found out earlier manually,
            the data item belongs with a higher probability to the class Play=Yes. Therefore we classify
            the data item into that class:

                $ python naive_bayes.py chess.csv
                [
                    ['Warm', 'Strong', 'Sunny', {
                        'Yes': 0.6666666666666666,
                        'No': 0.33333333333333337
                    }]
                ]



            Playing chess - dependent events

            Suppose that we would like to find out again if our friend would like to play chess in the
            park with us in a park in Cambridge, UK. But, this time, we are given different input data:

             Temperature Wind     Season   Play
             Cold          Strong Winter   No




                                                     [ 37 ]
   44   45   46   47   48   49   50   51   52   53   54