How to modify the Python programming - Support Vector Machine












0












$begingroup$


Using the SVC algorithm implemented by the Python Scikit-learn, classify the three types of flowers (Setosa, Versicolor, Virgin) in Iris dataset according to the Petal length and width



May I know how to modify my Python programming as refer to the attached file -



# To Get iris dataset
from sklearn import datasets
# To fit the svm classifier
from sklearn import svm
import numpy as np
import matplotlib.pyplot as plt

iris_dataset = datasets.load_iris()

def visuvalise_petal_data():
iris = datasets.load_iris()
# Only take the first two features
X = iris.data[:, 2:3]
y = iris.target

visuvalise_petal_data()
iris = datasets.load_iris()
# Only take the Sepal two features
X = iris.data[:, 2:3]
y = iris.target
# SVM regularization parameter

# SVC with rbf kernel
rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=1).fit(X, y)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=10).fit(X, y)
# step size in the mesh
h = 0.02
# create a mesh to plot in
def plotSVC(title):
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.subplot(1, 1, 1)
Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

C = [1, 10]
for c in cs:
svc = svm.SVC(kernel='rbf', C=1).fit(X, y)
svc = svm.SVC(kernel='rbf', C=10).fit(X, y)
plotSVC('C=' + str(c))

from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 100, random_state = 0)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

linear_svm1 = SVC(kernel = 'rbf', C = 1, random_state = 0)
linear_svm1.fit(X_train_std, y_train)
y_predict1 = linear_svm1.predict(X_test_std)
print('Gamma=0.01,C=1')

linear_svm2 = SVC(kernel = 'rbf', C = 10, random_state = 0)
linear_svm2.fit(X_train_std, y_train)
y_predict2 = linear_svm2.predict(X_test_std)
print('Gamma=0.01,C=10')

