6 Replies Latest reply on Jan 2, 2018 10:27 PM by Sandhiya

    ML101 - W2 - Qn 5

    nikhil_r

      When below code was executed, I should have seen accuracy as 1.0. But, I always see 0.9422. May I know what is wrong with below code? Thanks!

       

      When weighted distances are used for part 1 of this question, a value of 1.0 should be returned for the accuracy.

       

      from sklearn.neighbors import KNeighborsClassifier

      knn_w_dist = KNeighborsClassifier(n_neighbors=10, weights='distance')

      knn_w_dist = knn_w_dist.fit(X_data, y_data)

      y_pred_w_dist = knn_w_dist.predict(X_data)

      print(accuracy(y_data, y_pred_w_dist))

        • 1. Re: ML101 - W2 - Qn 5
          Anju_Paul

          Hi,

           

          Thank you for the question. We will check and get back to you.

           

          Regards,

          Anju

          • 2. Re: ML101 - W2 - Qn 5
            Sandhiya

            Hi Nikhil,

             

            Since you have used the variable name as y_pred_w_dist, make sure you change y_pred in the accuracy function to y_pred_w_dist.

             

            In simple terms, you are still making use of the y_pred defined earlier.

             

            Hence,as suggested, you could either rename the variable names in the accuracy method or you could overwrite the values by keeping the same variable names as shown below:

             

            knn = KNeighborsClassifier(n_neighbors=5, weights='distance')

            knn = knn.fit(X_data, y_data)

            y_pred = knn.predict(X_data)

            print(accuracy(y_data, y_pred))

             

            Please try this and get back for any further assistance.

             

            Regards,

            Sandhiya

            • 3. Re: ML101 - W2 - Qn 5
              nikhil_r

              Hi Sandhiya,

              Can you please look at the original code snippet, which I have posted in my question? I used the variable, which I created but not y_pred. But still I didn't get the expected answer earlier.

               

              However, I got the answer as 1.0, once I overwritten the variables (after quitting and reloading the notebook). But, I always got the same answer in consecutive questions also. Please find the attached picture.

              Could below be possibilities?

              1. Notebook has some issue. Even after creating new variables, the accuracy result is getting printed based on the previous calculation?
              2. Or the data is getting over-fit?
              • 4. Re: ML101 - W2 - Qn 5
                Sandhiya

                Hi Nikhil,

                 

                Can you make the change to the accuracy function(as shown in the image attached) and check if your problem is resolved?

                 

                 

                 

                Regards,

                Sandhiya

                 

                accuracy.png

                • 5. Re: ML101 - W2 - Qn 5
                  nikhil_r

                  The one you are asking me to change is a function definition. It is not supposed to be changed if it is correct implementation.

                  I can call the function with the required arguments wherever it is required!

                  • 6. Re: ML101 - W2 - Qn 5
                    Sandhiya

                    Looks like that is a correction that is to be made in the student toolkit.

                     

                    Probably in the next release this problem should be resolved.

                     

                     

                    Thanks & Regards,

                    Sandhiya