{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"\n\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.datasets import make_classification\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.decomposition import PCA\nimport time\n\n\ndef random_forest_classifier(features, target):\n    \"\"\"\n    To train the random forest classifier with features and target data\n    :param features:\n    :param target:\n    :return: trained random forest classifier\n    \"\"\"\n    clf = RandomForestClassifier()\n    clf.fit(features, target)\n    return clf\n\n    \ndef main():\n    #Assignment Step 1: \n    # Reading Input Train Data\n    train_data = pd.read_csv('../input/train.csv')\n    train_y = train_data[train_data.columns[0]]\n    train_x = train_data[train_data.columns[1::]]\n    print (train_x.head(2))\n    # Reading Input Test Data\n    test_data = pd.read_csv('../input/test.csv')\n    test_x = test_data\n    \n    # Assignment step 2\n    combined_x = pd.concat([train_x,test_x])\n    print(combined_x.shape)\n    \n    pca = PCA(n_components=780)\n    pca.fit(combined_x)\n    \n    # Explained Variance Per Component\n    evpc_array = pca.explained_variance_ratio_\n    total_variance_explained = 0\n    for index,evp in enumerate(evpc_array):\n        total_variance_explained += evp\n        print(index,total_variance_explained*100)\n        if total_variance_explained >= 0.995:\n            n_comps_for_95 = index + 1\n            break\n    # Number of PCA components for 95%\n    print  (n_comps_for_95)       \n    pca = PCA(n_components=n_comps_for_95)\n    pca.fit(combined_x)\n    print(\"Explained Variance:\", sum(pca.explained_variance_ratio_))\n    \n    # Transform Train Data\n    train_x = pca.transform(train_x)\n    \n    print (test_data.head(2))\n    # Start time\n    start_t = time.time()\n    # Create random forest classifier instance\n    trained_model = random_forest_classifier(train_x, train_y)\n    # End time\n    end_t = time.time()\n\n    print (\"Trained model :: \", trained_model)\n    print (\"time collapsed:,\", end_t - start_t)\n    \n    # Test\n    predictions = trained_model.predict(pca.transform(test_x))\n    # Train and Test Accuracy\n    print (\"Train Accuracy :: \", accuracy_score(train_y, trained_model.predict(train_x)))\n    \n    print (type(predictions))\n    print('Predictions: %s' % predictions)\n    with open('submission.csv','w') as f:\n        f.write(\"ImageId,Label\\n\")\n        for index,pred in enumerate(predictions):\n            f.write(\"%d,%d\\n\" % (index+1,pred))\n        \n    \n        \n    #pred_data = pd.DataFrame(data = predictions)\n    #pd.DataFrame(data=data[1:,1:], index=data[1:,0], columns=data[0,1:]) \n    #pred_data.to_csv('../submission.csv')\n\n\nif __name__ == \"__main__\":\n    main()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}