<jupyter_start><jupyter_text>Solution for Predicting Olympic Medal CountWe have a historical dataset on the modern Olympic Games, including all the Games from Athens 1896 to Rio 2012. We want to predict the number of medals for the 2016 Olympic Games. The dataset can be divided in three tables, the ground truth, country of olympics and the specific event with medals.\Here are the attributes of our tables.      Here we provide a solution for predicting Olympic Medal Count in 2016. Now you need to **follow the provided solution and select a subset of features** you think is helpful for predicting the Medal Count of the coming Olympic Games  in Tokyo. You are required to finish the tasks below: Steps: 1. learn about the dataset and the task of predicting Olympic Medal Count in the first cell called load data (input, ground truth, data, regressive model); 2. load the suggested features from experts and Automatic algorithms and check the features in the 3rd cell by clicking the button on the right of the cell. 3. Checked the information of the features in the table view and select a subset of features by ticking the checkbox, submit your selection by clicking the blue button Submit Selection in the view. 4. Copy the feature ids and evaluate these features in the evaluation cell by pasting the id list in the auto_human_feature_matrix.iloc[:,[feature id list]. You can select and evaluate the features iteratively or create new features from the three tables by yourself.  Load data<jupyter_code># load data and groung truth
import os
import pandas as pd
import utils_original as utils
import foldCode1 as load_df


DATA_DIR = os.path.join(os.getcwd(),"data/olympic_games_data")
es = utils.load_entityset(data_dir=DATA_DIR)
groundtruth_table, countries_table, events_table, cutoff_times_gt, dates, labels = load_df.loadData(es, DATA_DIR)<jupyter_output>/home/study816/suggest-features/naturalStudy_815/utils_original.py:152: FutureWarning: The default value of regex will change from True to False in a future version. In addition, single character regular expressions will *not* be treated as literal strings when regex=True.
  countries['Country'] = countries['Country'].str.replace('*', '')<jupyter_text>You can add new cells to print the 3 tables (groundtruth_table, countries_table, events_table) and the input (dates) and labels.  Generate Features (Edit)<jupyter_code>select_human_feature_names, select_human_features = load_df.human_features() #7
all_features = pd.read_csv("features/natural_features_all_815.csv")
# check feature matrix
# all_features
all_features
#check features
# MUST EDIT1: feature selection: input feature ID in the list below:
# feature_id = [0,1,34,96] #loc 0-98

# feature_id = [4,18,20,30,37,44,54,70,89,96,97,0,1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,19,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,38,39,40,41,42,43,45,46,47,48,49,50,51,52,53,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,90,91,92,93,94,95,98]
#          r2        mse Olympics Year
# 0  0.813924  61.717808    2016-08-05

# feature_id = [70]
# r2        mse Olympics Year
# 0  0.780269  72.880617    2016-08-05

# feature_id = [70, 89]
#  r2        mse Olympics Year
# 0  0.752792  81.994027    2016-08-05

# feature_id = [70, 89, 54]
# r2        mse Olympics Year
# 0  0.750798  82.655537    2016-08-05

# feature_id = [70, 0]
# r2        mse Olympics Year
# 0  0.821175  59.312608    2016-08-05

# feature_id = [70, 0, 58]
# r2        mse Olympics Year
# 0  0.819656  59.816678    2016-08-05

# feature_id = [70, 0, 14]
#  r2        mse Olympics Year
# 0  0.812218  62.283717    2016-08-05

# feature_id = [70, 0 ,18]
#  r2        mse Olympics Year
# 0  0.830903  56.086104    2016-08-05

feature_id = [70, 0 ,18, 15]
# r2       mse Olympics Year
# 0  0.844648  51.52713    2016-08-05

# feature_id = [70, 0 ,18, 15, 78]
#  r2        mse Olympics Year
# 0  0.843308  51.971846    2016-08-05


# feature_id = [70, 0 ,18, 15, 6,9,10,14,32,33,38,65,76,81,90]
# r2       mse Olympics Year
# 0  0.806419  64.20699    2016-08-05

# feature_id = [70, 0 ,18, 15, 10, 58]
# r2        mse Olympics Year
# 0  0.821115  59.332568    2016-08-05
# feature_id = [70, 0 ,18, 15, 58]
# r2       mse Olympics Year
# 0  0.839908  53.09938    2016-08-05
# feature_id = [70, 0 ,18, 15, 58, 78]
# r2        mse Olympics Year
# 0  0.83917  53.344246    2016-08-05

# feature_id = [70, 0 ,18, 15, 19]
# r2        mse Olympics Year
# 0  0.843927  51.766481    2016-08-05

# feature_id = [70, 0 ,18, 15, 19, 24]
# r2        mse Olympics Year
# 0  0.837741  53.818155    2016-08-05

# feature_id = [8,10,15,25,29,34,38,45,49,50,60,62,63,73,79,80,83,92,98]
#  r2        mse Olympics Year
# 0  0.729627  89.677538    2016-08-05

# feature_id = [8,10,15,25,29,34,38,45,49,50,60,62,63,73,79,80,83,92,98, 70, 0, 18]
#  r2        mse Olympics Year
# 0  0.810178  62.960304    2016-08-05

# OPTIONAL: write new features
# all_features.insert()

# OPTIONAL: you can check single feature:
# all_features.iloc[:, 0,1,2,3]<jupyter_output><empty_output><jupyter_text>Evaluate features with this given models<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import RobustScaler
import utils_original as utils

# parameters of the regressive model
pipeline_preprocessing = [("imputer",
                            SimpleImputer()),
                            ("scaler", RobustScaler(with_centering=True))]
splitter = utils.TimeSeriesSplitByDate(dates=dates, earliest_date=pd.Timestamp('01/01/2012')) # using data before and contain 2012 for training

# Optional: If you want to use all features
# all_X = all_features.values  

# MUST EDIT2: selection input, you can edit here to evalute feature subsets
all_X = all_features.iloc[:, feature_id].values

rf_regressor = RandomForestRegressor(
        n_estimators=200,
        random_state=50
    )
pipeline_reg = Pipeline(pipeline_preprocessing + [('rf_reg', rf_regressor)])
regression_score = utils.fit_and_score(all_X, labels, splitter, pipeline_reg)

print(regression_score)
# check the importance score of selected features
feature_imp = utils.get_feature_importances(pipeline_reg, 
#                                              all_features,
                                            # MUST EDIT3: 
                                            all_features.iloc[:, feature_id], # edit here to check the score of selected features
                                            labels, splitter, 100)
 
test_date = pd.Timestamp('08/05/2016')
feature_imp[test_date].reset_index(drop=True)
# feature_imp[test_date]['Feature']
for idx, feat in enumerate(feature_imp[test_date]['Feature']):
    print(idx, feat)<jupyter_output>0 SUM(medaling_athletes.NUM_UNIQUE(athletes.Athelete))
1 COUNT(medaling_athletes WHERE athlete.Gender = Men)
2 MEAN(countries_at_olympic_games.NUM_UNIQUE(medals_won.Event))
3 MEAN(countries_at_olympic_games.SUM(medals_won.Height))
4 MIN(countries_at_olympic_games.SKEW(medals_won.Age))
5 MAX(countries_at_olympic_games.MIN(medals_won.Age))
6 PERCENTILE(NUM_UNIQUE(medals_won.Medal))
7 MEAN(countries_at_olympic_games.SUM(medals_won.Weight))
8 MEAN(countries_at_olympic_games.COUNT(medals_won))
9 ABSOLUTE(MEAN(medals_won.Height))
10 PERCENTILE(SKEW(medals_won.Weight))
11 MIN(countries_at_olympic_games.SUM(medals_won.Height))
12 MEAN(countries_at_olympic_games.MEAN(medals_won.Height))
13 MIN(countries_at_olympic_games.COUNT(medals_won))
14 MAX(countries_at_olympic_games.MEAN(medals_won.Weight))
15 MEAN(countries_at_olympic_games.MIN(medals_won.Height))
16 TREND(countries_at_olympic_games.SKEW(medals_won.Weight), Year)
17 PERCENTILE(TREND(medals_won.Weight, Year))
18 TREND(countries_at_olympic[...]<jupyter_text>Optional: Try multiple regressive models<jupyter_code>import foldCode2 as model

# other regressive models
linear_reg, logistic_reg, sgd_reg, knn_reg, mlp_reg, decisionTree_reg, extTree_reg, gb_reg, svc_reg, svr_reg, ard_reg = model.multipleReg_model()


pipeline_reg1 = Pipeline(pipeline_preprocessing + [('linear_reg', linear_reg)])
regression_score1 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg1)
print("linear_reg: ")
print(regression_score1)

pipeline_reg2 = Pipeline(pipeline_preprocessing + [('logistic_reg', logistic_reg)])
regression_score2 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg2)
print("logistic_reg: ")
print(regression_score2)

