{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"source":"#!/usr/bin/env python3\n'''\nRandomforest - plot: # of trees - accuracy\n\nModifications by: Noah Carter\nDate 4/25/16\nOriginal Author: Hideki Ikeda\nDate 7/11/15\n'''\n#first, uses an outer loop of training set size and an inner loop of forest size; outputs the results\n#then, outputs the graph of the highest forest size's performance for different training set sizes\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.cross_validation import cross_val_score\n\ndef main():\n    # loading training data\n    print('Loading training data')\n    data = pd.read_csv('../input/train.csv')\n    fig,axs=plt.subplots(1,2,figsize=(20,14))\n\n    n_trees = [10, 15, 20, 25, 30, 40, 50]#, 70, 100, 150]\n\n    \n    performTests(data,42000,12,n_trees,True,True,axs)\n    \n    \ndef plotByNumberOfTrees(n_trees,scores,sc_array,std_array,showStd,axs):\n    \n    \n    pd.Series(scores, index=n_trees).plot(ax=axs[0])\n    if (showStd):\n        pd.Series(sc_array + std_array,index=n_trees).plot(style=\"b--\",ax=axs[0])\n        pd.Series(sc_array - std_array,index=n_trees).plot(style=\"b--\",ax=axs[0])\n    axs[0].set_xlabel(\"# of trees\")\n    axs[0].set_ylabel(\"CV score\")\n    axs[0].set_title(\"Number of Trees vs. Model Accuracy for Different Training Set Sizes\")\n    ###plt.savefig('cv_trees.png')\ndef plotByTrainingSetSize(n_obsInTrainingSet,scores,sc_array,std_array,showStd,axs):\n    pd.Series(scores, index=n_obsInTrainingSet).plot(ax=axs[1])\n    if (showStd):\n        pd.Series(sc_array + std_array,index=n_obsInTrainingSet).plot(style=\"b--\",ax=axs[1])\n        pd.Series(sc_array - std_array,index=n_obsInTrainingSet).plot(style=\"b--\",ax=axs[1])\n    axs[1].set_xlabel(\"Size of Training Set\")\n    axs[1].set_ylabel(\"CV score\")\n    axs[1].set_title(\"Training Set Size vs. Model Accuracy (for the Best Measured Model: the Largest Forest)\")\n    #######plt.savefig('cv_trees2.png')\n    \ndef trainAndTest(n_trees,X_tr,y_tr,scores,scores_std):\n    for n_tree in n_trees:\n        print(n_tree)\n        recognizer = RandomForestClassifier(n_tree)\n        score = cross_val_score(recognizer, X_tr, y_tr)\n        scores.append(np.mean(score))\n        scores_std.append(np.std(score))\ndef performTests(data,numberOfObservations,numberOfDivisions,n_trees,showStd1,showStd2,axs):\n    sizeOfGap=int(numberOfObservations/numberOfDivisions)\n    \n    scoresListByTrainingSet=list()\n    stdListByTrainingSet=list()\n    n_obsInTrainingSet=list(range(sizeOfGap,numberOfObservations+1,sizeOfGap))\n    #f1=plt.figure() #create a new figure\n\n    for i in range(sizeOfGap,numberOfObservations+1,sizeOfGap):\n        X_tr = data.values[:i, 1:].astype(float)\n        y_tr = data.values[:i, 0]\n        \n        scores = list()\n        scores_std = list()\n        \n        print('Start learning...')\n                \n        trainAndTest(n_trees,X_tr,y_tr,scores,scores_std)\n        \n        sc_array = np.array(scores)\n        std_array = np.array(scores_std)\n        print('Score: ', sc_array)\n        print('Std  : ', std_array)\n        \n        plotByNumberOfTrees(n_trees,scores,sc_array,std_array,showStd1,axs)\n        \n        scoresListByTrainingSet.append(scores[len(n_trees)-1])  #store the mean and std performance\n        stdListByTrainingSet.append(scores_std[len(n_trees)-1])      #for the largest forest size on this\n                                                                     #training set size\n\n    scoresByTrainingSet=np.array(scoresListByTrainingSet)\n    stdByTrainingSet=np.array(stdListByTrainingSet)\n    print(\"Scores by Training Set Size: \",scoresByTrainingSet)\n    print(\"Standard deviations of Scores by Training Set Size: \",stdByTrainingSet)\n    plotByTrainingSetSize(n_obsInTrainingSet,scoresListByTrainingSet,scoresByTrainingSet,stdByTrainingSet,showStd2,axs)\n        \nif __name__ == '__main__':\n    main()\n"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"source":""}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":0}