SciKit-Learn Decision Tree Overfitting












1












$begingroup$


I'm pursuing a computer science minor at my university, and one class I'm in is Machine Learning.
We have a project to utilize a few algorithms we have learned so far.
I've been using SciKit-Learn to perform these algorithms, but when it comes to decision trees I keep getting a feeling I am overfitting.



I'm using a dataset about weather, giving characteristics such as city, state, month, year, wind direction, wind speed, etc... where the target variable is the average temperature for the day. Now I know this is hard to classify, as it is pretty much a continuous variable space, but I've simplified it to the predicted being within a range of 5 to the actual.



Here is a link to the csv I'm using



The following is my code:



address2 = 'C:/.../weather.csv'
weather = pd.read_csv(address2)

cityCode= le.fit_transform(weather.iloc[:,2])
windDirection = le.fit_transform(weather.iloc[:,3])
month = le.fit_transform(weather.iloc[:,8])
precip = le.fit_transform(weather.iloc[:,9])
windSpeed = le.fit_transform(weather.iloc[:,10])
state = le.fit_transform(weather.iloc[:,11])
week = le.fit_transform(weather.iloc[:,12])
year = le.fit_transform(weather.iloc[:,13])

Xweather = list(zip(cityCode,windDirection,month,precip,windSpeed,state,week,year))
yweather= weather.iloc[:,0]

yweather_test = train_test_split(Xweather, y, test_size = 0.2, random_state=413)

cWeather = tree.DecisionTreeClassifier()
cWeather.fit(Xweather_train,yweather_train)
accu_train_weather=np.sum(abs(cWeather.predict(Xweather_train)-yweather_train)<=5)/float(yweather_train.size)*100
accu_test_weather=np.sum(abs(cWeather.predict(Xweather_test)-yweather_test)<=5)/float(yweather_test.size)*100
print("Classificaton accuracy on training set", accu_train_weather, "%")
print("Classificaton accuracy on test set", accu_test_weather, "%")


My training set constantly gets 100% training accuracy, but the test set is constantly 57% accurate, which leads me to believe the tree is overfitting to the training set.



I know I'm not doing any pruning, but even when I do, I can get the same test accuracy as unpruned at best.
By pruning I mean setting the tree classifier to have a maximum number of leaves, minimum samples per leaf, and maximum depth.



I'm not an advanced coder (as you can probably tell by my code), but any help would be great.










share|improve this question







New contributor




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







$endgroup$

















    1












    $begingroup$


    I'm pursuing a computer science minor at my university, and one class I'm in is Machine Learning.
    We have a project to utilize a few algorithms we have learned so far.
    I've been using SciKit-Learn to perform these algorithms, but when it comes to decision trees I keep getting a feeling I am overfitting.



    I'm using a dataset about weather, giving characteristics such as city, state, month, year, wind direction, wind speed, etc... where the target variable is the average temperature for the day. Now I know this is hard to classify, as it is pretty much a continuous variable space, but I've simplified it to the predicted being within a range of 5 to the actual.



    Here is a link to the csv I'm using



    The following is my code:



    address2 = 'C:/.../weather.csv'
    weather = pd.read_csv(address2)

    cityCode= le.fit_transform(weather.iloc[:,2])
    windDirection = le.fit_transform(weather.iloc[:,3])
    month = le.fit_transform(weather.iloc[:,8])
    precip = le.fit_transform(weather.iloc[:,9])
    windSpeed = le.fit_transform(weather.iloc[:,10])
    state = le.fit_transform(weather.iloc[:,11])
    week = le.fit_transform(weather.iloc[:,12])
    year = le.fit_transform(weather.iloc[:,13])

    Xweather = list(zip(cityCode,windDirection,month,precip,windSpeed,state,week,year))
    yweather= weather.iloc[:,0]

    yweather_test = train_test_split(Xweather, y, test_size = 0.2, random_state=413)

    cWeather = tree.DecisionTreeClassifier()
    cWeather.fit(Xweather_train,yweather_train)
    accu_train_weather=np.sum(abs(cWeather.predict(Xweather_train)-yweather_train)<=5)/float(yweather_train.size)*100
    accu_test_weather=np.sum(abs(cWeather.predict(Xweather_test)-yweather_test)<=5)/float(yweather_test.size)*100
    print("Classificaton accuracy on training set", accu_train_weather, "%")
    print("Classificaton accuracy on test set", accu_test_weather, "%")


    My training set constantly gets 100% training accuracy, but the test set is constantly 57% accurate, which leads me to believe the tree is overfitting to the training set.



    I know I'm not doing any pruning, but even when I do, I can get the same test accuracy as unpruned at best.
    By pruning I mean setting the tree classifier to have a maximum number of leaves, minimum samples per leaf, and maximum depth.



    I'm not an advanced coder (as you can probably tell by my code), but any help would be great.










    share|improve this question







    New contributor




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







    $endgroup$















      1












      1








      1


      1



      $begingroup$


      I'm pursuing a computer science minor at my university, and one class I'm in is Machine Learning.
      We have a project to utilize a few algorithms we have learned so far.
      I've been using SciKit-Learn to perform these algorithms, but when it comes to decision trees I keep getting a feeling I am overfitting.



      I'm using a dataset about weather, giving characteristics such as city, state, month, year, wind direction, wind speed, etc... where the target variable is the average temperature for the day. Now I know this is hard to classify, as it is pretty much a continuous variable space, but I've simplified it to the predicted being within a range of 5 to the actual.



      Here is a link to the csv I'm using



      The following is my code:



      address2 = 'C:/.../weather.csv'
      weather = pd.read_csv(address2)

      cityCode= le.fit_transform(weather.iloc[:,2])
      windDirection = le.fit_transform(weather.iloc[:,3])
      month = le.fit_transform(weather.iloc[:,8])
      precip = le.fit_transform(weather.iloc[:,9])
      windSpeed = le.fit_transform(weather.iloc[:,10])
      state = le.fit_transform(weather.iloc[:,11])
      week = le.fit_transform(weather.iloc[:,12])
      year = le.fit_transform(weather.iloc[:,13])

      Xweather = list(zip(cityCode,windDirection,month,precip,windSpeed,state,week,year))
      yweather= weather.iloc[:,0]

      yweather_test = train_test_split(Xweather, y, test_size = 0.2, random_state=413)

      cWeather = tree.DecisionTreeClassifier()
      cWeather.fit(Xweather_train,yweather_train)
      accu_train_weather=np.sum(abs(cWeather.predict(Xweather_train)-yweather_train)<=5)/float(yweather_train.size)*100
      accu_test_weather=np.sum(abs(cWeather.predict(Xweather_test)-yweather_test)<=5)/float(yweather_test.size)*100
      print("Classificaton accuracy on training set", accu_train_weather, "%")
      print("Classificaton accuracy on test set", accu_test_weather, "%")


      My training set constantly gets 100% training accuracy, but the test set is constantly 57% accurate, which leads me to believe the tree is overfitting to the training set.



      I know I'm not doing any pruning, but even when I do, I can get the same test accuracy as unpruned at best.
      By pruning I mean setting the tree classifier to have a maximum number of leaves, minimum samples per leaf, and maximum depth.



      I'm not an advanced coder (as you can probably tell by my code), but any help would be great.










      share|improve this question







      New contributor




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







      $endgroup$




      I'm pursuing a computer science minor at my university, and one class I'm in is Machine Learning.
      We have a project to utilize a few algorithms we have learned so far.
      I've been using SciKit-Learn to perform these algorithms, but when it comes to decision trees I keep getting a feeling I am overfitting.



      I'm using a dataset about weather, giving characteristics such as city, state, month, year, wind direction, wind speed, etc... where the target variable is the average temperature for the day. Now I know this is hard to classify, as it is pretty much a continuous variable space, but I've simplified it to the predicted being within a range of 5 to the actual.



      Here is a link to the csv I'm using



      The following is my code:



      address2 = 'C:/.../weather.csv'
      weather = pd.read_csv(address2)

      cityCode= le.fit_transform(weather.iloc[:,2])
      windDirection = le.fit_transform(weather.iloc[:,3])
      month = le.fit_transform(weather.iloc[:,8])
      precip = le.fit_transform(weather.iloc[:,9])
      windSpeed = le.fit_transform(weather.iloc[:,10])
      state = le.fit_transform(weather.iloc[:,11])
      week = le.fit_transform(weather.iloc[:,12])
      year = le.fit_transform(weather.iloc[:,13])

      Xweather = list(zip(cityCode,windDirection,month,precip,windSpeed,state,week,year))
      yweather= weather.iloc[:,0]

      yweather_test = train_test_split(Xweather, y, test_size = 0.2, random_state=413)

      cWeather = tree.DecisionTreeClassifier()
      cWeather.fit(Xweather_train,yweather_train)
      accu_train_weather=np.sum(abs(cWeather.predict(Xweather_train)-yweather_train)<=5)/float(yweather_train.size)*100
      accu_test_weather=np.sum(abs(cWeather.predict(Xweather_test)-yweather_test)<=5)/float(yweather_test.size)*100
      print("Classificaton accuracy on training set", accu_train_weather, "%")
      print("Classificaton accuracy on test set", accu_test_weather, "%")


      My training set constantly gets 100% training accuracy, but the test set is constantly 57% accurate, which leads me to believe the tree is overfitting to the training set.



      I know I'm not doing any pruning, but even when I do, I can get the same test accuracy as unpruned at best.
      By pruning I mean setting the tree classifier to have a maximum number of leaves, minimum samples per leaf, and maximum depth.



      I'm not an advanced coder (as you can probably tell by my code), but any help would be great.







      machine-learning python scikit-learn decision-trees overfitting






      share|improve this question







      New contributor




      Paulfryy 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




      Paulfryy 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






      New contributor




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









      asked 2 hours ago









      PaulfryyPaulfryy

      61




      61




      New contributor




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





      New contributor





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






      Paulfryy 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
          });


          }
          });






          Paulfryy 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%2f45319%2fscikit-learn-decision-tree-overfitting%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








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










          draft saved

          draft discarded


















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













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












          Paulfryy 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%2f45319%2fscikit-learn-decision-tree-overfitting%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