Why the output data result not the same for Random Forest












0












$begingroup$


May I know how to modify my Python programming thus it will be get the same result as refer to the attached image file?



import numpy as np
from sklearn import datasets as data
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
def load_data(feature_len = 4):
return data.load_iris()['data'][:, :feature_len], data.load_iris()['target']
def tr_te_split(data, target, test_ratio = .2):
return train_test_split(data, target, test_size=test_ratio, random_state=0)
def build_model():
return DecisionTreeClassifier(criterion='entropy'), RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1)
def train(model, x, y):
model.fit(x, y)
print("{} Training Accuracy = {}".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
return model
def evaluate(model, x, y):
print("{} Testing Accuracy = {}n".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
return accuracy_score(model.predict(x), y)
def main():
data, target = load_data()

tree_score =
rf_score =
for feature_len in range(1, data.shape[-1] + 1):
print("Use {} features.".format(feature_len))
data, target = load_data(feature_len)
x_tr, x_te, y_tr, y_te = tr_te_split(data, target, .4)

tree_model, rf_model = build_model()

tree_model_trained = train(tree_model, x_tr, y_tr)
tree_score.append(evaluate(tree_model_trained, x_te, y_te))

rf_model_trained = train(rf_model, x_tr, y_tr)
rf_score.append(evaluate(rf_model_trained, x_te, y_te))

# draw
tree_plt = plt
tree_plt.plot(tree_score)
tree_plt.xlabel('Number of features')
tree_plt.xticks([0,1,2,3],('1','2','3','4'))
tree_plt.ylabel('Accuracy')
tree_plt.title('Decision Tree')
tree_plt.show()

rf_plt = plt
rf_plt.plot(rf_score)
rf_plt.xlabel('Number of features')
rf_plt.xticks([0,1,2,3],('1','2','3','4'))
rf_plt.ylabel('Accuracy')
plt.yticks(np.arange(0.90, 0.95, 0.01))
rf_plt.title('Ransom Forest')
rf_plt.show()

if __name__ == '__main__':
main()


Please see the image file -



enter image description here



Please help me on this case









share







New contributor




vokoyo 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$


    May I know how to modify my Python programming thus it will be get the same result as refer to the attached image file?



    import numpy as np
    from sklearn import datasets as data
    from sklearn.model_selection import train_test_split
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.metrics import accuracy_score
    import matplotlib.pyplot as plt
    def load_data(feature_len = 4):
    return data.load_iris()['data'][:, :feature_len], data.load_iris()['target']
    def tr_te_split(data, target, test_ratio = .2):
    return train_test_split(data, target, test_size=test_ratio, random_state=0)
    def build_model():
    return DecisionTreeClassifier(criterion='entropy'), RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1)
    def train(model, x, y):
    model.fit(x, y)
    print("{} Training Accuracy = {}".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
    return model
    def evaluate(model, x, y):
    print("{} Testing Accuracy = {}n".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
    return accuracy_score(model.predict(x), y)
    def main():
    data, target = load_data()

    tree_score =
    rf_score =
    for feature_len in range(1, data.shape[-1] + 1):
    print("Use {} features.".format(feature_len))
    data, target = load_data(feature_len)
    x_tr, x_te, y_tr, y_te = tr_te_split(data, target, .4)

    tree_model, rf_model = build_model()

    tree_model_trained = train(tree_model, x_tr, y_tr)
    tree_score.append(evaluate(tree_model_trained, x_te, y_te))

    rf_model_trained = train(rf_model, x_tr, y_tr)
    rf_score.append(evaluate(rf_model_trained, x_te, y_te))

    # draw
    tree_plt = plt
    tree_plt.plot(tree_score)
    tree_plt.xlabel('Number of features')
    tree_plt.xticks([0,1,2,3],('1','2','3','4'))
    tree_plt.ylabel('Accuracy')
    tree_plt.title('Decision Tree')
    tree_plt.show()

    rf_plt = plt
    rf_plt.plot(rf_score)
    rf_plt.xlabel('Number of features')
    rf_plt.xticks([0,1,2,3],('1','2','3','4'))
    rf_plt.ylabel('Accuracy')
    plt.yticks(np.arange(0.90, 0.95, 0.01))
    rf_plt.title('Ransom Forest')
    rf_plt.show()

    if __name__ == '__main__':
    main()


    Please see the image file -



    enter image description here



    Please help me on this case









    share







    New contributor




    vokoyo 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$


      May I know how to modify my Python programming thus it will be get the same result as refer to the attached image file?



      import numpy as np
      from sklearn import datasets as data
      from sklearn.model_selection import train_test_split
      from sklearn.tree import DecisionTreeClassifier
      from sklearn.ensemble import RandomForestClassifier
      from sklearn.metrics import accuracy_score
      import matplotlib.pyplot as plt
      def load_data(feature_len = 4):
      return data.load_iris()['data'][:, :feature_len], data.load_iris()['target']
      def tr_te_split(data, target, test_ratio = .2):
      return train_test_split(data, target, test_size=test_ratio, random_state=0)
      def build_model():
      return DecisionTreeClassifier(criterion='entropy'), RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1)
      def train(model, x, y):
      model.fit(x, y)
      print("{} Training Accuracy = {}".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
      return model
      def evaluate(model, x, y):
      print("{} Testing Accuracy = {}n".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
      return accuracy_score(model.predict(x), y)
      def main():
      data, target = load_data()

      tree_score =
      rf_score =
      for feature_len in range(1, data.shape[-1] + 1):
      print("Use {} features.".format(feature_len))
      data, target = load_data(feature_len)
      x_tr, x_te, y_tr, y_te = tr_te_split(data, target, .4)

      tree_model, rf_model = build_model()

      tree_model_trained = train(tree_model, x_tr, y_tr)
      tree_score.append(evaluate(tree_model_trained, x_te, y_te))

      rf_model_trained = train(rf_model, x_tr, y_tr)
      rf_score.append(evaluate(rf_model_trained, x_te, y_te))

      # draw
      tree_plt = plt
      tree_plt.plot(tree_score)
      tree_plt.xlabel('Number of features')
      tree_plt.xticks([0,1,2,3],('1','2','3','4'))
      tree_plt.ylabel('Accuracy')
      tree_plt.title('Decision Tree')
      tree_plt.show()

      rf_plt = plt
      rf_plt.plot(rf_score)
      rf_plt.xlabel('Number of features')
      rf_plt.xticks([0,1,2,3],('1','2','3','4'))
      rf_plt.ylabel('Accuracy')
      plt.yticks(np.arange(0.90, 0.95, 0.01))
      rf_plt.title('Ransom Forest')
      rf_plt.show()

      if __name__ == '__main__':
      main()


      Please see the image file -



      enter image description here



      Please help me on this case









      share







      New contributor




      vokoyo 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 thus it will be get the same result as refer to the attached image file?



      import numpy as np
      from sklearn import datasets as data
      from sklearn.model_selection import train_test_split
      from sklearn.tree import DecisionTreeClassifier
      from sklearn.ensemble import RandomForestClassifier
      from sklearn.metrics import accuracy_score
      import matplotlib.pyplot as plt
      def load_data(feature_len = 4):
      return data.load_iris()['data'][:, :feature_len], data.load_iris()['target']
      def tr_te_split(data, target, test_ratio = .2):
      return train_test_split(data, target, test_size=test_ratio, random_state=0)
      def build_model():
      return DecisionTreeClassifier(criterion='entropy'), RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1)
      def train(model, x, y):
      model.fit(x, y)
      print("{} Training Accuracy = {}".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
      return model
      def evaluate(model, x, y):
      print("{} Testing Accuracy = {}n".format(str(model.__class__).split('.')[-1].split("'")[0], model.score(x, y)))
      return accuracy_score(model.predict(x), y)
      def main():
      data, target = load_data()

      tree_score =
      rf_score =
      for feature_len in range(1, data.shape[-1] + 1):
      print("Use {} features.".format(feature_len))
      data, target = load_data(feature_len)
      x_tr, x_te, y_tr, y_te = tr_te_split(data, target, .4)

      tree_model, rf_model = build_model()

      tree_model_trained = train(tree_model, x_tr, y_tr)
      tree_score.append(evaluate(tree_model_trained, x_te, y_te))

      rf_model_trained = train(rf_model, x_tr, y_tr)
      rf_score.append(evaluate(rf_model_trained, x_te, y_te))

      # draw
      tree_plt = plt
      tree_plt.plot(tree_score)
      tree_plt.xlabel('Number of features')
      tree_plt.xticks([0,1,2,3],('1','2','3','4'))
      tree_plt.ylabel('Accuracy')
      tree_plt.title('Decision Tree')
      tree_plt.show()

      rf_plt = plt
      rf_plt.plot(rf_score)
      rf_plt.xlabel('Number of features')
      rf_plt.xticks([0,1,2,3],('1','2','3','4'))
      rf_plt.ylabel('Accuracy')
      plt.yticks(np.arange(0.90, 0.95, 0.01))
      rf_plt.title('Ransom Forest')
      rf_plt.show()

      if __name__ == '__main__':
      main()


      Please see the image file -



      enter image description here



      Please help me on this case







      machine-learning python random-forest





      share







      New contributor




      vokoyo 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




      vokoyo 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




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









      asked 4 mins ago









      vokoyovokoyo

      11




      11




      New contributor




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





      New contributor





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






      vokoyo 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.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
          });


          }
          });






          vokoyo 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%2f49224%2fwhy-the-output-data-result-not-the-same-for-random-forest%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








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










          draft saved

          draft discarded


















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













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












          vokoyo 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%2f49224%2fwhy-the-output-data-result-not-the-same-for-random-forest%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

          Aikido

          Tivadar Csontváry Kosztka

          Metroo de Marsejlo