{"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 all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.tree import DecisionTreeRegressor\nncaa = pd.read_csv(\"../input/ncaaluis/ncaap.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#ncaa.describe()\nncaa.isnull().sum()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ncaa.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ncaa['ScoreRate'] = ncaa['WScore']/ncaa['LScore']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ncaa.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ncaa.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Declaro variables seleccionadas y = ScoreRate\n# Se eliminará Wscore y LScrore\n# Extraigo todo lo mayor a 2015 y se lo doy a X_test\n\nX_test = ncaa.drop(ncaa[ncaa.Season < 2015].index)\nncaa = ncaa.drop(ncaa[ncaa.Season >= 2015].index)\ny = ncaa.ScoreRate\nncaa_features = ['Season', 'DayNum', 'WTeamID', 'wcoach', 'WConfCode',\n       'LTeamID', 'LCoach', 'LConfCode', 'WLoc']\nX = ncaa[ncaa_features]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Se fracciona la muestras entre entrenamiento y validación\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X, y, random_state = 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def score_dataset(X_train, X_val, y_train, y_val):\n    model = RandomForestRegressor(n_estimators=100, random_state=0)\n    model.fit(X_train, y_train)\n    preds = model.predict(X_val)\n    return mean_absolute_error(y_val, preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Solo para saber cuantas cacillas vacias habia\n#X.isnull().sum()\n#X_val.loc[:, X_val.isnull().any()]\n#nan_col = [i for i in X_val.columns if X_val[i].isnull().any()]\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#opcion con Label Encoder\n\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import LabelEncoder\ns = (X_train.dtypes == 'object')\nobject_cols = list(s[s].index)\nlabel_X_train = X_train.copy()\nlabel_X_valid = X_val.copy()\n# Apply label encoder to each column with categorical data\nlabel_encoder = LabelEncoder()\nfor col in object_cols:\n    label_X_train[col] = label_encoder.fit_transform(X_train[col])\n    label_X_valid[col] = label_encoder.transform(X_val[col])\n\nprint(\"MAE from Approach 2 (Label Encoding):\") \n#Abajo entre comillas por tener aun valores con NA\n#print(score_dataset(label_X_train, label_X_valid, y_train, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_X_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Usand table compacta, no requiere usar imputer dado que todo los datos están acá\nfrom sklearn.impute import SimpleImputer\n\n# Imputation\nmy_imputer = SimpleImputer()\nimputed_X_train = pd.DataFrame(my_imputer.fit_transform(label_X_train))\nimputed_X_valid = pd.DataFrame(my_imputer.transform(label_X_valid))\n\n# Imputation removed column names; put them back\nimputed_X_train.columns = label_X_train.columns\nimputed_X_valid.columns = label_X_valid.columns\n\nprint(\"MAE from Approach 2 (Imputation):\")\nprint(score_dataset(imputed_X_train, imputed_X_valid, y_train, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#from sklearn.model_selection import train_test_split\n\n#ncaa_model = RandomForestRegressor(random_state=1)\n#ncaa_model.fit(train_X, train_y)\n#ncaa_preds = ncaa_model.predict(val_X)\n#print(mean_absolute_error(val_y, ncaa_preds))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Prueba con Imputer Extended, Agrega columna donde hubo cambio. El resutado fue tirando a mejor, pero sólo 0.00002\n\n\n# Get names of columns with missing values\ncols_with_missing = [col for col in X_train.columns\n                     if X_train[col].isnull().any()]\n\n# Prueba con Imputer Extended, Agrega columna donde hubo cambio\n\nX_train_plus = label_X_train.copy()\nX_valid_plus = label_X_valid.copy()\n\n# Make new columns indicating what will be imputed\nfor col in cols_with_missing:\n    X_train_plus[col + '_was_missing'] = X_train_plus[col].isnull()\n    X_valid_plus[col + '_was_missing'] = X_valid_plus[col].isnull()\n\n# Imputation\nmy_imputer = SimpleImputer()\nimputed_X_train_plus = pd.DataFrame(my_imputer.fit_transform(X_train_plus))\nimputed_X_valid_plus = pd.DataFrame(my_imputer.transform(X_valid_plus))\n\n# Imputation removed column names; put them back\nimputed_X_train_plus.columns = X_train_plus.columns\nimputed_X_valid_plus.columns = X_valid_plus.columns\nprint(\"MAE con Imputer extended usando Label:\")\nprint(score_dataset(imputed_X_train_plus, imputed_X_valid_plus, y_train, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"#Funcionó pero sólo con OH, Es mejor usar Label por eso es mejor no correr esta fase.\n\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import mean_absolute_error\n\nnumerical_transformer = SimpleImputer(strategy='mean')\n\ncategorical_cols = [cname for cname in X_train.columns if\n                    X_train[cname].nunique() < 10 and \n                    X_train[cname].dtype == \"object\"]\n\n# Select numerical columns\nnumerical_cols = [cname for cname in X_train.columns if \n                X_train[cname].dtype in ['int64', 'float64']]\n\n# Preprocessing for categorical data\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\n# Bundle preprocessing for numerical and categorical data\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ])\n\n\nmodel = RandomForestRegressor(n_estimators=300, random_state=0)\n\nmy_pipeline = Pipeline(steps=[('preprocessor', preprocessor),\n                              ('model', model)\n                             ])\n\n# Preprocessing of training data, fit model \nmy_pipeline.fit(X_train, y_train)\n\n# Preprocessing of validation data, get predictions\npreds = my_pipeline.predict(X_val)\n\n# Evaluate the model\nscore = mean_absolute_error(y_val, preds)\nprint('MAE:', score)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from xgboost import XGBRegressor\nX_reg_train = label_X_train\nX_reg_valid = label_X_valid\n\nmy_model = XGBRegressor(n_estimators=1700, learning_rate=0.13, n_jobs=4)\nmy_model.fit(X_reg_train, y_train, \n             early_stopping_rounds=6, \n             eval_set=[(X_reg_valid, y_val)], \n             verbose=False)\n\npredictions = my_model.predict(X_reg_valid)\nprint(\"Mean Absolute Error: \" + str(mean_absolute_error(predictions, y_val)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test.describe()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chop_X_test = X_test.drop(['WScore','LScore','ScoreRate'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chop_X_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Label Encoder para los valores de test\n\ns = (X_test.dtypes == 'object')\nobject_cols = list(s[s].index)\nlabel_X_test = chop_X_test.copy()\n\n# Apply label encoder to each column with categorical data\nlabel_encoder_test = LabelEncoder()\nfor col in object_cols:\n    label_X_test[col] = label_encoder_test.fit_transform(X_test[col])\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_X_test.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds_test = my_model.predict(label_X_test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output = pd.DataFrame({'Season': label_X_test.Season, 'WID': label_X_test.WTeamID, 'LID': label_X_test.LTeamID,\n                       'Pred': preds_test})\n#output.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"output.iloc[:50, :50]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output2 = output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output2 = pd.DataFrame (columns = ['ID','Pred'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mayor = output.where(output['WID'] > output['LID'])\nmenor = output.where(output['LID'] > output['WID'])\nmayor = mayor.dropna()\nmenor = menor.dropna()\nmayor['Season'] = mayor['Season'].astype(np.int64)\nmayor['WID'] = mayor['WID'].astype(np.int64)\nmayor['LID'] = mayor['LID'].astype(np.int64)\nmenor['Season'] = menor['Season'].astype(np.int64)\nmenor['WID'] = menor['WID'].astype(np.int64)\nmenor['LID'] = menor['LID'].astype(np.int64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output2 = mayor.assign(ID = mayor.Season.astype(str) + \"_\" + mayor.LID.astype(str) + \"_\" + mayor.WID.astype(str))\noutput3 = menor.assign(ID = menor.Season.astype(str) + \"_\" + menor.WID.astype(str) + \"_\" + menor.LID.astype(str))\nframes = [output2, output3]\noutput4 = pd.concat(frames)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''  if output4['WID'] > output4['LID']:\n    output4 = output4.assign(Pred2 = output4.Pred * 50)\nelse:\n    output4 = output4.assign(Pred2 = (1/output4.Pred) * 50)\n    ''' \n    \noutput4.loc[output4['WID'] > output4['LID'], 'Pred2'] = output4['Pred'] * 50\noutput4.loc[output4['WID'] < output4['LID'], 'Pred2'] = 1/output4['Pred'] * 50\noutputf = output4.drop(['Season', 'WID', 'LID', 'Pred'], axis=1)\noutputf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"outputf = outputf.rename(columns={\"Pred2\": \"Pred\"})\noutputf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"outputf.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}