svm = SVC(kernel='linear', C=1.0, random_state=0)
svm.fit(X_train_std, y_train)
plot_decision_regions(X, y, classifier=svm, test_idx=range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()


The error message is -



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

File "<ipython-input-85-761bed922ac3>", line 1, in <module>
runfile('C:/Users/HSIPL/Desktop/Homework 6 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 6 Solution draft.py", line 44, in <module>
plotSVC('C=' + str(c))

File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 32, in plotSVC
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

IndexError: index 1 is out of bounds for axis 1 with size 1


enter image description here



Please help so that I can improve my computing skills









share







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$

















    0












    $begingroup$


    Using the SVC algorithm implemented by the Python Scikit-learn, classify the three types of flowers (Setosa, Versicolor, Virgin) in Iris dataset according to the Petal length and width



    May I know how to modify my Python programming as refer to the attached file -



    # To Get iris dataset
    from sklearn import datasets
    # To fit the svm classifier
    from sklearn import svm
    import numpy as np
    import matplotlib.pyplot as plt

    iris_dataset = datasets.load_iris()

    def visuvalise_petal_data():
    iris = datasets.load_iris()
    # Only take the first two features
    X = iris.data[:, 2:3]
    y = iris.target

    visuvalise_petal_data()
    iris = datasets.load_iris()
    # Only take the Sepal two features
    X = iris.data[:, 2:3]
    y = iris.target
    # SVM regularization parameter

    # SVC with rbf kernel
    rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=1).fit(X, y)
    rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=10).fit(X, y)
    # step size in the mesh
    h = 0.02
    # create a mesh to plot in
    def plotSVC(title):
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    h = (x_max / x_min)/100
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
    np.arange(y_min, y_max, h))
    plt.subplot(1, 1, 1)
    Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    C = [1, 10]
    for c in cs:
    svc = svm.SVC(kernel='rbf', C=1).fit(X, y)
    svc = svm.SVC(kernel='rbf', C=10).fit(X, y)
    plotSVC('C=' + str(c))

    from sklearn.svm import SVC
    from sklearn.preprocessing import StandardScaler
    from sklearn.cross_validation import train_test_split

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 100, random_state = 0)

    sc = StandardScaler()
    sc.fit(X_train)
    X_train_std = sc.transform(X_train)
    X_test_std = sc.transform(X_test)

    linear_svm1 = SVC(kernel = 'rbf', C = 1, random_state = 0)
    linear_svm1.fit(X_train_std, y_train)
    y_predict1 = linear_svm1.predict(X_test_std)
    print('Gamma=0.01,C=1')

    linear_svm2 = SVC(kernel = 'rbf', C = 10, random_state = 0)
    linear_svm2.fit(X_train_std, y_train)
    y_predict2 = linear_svm2.predict(X_test_std)
    print('Gamma=0.01,C=10')

    svm = SVC(kernel='linear', C=1.0, random_state=0)
    svm.fit(X_train_std, y_train)
    plot_decision_regions(X, y, classifier=svm, test_idx=range(105,150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()


    The error message is -



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

    File "<ipython-input-85-761bed922ac3>", line 1, in <module>
    runfile('C:/Users/HSIPL/Desktop/Homework 6 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 6 Solution draft.py", line 44, in <module>
    plotSVC('C=' + str(c))

    File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 32, in plotSVC
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

    IndexError: index 1 is out of bounds for axis 1 with size 1


    enter image description here



    Please help so that I can improve my computing skills









    share







    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$















      0












      0








      0





      $begingroup$


      Using the SVC algorithm implemented by the Python Scikit-learn, classify the three types of flowers (Setosa, Versicolor, Virgin) in Iris dataset according to the Petal length and width



      May I know how to modify my Python programming as refer to the attached file -



      # To Get iris dataset
      from sklearn import datasets
      # To fit the svm classifier
      from sklearn import svm
      import numpy as np
      import matplotlib.pyplot as plt

      iris_dataset = datasets.load_iris()

      def visuvalise_petal_data():
      iris = datasets.load_iris()
      # Only take the first two features
      X = iris.data[:, 2:3]
      y = iris.target

      visuvalise_petal_data()
      iris = datasets.load_iris()
      # Only take the Sepal two features
      X = iris.data[:, 2:3]
      y = iris.target
      # SVM regularization parameter

      # SVC with rbf kernel
      rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=1).fit(X, y)
      rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=10).fit(X, y)
      # step size in the mesh
      h = 0.02
      # create a mesh to plot in
      def plotSVC(title):
      x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
      y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
      h = (x_max / x_min)/100
      xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
      np.arange(y_min, y_max, h))
      plt.subplot(1, 1, 1)
      Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
      Z = Z.reshape(xx.shape)

      C = [1, 10]
      for c in cs:
      svc = svm.SVC(kernel='rbf', C=1).fit(X, y)
      svc = svm.SVC(kernel='rbf', C=10).fit(X, y)
      plotSVC('C=' + str(c))

      from sklearn.svm import SVC
      from sklearn.preprocessing import StandardScaler
      from sklearn.cross_validation import train_test_split

      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 100, random_state = 0)

      sc = StandardScaler()
      sc.fit(X_train)
      X_train_std = sc.transform(X_train)
      X_test_std = sc.transform(X_test)

      linear_svm1 = SVC(kernel = 'rbf', C = 1, random_state = 0)
      linear_svm1.fit(X_train_std, y_train)
      y_predict1 = linear_svm1.predict(X_test_std)
      print('Gamma=0.01,C=1')

      linear_svm2 = SVC(kernel = 'rbf', C = 10, random_state = 0)
      linear_svm2.fit(X_train_std, y_train)
      y_predict2 = linear_svm2.predict(X_test_std)
      print('Gamma=0.01,C=10')

      svm = SVC(kernel='linear', C=1.0, random_state=0)
      svm.fit(X_train_std, y_train)
      plot_decision_regions(X, y, classifier=svm, test_idx=range(105,150))
      plt.xlabel('petal length [standardized]')
      plt.ylabel('petal width [standardized]')
      plt.legend(loc='upper left')
      plt.show()


      The error message is -



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

      File "<ipython-input-85-761bed922ac3>", line 1, in <module>
      runfile('C:/Users/HSIPL/Desktop/Homework 6 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 6 Solution draft.py", line 44, in <module>
      plotSVC('C=' + str(c))

      File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 32, in plotSVC
      y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

      IndexError: index 1 is out of bounds for axis 1 with size 1


      enter image description here



      Please help so that I can improve my computing skills









      share







      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$




      Using the SVC algorithm implemented by the Python Scikit-learn, classify the three types of flowers (Setosa, Versicolor, Virgin) in Iris dataset according to the Petal length and width



      May I know how to modify my Python programming as refer to the attached file -



      # To Get iris dataset
      from sklearn import datasets
      # To fit the svm classifier
      from sklearn import svm
      import numpy as np
      import matplotlib.pyplot as plt

      iris_dataset = datasets.load_iris()

      def visuvalise_petal_data():
      iris = datasets.load_iris()
      # Only take the first two features
      X = iris.data[:, 2:3]
      y = iris.target

      visuvalise_petal_data()
      iris = datasets.load_iris()
      # Only take the Sepal two features
      X = iris.data[:, 2:3]
      y = iris.target
      # SVM regularization parameter

      # SVC with rbf kernel
      rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=1).fit(X, y)
      rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=10).fit(X, y)
      # step size in the mesh
      h = 0.02
      # create a mesh to plot in
      def plotSVC(title):
      x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
      y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
      h = (x_max / x_min)/100
      xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
      np.arange(y_min, y_max, h))
      plt.subplot(1, 1, 1)
      Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
      Z = Z.reshape(xx.shape)

      C = [1, 10]
      for c in cs:
      svc = svm.SVC(kernel='rbf', C=1).fit(X, y)
      svc = svm.SVC(kernel='rbf', C=10).fit(X, y)
      plotSVC('C=' + str(c))

      from sklearn.svm import SVC
      from sklearn.preprocessing import StandardScaler
      from sklearn.cross_validation import train_test_split

      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 100, random_state = 0)

      sc = StandardScaler()
      sc.fit(X_train)
      X_train_std = sc.transform(X_train)
      X_test_std = sc.transform(X_test)

      linear_svm1 = SVC(kernel = 'rbf', C = 1, random_state = 0)
      linear_svm1.fit(X_train_std, y_train)
      y_predict1 = linear_svm1.predict(X_test_std)
      print('Gamma=0.01,C=1')

      linear_svm2 = SVC(kernel = 'rbf', C = 10, random_state = 0)
      linear_svm2.fit(X_train_std, y_train)
      y_predict2 = linear_svm2.predict(X_test_std)
      print('Gamma=0.01,C=10')

      svm = SVC(kernel='linear', C=1.0, random_state=0)
      svm.fit(X_train_std, y_train)
      plot_decision_regions(X, y, classifier=svm, test_idx=range(105,150))
      plt.xlabel('petal length [standardized]')
      plt.ylabel('petal width [standardized]')
      plt.legend(loc='upper left')
      plt.show()


      The error message is -



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

      File "<ipython-input-85-761bed922ac3>", line 1, in <module>
      runfile('C:/Users/HSIPL/Desktop/Homework 6 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 6 Solution draft.py", line 44, in <module>
      plotSVC('C=' + str(c))

      File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 32, in plotSVC
      y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

      IndexError: index 1 is out of bounds for axis 1 with size 1


      enter image description here



      Please help so that I can improve my computing skills







      python svm ai





      share







      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







      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



      share






      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 3 mins ago









      mastermaster

      1




      1




      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.






















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          master is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48561%2fhow-to-modify-the-python-programming-support-vector-machine%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          master is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          master is a new contributor. Be nice, and check out our Code of Conduct.













          master is a new contributor. Be nice, and check out our Code of Conduct.












          master is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48561%2fhow-to-modify-the-python-programming-support-vector-machine%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár