How to Programming for Data Science by using KNN [on hold]












-4












$begingroup$


May I know how to modify my Python programming so that can obtain the accuracy vs number of neighbours as refer to the attached image file -



# read in the iris data
from sklearn.datasets import load_iris
iris = load_iris()
# create X (features) and y (response)
X = iris.data
y = iris.target

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)
knn.fit(X, y)
y_pred = knn.predict(X)

from sklearn import metrics
metrics.accuracy_score(y,y_pred)
knn = KNeighborsClassifier(n_neighbors=1)
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

# import Matplotlib (scientific plotting library)
import matplotlib.pyplot as plt

# try K=1 through K=9 and record testing accuracy
k_range = range(1, 9)
# create Python dictionary using
scores =

for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test, y_pred))

# plot the relationship between K and testing accuracy
# plt.plot(x_axis, y_axis)
plt.plot(k_range, scores)
plt.xlabel('Number of neighbors')
plt.ylabel('Accuracy')


The error message is -



runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')
Reloaded modules: __mp_main__
Traceback (most recent call last):

File "<ipython-input-3-1ba40d3637a3>", line 1, in <module>
runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')

File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 668, in runfile
execfile(filename, namespace)

File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py", line 11, in <module>
knn.fit(X, y)

File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 790, in fit
return self._fit(X)

File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 229, in _fit
self.n_neighbors < self._fit_X.shape[0] // 2) and

TypeError: '<' not supported between instances of 'list' and 'int'


Please help me on this case so that I can improve my computing skills










share|improve this question









New contributor




master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Simon Larsson, Ethan, oW_ 2 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question does not appear to be about data science, within the scope defined in the help center." – oW_

