{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "import numpy as np\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from sklearn.preprocessing import MultiLabelBinarizer\n", "from sklearn.multiclass import OneVsRestClassifier\n", "from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier\n", "from sklearn.metrics import r2_score, roc_auc_score\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Read Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "path = ''" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "dataTraining = pd.read_csv(os.path.join(path, 'data', 'dataTraining.csv'), encoding='UTF-8', index_col=0)\n", "dataTesting = pd.read_csv(os.path.join(path, 'data', 'dataTesting.csv'), encoding='UTF-8', index_col=0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yeartitleplot
11999Message in a Bottlewho meets by fate , shall be sealed by fate ....
41978Midnight Expressthe true story of billy hayes , an american c...
51996Primal Fearmartin vail left the chicago da ' s office to ...
61950Crisishusband and wife americans dr . eugene and mr...
71959The Tinglerthe coroner and scientist dr . warren chapin ...
\n", "
" ], "text/plain": [ " year title \\\n", "1 1999 Message in a Bottle \n", "4 1978 Midnight Express \n", "5 1996 Primal Fear \n", "6 1950 Crisis \n", "7 1959 The Tingler \n", "\n", " plot \n", "1 who meets by fate , shall be sealed by fate .... \n", "4 the true story of billy hayes , an american c... \n", "5 martin vail left the chicago da ' s office to ... \n", "6 husband and wife americans dr . eugene and mr... \n", "7 the coroner and scientist dr . warren chapin ... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataTesting.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yeartitleplot
11999Message in a Bottlewho meets by fate , shall be sealed by fate ....
41978Midnight Expressthe true story of billy hayes , an american c...
51996Primal Fearmartin vail left the chicago da ' s office to ...
61950Crisishusband and wife americans dr . eugene and mr...
71959The Tinglerthe coroner and scientist dr . warren chapin ...
\n", "
" ], "text/plain": [ " year title \\\n", "1 1999 Message in a Bottle \n", "4 1978 Midnight Express \n", "5 1996 Primal Fear \n", "6 1950 Crisis \n", "7 1959 The Tingler \n", "\n", " plot \n", "1 who meets by fate , shall be sealed by fate .... \n", "4 the true story of billy hayes , an american c... \n", "5 martin vail left the chicago da ' s office to ... \n", "6 husband and wife americans dr . eugene and mr... \n", "7 the coroner and scientist dr . warren chapin ... " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataTesting.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create count vectorizer with ngrams" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(7895, 1000)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vect = CountVectorizer(ngram_range=(1, 2), max_features=1000)\n", "X_dtm = vect.fit_transform(dataTraining['plot'])\n", "X_dtm.shape" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['able', 'able to', 'about', 'about the', 'about to', 'accident', 'across', 'act', 'action', 'actually', 'affair', 'after', 'after the', 'again', 'against', 'against the', 'age', 'agent', 'ago', 'alex', 'alive', 'all', 'all of', 'all the', 'alone', 'along', 'along the', 'along with', 'already', 'also', 'although', 'always', 'america', 'american', 'among', 'an', 'an old', 'and', 'and has', 'and he', 'and her', 'and his', 'and is', 'and she', 'and that', 'and the', 'and their', 'and then', 'and they', 'and when']\n" ] } ], "source": [ "print(vect.get_feature_names()[:50])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create y" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "dataTraining['genres'] = dataTraining['genres'].map(lambda x: eval(x))\n", "\n", "le = MultiLabelBinarizer()\n", "y_genres = le.fit_transform(dataTraining['genres'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Split train and test" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train_genres, y_test_genres = train_test_split(X_dtm, y_genres, test_size=0.33, random_state=42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Train multi-class multi-label model" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "clf = OneVsRestClassifier(RandomForestClassifier(n_jobs=-1, n_estimators=100, max_depth=10, random_state=42))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OneVsRestClassifier(estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n", " max_depth=10, max_features='auto', max_leaf_nodes=None,\n", " min_impurity_decrease=0.0, min_impurity_split=None,\n", " min_samples_leaf=1, min_samples_split=2,\n", " min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=-1,\n", " oob_score=False, random_state=42, verbose=0, warm_start=False),\n", " n_jobs=1)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clf.fit(X_train, y_train_genres)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "y_pred_genres = clf.predict_proba(X_test)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7437754044790014" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "roc_auc_score(y_test_genres, y_pred_genres, average='macro')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Apply models to kaggle test" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "X_test_dtm = vect.transform(dataTesting['plot'])\n", "\n", "cols = ['p_Action', 'p_Adventure', 'p_Animation', 'p_Biography', 'p_Comedy', 'p_Crime', 'p_Documentary', 'p_Drama', 'p_Family',\n", " 'p_Fantasy', 'p_Film-Noir', 'p_History', 'p_Horror', 'p_Music', 'p_Musical', 'p_Mystery', 'p_News', 'p_Romance',\n", " 'p_Sci-Fi', 'p_Short', 'p_Sport', 'p_Thriller', 'p_War', 'p_Western']\n", "\n", "y_pred_test_genres = clf.predict_proba(X_test_dtm)\n", "\n", "pd.DataFrame(y_pred_test_genres, index=dataTesting.index, columns=cols).to_csv('pred_genres_text_RF.csv', index_label='ID')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }