<jupyter_start><jupyter_text>Cross-Validation SVM<jupyter_code>from sklearn.svm import SVC
from sklearn.model_selection import RandomizedSearchCV, KFold
from sklearn.metrics import log_loss, make_scorer

lags = 100
for s in test.station.unique():
    data = wide_series[list(wide_series.reset_index().day < '2015-01-01')]
    to_lag = data[[c for c in data.columns if not c in ['{}_{}'.format(s, agg) for agg in agg_types]]]
    features = create_lagged_features(to_lag, lags)\
        .join(extra_features[extra_features.station == s].set_index('date'))\
        .join(rolling_mean_features[rolling_mean_features.station == s]
              .set_index('date').drop(['station', 'max_conc_obs'], axis=1))\
        .join(obs_and_mods[obs_and_mods.station == s][['Concentration', 'day']].groupby('day').max())\
        .fillna(method='ffill').fillna(0)
    X = features[[c for c in features.columns if not c in [
        'time', 'datetime', 'Concentration', 'target', 'station'
    ]]].values[:, np.where(X[0] != 98)[0]]
    y = (features['Concentration'].fillna(method='ffill').values > 100).astype(int)

    params = pd.DataFrame({
        'C': np.random.choice(np.linspace(0.01, .99, 100), 15),
        'score': [np.nan] * 15
    })
    for i, r in params[['C']].iterrows():
        kf = KFold(n_splits=3)
        metric = []
        for train_index, test_index in kf.split(X):
            X_train, X_test = X[train_index], X[test_index]
            y_train, y_test = y[train_index], y[test_index]
            p = dict(r)
            p['probability'] = True
            try:
                model = SVC(**p)
                model.fit(X_train, y_train)
                y_pred = model.predict_proba(X_test)
                metric.append(
                    log_loss(y_test, y_pred, labels=(0, 1))
                ) 
            except ValueError as e: 
                print(e)
                metric.append(np.nan)
                pass

        params.loc[i, 'score'] = np.mean(metric)
        print(params.iloc[i])

    params.to_csv('../reports/cv_ssvm_Jonas_8_{}_{}.csv'
                  .format(s, len(glob.glob('../reports/cv_ssvm_Jonas_8_{}_*'.format(s)))))
    params.head()<jupyter_output>C       0.09
score   0.28
Name: 0, dtype: float64
C       0.87
score   0.28
Name: 1, dtype: float64
C       0.76
score   0.28
Name: 2, dtype: float64
C       0.37
score   0.28
Name: 3, dtype: float64
C       0.46
score   0.28
Name: 4, dtype: float64
C       0.89
score   0.28
Name: 5, dtype: float64
C       0.56
score   0.28
Name: 6, dtype: float64
C       0.81
score   0.28
Name: 7, dtype: float64
C       0.02
score   0.28
Name: 8, dtype: float64
C       0.03
score   0.28
Name: 9, dtype: float64
C       0.45
score   0.28
Name: 10, dtype: float64
C       0.49
score   0.28
Name: 11, dtype: float64
C       0.37
score   0.28
Name: 12, dtype: float64
C       0.56
score   0.28
Name: 13, dtype: float64
C       0.86
score   0.28
Name: 14, dtype: float64
C       0.68
score   0.31
Name: 0, dtype: float64
C       0.76
score   0.31
Name: 1, dtype: float64
C       0.11
score   0.31
Name: 2, dtype: float64
C       0.18
score   0.31
Name: 3, dtype: float64
C       0.50
score   0.31
Name: 4, dtype: flo[...]<jupyter_text>SVM Classification per Station* Targeting `target`* Using imputed for 2015<jupyter_code>tall_series_mod = obs_and_mods.fillna(0).groupby(['day', 'station']).agg({
    'pred_0_days': agg_types
})['pred_0_days'].reset_index().rename(columns={
    'pred_0_days': 'Concentration'
})
aggs = [tall_series_mod.pivot(index='day', columns='station', values=agg) for agg in agg_types]
aggs = [df.rename(columns={c: c + '_' + agg for c in df.columns}) for df, agg in zip(aggs, agg_types)]
wide_series_mod = pd.concat(aggs, axis=1)
wide_series_mod.tail()
to_impute = wide_series.loc[test['date'].unique()]
for s in obs_and_mods.station.unique():
    columns = [s + '_' + agg for agg in agg_types]
    to_impute[columns] = wide_series_mod.loc[to_impute.reset_index().day, columns]
    
originals = wide_series[list(~wide_series.reset_index().day.isin(test['date'].unique()))]
wide_series_imputed = pd.concat([to_impute, originals]).sort_index()
wide_series_imputed.head()
all_frames = []
for s in test.station.unique():
    frames = [pd.read_csv(f, index_col=0) for f in glob.glob('../reports/cv_ssvm_Jonas_8*{}*'.format(s))]
    try:
        frame = pd.concat(frames)
        frame['station'] = s
        all_frames.append(frame)
    except ValueError:
        pass
    
cv_results = pd.concat(all_frames)
cv_results = cv_results[cv_results['score'] > 0]
cv_results.sort_values(['score', 'station']).drop_duplicates('station')
test_params = []
for i, r in cv_results.sort_values(['score', 'station']).drop_duplicates('station').iterrows():
    p = dict(r)
    del p['score']
    del p['station']
    test_params.append((r['station'], p))
    
test_params
# from sklearn.svm import SVC
# from sklearn.model_selection import  KFold
# from sklearn.metrics import log_loss

# lags = 100
# preds_by_station = []
# for s, p in test_params:
#     data = pd.concat([
#         wide_series[list(wide_series.reset_index().day < '2015-01-01')],
#         wide_series_imputed[list(wide_series_imputed.reset_index().day >= '2015-01-01')]
#     ])
#     to_lag = data[[c for c in data.columns if not c in ['{}_{}'.format(s, agg) for agg in agg_types]]]
#     features = create_lagged_features(to_lag, lags)\
#         .join(extra_features[extra_features.station == s].set_index('date'))\
#         .join(rolling_mean_features[rolling_mean_features.station == s]
#               .set_index('date').drop(['station', 'conc_obs', 'weekend', 'week_day', 'month'], axis=1))\
#         .join(obs_and_mods[obs_and_mods.station == s][['Concentration', 'day']].groupby('day').max())\
#         .fillna(method='ffill').fillna(0)
#     X = features[[c for c in features.columns if not c in [
#         'time', 'datetime', 'Concentration', 'target', 'station'
#     ]]].values
#     y = (features['Concentration'].values > 100).astype(int)
#     y_pred_all = copy.deepcopy(y).astype(float)
#     metrics_all = []
    
#     kf = KFold(n_splits=2)
#     for train_index, test_index in kf.split(X):
#         X_train, X_test = X[train_index], X[test_index]
#         y_train, y_test = y[train_index], y[test_index]
#         p['probability'] = True
#         model = SVC(**p)
#         model.fit(X_train, y_train)
#         y_pred = model.predict_proba(X_test)[:, 1]
#         y_pred_all[test_index] = y_pred
#         metrics_all.append(log_loss(y_test, y_pred, labels=(0, 1)))
        
#     preds_by_station.append((s, y_pred_all))

predictions = pd.DataFrame(np.column_stack([e[1] for e in preds_by_station]), columns=test.station.unique())
predictions['date'] = features.drop('day', axis=1).reset_index()['day']
predictions.to_csv('../reports/pred_ssvm_Jonas_8_{}.csv'.format(len(glob.glob('../reports/pred_ssvm_Jonas_8_*'))))
print('CV Metric: {}'.format(np.mean(metrics_all)))
predictions.tail()<jupyter_output>CV Metric: 0.6679334765440583
{'fraction_non_alphanumeric': 0.1569130001423893, 'fraction_numerical': 0.04001139114338602, 'mean_word_length': 3.1004086398131934, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '387802', 'license': 'MIT', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2552, 'n_words': 534, 'path': 'notebooks/Jonas_8.ipynb', 'repo_name': 'javiermas/BCNAirQualityDatathon', 'size': '7023'}