If this question can be reworded to fit the rules in the help center, please edit the question.





















    -4












    $begingroup$


    May I know how to modify my Python programming so that can obtain the accuracy vs number of neighbours as refer to the attached image file -



    # read in the iris data
    from sklearn.datasets import load_iris
    iris = load_iris()
    # create X (features) and y (response)
    X = iris.data
    y = iris.target

    from sklearn.neighbors import KNeighborsClassifier
    knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)
    knn.fit(X, y)
    y_pred = knn.predict(X)

    from sklearn import metrics
    metrics.accuracy_score(y,y_pred)
    knn = KNeighborsClassifier(n_neighbors=1)
    from sklearn.cross_validation import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

    # import Matplotlib (scientific plotting library)
    import matplotlib.pyplot as plt

    # try K=1 through K=9 and record testing accuracy
    k_range = range(1, 9)
    # create Python dictionary using
    scores =

    for k in k_range:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    scores.append(metrics.accuracy_score(y_test, y_pred))

    # plot the relationship between K and testing accuracy
    # plt.plot(x_axis, y_axis)
    plt.plot(k_range, scores)
    plt.xlabel('Number of neighbors')
    plt.ylabel('Accuracy')


    The error message is -



    runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')
    Reloaded modules: __mp_main__
    Traceback (most recent call last):

    File "<ipython-input-3-1ba40d3637a3>", line 1, in <module>
    runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')

    File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

    File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

    File "C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py", line 11, in <module>
    knn.fit(X, y)

    File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 790, in fit
    return self._fit(X)

    File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 229, in _fit
    self.n_neighbors < self._fit_X.shape[0] // 2) and

    TypeError: '<' not supported between instances of 'list' and 'int'


    Please help me on this case so that I can improve my computing skills










    share|improve this question









    New contributor




    master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$



    put on hold as off-topic by Simon Larsson, Ethan, oW_ 2 hours ago


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "This question does not appear to be about data science, within the scope defined in the help center." – oW_

    If this question can be reworded to fit the rules in the help center, please edit the question.



















      -4












      -4








      -4





      $begingroup$


      May I know how to modify my Python programming so that can obtain the accuracy vs number of neighbours as refer to the attached image file -



      # read in the iris data
      from sklearn.datasets import load_iris
      iris = load_iris()
      # create X (features) and y (response)
      X = iris.data
      y = iris.target

      from sklearn.neighbors import KNeighborsClassifier
      knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)
      knn.fit(X, y)
      y_pred = knn.predict(X)

      from sklearn import metrics
      metrics.accuracy_score(y,y_pred)
      knn = KNeighborsClassifier(n_neighbors=1)
      from sklearn.cross_validation import train_test_split
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

      # import Matplotlib (scientific plotting library)
      import matplotlib.pyplot as plt

      # try K=1 through K=9 and record testing accuracy
      k_range = range(1, 9)
      # create Python dictionary using
      scores =

      for k in k_range:
      knn = KNeighborsClassifier(n_neighbors=k)
      knn.fit(X_train, y_train)
      y_pred = knn.predict(X_test)
      scores.append(metrics.accuracy_score(y_test, y_pred))

      # plot the relationship between K and testing accuracy
      # plt.plot(x_axis, y_axis)
      plt.plot(k_range, scores)
      plt.xlabel('Number of neighbors')
      plt.ylabel('Accuracy')


      The error message is -



      runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')
      Reloaded modules: __mp_main__
      Traceback (most recent call last):

      File "<ipython-input-3-1ba40d3637a3>", line 1, in <module>
      runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')

      File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 668, in runfile
      execfile(filename, namespace)

      File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 108, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)

      File "C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py", line 11, in <module>
      knn.fit(X, y)

      File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 790, in fit
      return self._fit(X)

      File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 229, in _fit
      self.n_neighbors < self._fit_X.shape[0] // 2) and

      TypeError: '<' not supported between instances of 'list' and 'int'


      Please help me on this case so that I can improve my computing skills










      share|improve this question









      New contributor




      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      May I know how to modify my Python programming so that can obtain the accuracy vs number of neighbours as refer to the attached image file -



      # read in the iris data
      from sklearn.datasets import load_iris
      iris = load_iris()
      # create X (features) and y (response)
      X = iris.data
      y = iris.target

      from sklearn.neighbors import KNeighborsClassifier
      knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)
      knn.fit(X, y)
      y_pred = knn.predict(X)

      from sklearn import metrics
      metrics.accuracy_score(y,y_pred)
      knn = KNeighborsClassifier(n_neighbors=1)
      from sklearn.cross_validation import train_test_split
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

      # import Matplotlib (scientific plotting library)
      import matplotlib.pyplot as plt

      # try K=1 through K=9 and record testing accuracy
      k_range = range(1, 9)
      # create Python dictionary using
      scores =

      for k in k_range:
      knn = KNeighborsClassifier(n_neighbors=k)
      knn.fit(X_train, y_train)
      y_pred = knn.predict(X_test)
      scores.append(metrics.accuracy_score(y_test, y_pred))

      # plot the relationship between K and testing accuracy
      # plt.plot(x_axis, y_axis)
      plt.plot(k_range, scores)
      plt.xlabel('Number of neighbors')
      plt.ylabel('Accuracy')


      The error message is -



      runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')
      Reloaded modules: __mp_main__
      Traceback (most recent call last):

      File "<ipython-input-3-1ba40d3637a3>", line 1, in <module>
      runfile('C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')

      File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 668, in runfile
      execfile(filename, namespace)

      File "C:UsersHSIPLAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 108, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)

      File "C:/Users/HSIPL/Desktop/Homework 8 Solution draft.py", line 11, in <module>
      knn.fit(X, y)

      File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 790, in fit
      return self._fit(X)

      File "C:UsersHSIPLAnaconda3libsite-packagessklearnneighborsbase.py", line 229, in _fit
      self.n_neighbors < self._fit_X.shape[0] // 2) and

      TypeError: '<' not supported between instances of 'list' and 'int'


      Please help me on this case so that I can improve my computing skills







      python k-nn ai






      share|improve this question









      New contributor




      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 9 mins ago







      master













      New contributor




      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      mastermaster

      12




      12




      New contributor




      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      master is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      put on hold as off-topic by Simon Larsson, Ethan, oW_ 2 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "This question does not appear to be about data science, within the scope defined in the help center." – oW_

      If this question can be reworded to fit the rules in the help center, please edit the question.







      put on hold as off-topic by Simon Larsson, Ethan, oW_ 2 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "This question does not appear to be about data science, within the scope defined in the help center." – oW_

      If this question can be reworded to fit the rules in the help center, please edit the question.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          Tip:



          Your problem is here:



          knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)


          You should always refer to documentation when you are using modules with high level of abstraction.



          I think it is important on this phase of your learning process to try a bit harder to read documentation. Also I will give you the answer because it is common even after years of experience to be blindfolded by frustration.



          Answer (Only look it after trying for at least 10 minutes with the tip)



          If we look at sklearn documentation at sklearn.neighbors.KNeighborsClassifier.html you can see the parameters that the KNeighborsClassifier constructor takes:




          n_neighbors : int, optional (default = 5)



          Number of neighbors to use by default for kneighbors queries.




          So, you should always use a single integer when calling this constructor. when you passed [1,2,3,4,5,6,7,8,9] the constructor did not notice that you passed a invalid parameter, that would happen in languages such as C and C++ that tests the parameters data type on function call.



          How to do it then?
          Well, you LOOP IT!



          for k in neighbors:
          knn = KNeighborsClassifier(n_neighbors=k,metric='minkowski', p=2)
          # <todo> ...


          Also, check on this tutorial for further clarification.






          share|improve this answer









          $endgroup$




















            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0












            $begingroup$

            Tip:



            Your problem is here:



            knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)


            You should always refer to documentation when you are using modules with high level of abstraction.



            I think it is important on this phase of your learning process to try a bit harder to read documentation. Also I will give you the answer because it is common even after years of experience to be blindfolded by frustration.



            Answer (Only look it after trying for at least 10 minutes with the tip)



            If we look at sklearn documentation at sklearn.neighbors.KNeighborsClassifier.html you can see the parameters that the KNeighborsClassifier constructor takes:




            n_neighbors : int, optional (default = 5)



            Number of neighbors to use by default for kneighbors queries.




            So, you should always use a single integer when calling this constructor. when you passed [1,2,3,4,5,6,7,8,9] the constructor did not notice that you passed a invalid parameter, that would happen in languages such as C and C++ that tests the parameters data type on function call.



            How to do it then?
            Well, you LOOP IT!



            for k in neighbors:
            knn = KNeighborsClassifier(n_neighbors=k,metric='minkowski', p=2)
            # <todo> ...


            Also, check on this tutorial for further clarification.






            share|improve this answer









            $endgroup$


















              0












              $begingroup$

              Tip:



              Your problem is here:



              knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)


              You should always refer to documentation when you are using modules with high level of abstraction.



              I think it is important on this phase of your learning process to try a bit harder to read documentation. Also I will give you the answer because it is common even after years of experience to be blindfolded by frustration.



              Answer (Only look it after trying for at least 10 minutes with the tip)



              If we look at sklearn documentation at sklearn.neighbors.KNeighborsClassifier.html you can see the parameters that the KNeighborsClassifier constructor takes:




              n_neighbors : int, optional (default = 5)



              Number of neighbors to use by default for kneighbors queries.




              So, you should always use a single integer when calling this constructor. when you passed [1,2,3,4,5,6,7,8,9] the constructor did not notice that you passed a invalid parameter, that would happen in languages such as C and C++ that tests the parameters data type on function call.



              How to do it then?
              Well, you LOOP IT!



              for k in neighbors:
              knn = KNeighborsClassifier(n_neighbors=k,metric='minkowski', p=2)
              # <todo> ...


              Also, check on this tutorial for further clarification.






              share|improve this answer









              $endgroup$
















                0












                0








                0





                $begingroup$

                Tip:



                Your problem is here:



                knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)


                You should always refer to documentation when you are using modules with high level of abstraction.



                I think it is important on this phase of your learning process to try a bit harder to read documentation. Also I will give you the answer because it is common even after years of experience to be blindfolded by frustration.



                Answer (Only look it after trying for at least 10 minutes with the tip)



                If we look at sklearn documentation at sklearn.neighbors.KNeighborsClassifier.html you can see the parameters that the KNeighborsClassifier constructor takes:




                n_neighbors : int, optional (default = 5)



                Number of neighbors to use by default for kneighbors queries.




                So, you should always use a single integer when calling this constructor. when you passed [1,2,3,4,5,6,7,8,9] the constructor did not notice that you passed a invalid parameter, that would happen in languages such as C and C++ that tests the parameters data type on function call.



                How to do it then?
                Well, you LOOP IT!



                for k in neighbors:
                knn = KNeighborsClassifier(n_neighbors=k,metric='minkowski', p=2)
                # <todo> ...


                Also, check on this tutorial for further clarification.






                share|improve this answer









                $endgroup$



                Tip:



                Your problem is here:



                knn = KNeighborsClassifier(n_neighbors=[1,2,3,4,5,6,7,8,9], metric='minkowski', p=2)


                You should always refer to documentation when you are using modules with high level of abstraction.



                I think it is important on this phase of your learning process to try a bit harder to read documentation. Also I will give you the answer because it is common even after years of experience to be blindfolded by frustration.



                Answer (Only look it after trying for at least 10 minutes with the tip)



                If we look at sklearn documentation at sklearn.neighbors.KNeighborsClassifier.html you can see the parameters that the KNeighborsClassifier constructor takes:




                n_neighbors : int, optional (default = 5)



                Number of neighbors to use by default for kneighbors queries.




                So, you should always use a single integer when calling this constructor. when you passed [1,2,3,4,5,6,7,8,9] the constructor did not notice that you passed a invalid parameter, that would happen in languages such as C and C++ that tests the parameters data type on function call.



                How to do it then?
                Well, you LOOP IT!



                for k in neighbors:
                knn = KNeighborsClassifier(n_neighbors=k,metric='minkowski', p=2)
                # <todo> ...


                Also, check on this tutorial for further clarification.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                Pedro Henrique MonfortePedro Henrique Monforte

                356111




                356111















                    Popular posts from this blog

                    Aikido

                    Tivadar Csontváry Kosztka

                    Metroo de Marsejlo