pipeline_reg3 = Pipeline(pipeline_preprocessing + [('sgd_reg', sgd_reg)])
regression_score3 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg3)
print("sgd_reg: ") 
print(regression_score3)

pipeline_reg4 = Pipeline(pipeline_preprocessing + [('knn_reg', knn_reg)])
regression_score4 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg4)
print("knn_reg: ")
print(regression_score4)

pipeline_reg5 = Pipeline(pipeline_preprocessing + [('mlp_reg', mlp_reg)])
regression_score5 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg5)
print("mlp_reg: ")
print(regression_score5)

pipeline_reg6 = Pipeline(pipeline_preprocessing + [('decisionTree_reg', decisionTree_reg)])
regression_score6 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg6)
print("decisionTree_reg: ")
print(regression_score6)

pipeline_reg7 = Pipeline(pipeline_preprocessing + [('extTree_reg', extTree_reg)])
regression_score7 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg7)
print("extTree_reg: ")
print(regression_score7)

pipeline_reg8 = Pipeline(pipeline_preprocessing + [('gb_reg', gb_reg)])
regression_score8 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg8)
print("gb_reg: ")
print(regression_score8)

pipeline_reg9 = Pipeline(pipeline_preprocessing + [('svc_reg', svc_reg)])
regression_score9 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg9)
print("svc_reg: ")
print(regression_score9)

pipeline_reg10 = Pipeline(pipeline_preprocessing + [('svr_reg', svr_reg)])
regression_score10 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg10)
print("svr_reg: ")
print(regression_score10)

pipeline_reg11 = Pipeline(pipeline_preprocessing + [('ard_reg', ard_reg)])
regression_score11 = utils.fit_and_score(all_X, labels, splitter, pipeline_reg11)
print("ard_reg: ")
print(regression_score11)
# check the importance score of selected features
feature_imp = utils.get_feature_importances(pipeline_reg8, # EDIT: model name
                                             all_features,
#                                             all_features.iloc[:, feature_id], # edit here to check the score of selected features
                                            labels, splitter, 300)
 
test_date = pd.Timestamp('08/05/2016')
feature_imp[test_date].reset_index(drop=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10327687776141384, 'fraction_numerical': 0.09296759941089838, 'mean_word_length': 4.566086065573771, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '561363', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4375, 'n_tokens_neox': 3811, 'n_words': 935, 'path': 'naturalStudy_815/nb/2-Industry_816_peng.ipynb', 'repo_name': 'CinderD/suggest-features', 'size': '10864'}