<jupyter_start><jupyter_text>Stage1<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB

level_1_classifier = {'logistic_classifier'             : LogisticRegression(),
                      'svm_classifier'                  : SVC(),
                      'naive_bayse_gaussian_classifier' : GaussianNB()}

level_1_out_X, model_dict = StackingMethod(X, Y, 10, 0, **level_1_classifier)
#level_1_out_X = StackingMethod(X, Y, 10, 0, **level_1_classifier)
level_1_out_X
level_1_out_X['Label'] = Y['Label']
level_1_out_X
level_1_out_X = level_1_out_X.drop(['Label'], axis=1)
level_1_out_X<jupyter_output><empty_output><jupyter_text>Stage2<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from xgboost import XGBClassifier

level_2_classifier = {'rf_classifer'    : RandomForestClassifier(),
                      'gbdt_classifier' : GradientBoostingClassifier(),
                      'xgb_classifier'  : XGBClassifier()}

level_2_out_X, model_dict = StackingMethod(level_1_out_X, Y, 10, 0, **level_2_classifier)
level_2_out_X
level_2_out_X['Label'] = Y['Label']
level_2_out_X
level_2_out_X = level_2_out_X.drop(['Label'], axis=1)
level_2_out_X<jupyter_output><empty_output><jupyter_text>Stage3<jupyter_code>from lightgbm import LGBMClassifier 

# Set the parameters by cross-validation
tuned_parameters = []

# Number of random trials
NUM_TRIALS = 1

clf = LGBMClassifier()

(max_score, best_estimator) = CrossValidationGridSearchNested(level_2_out_X, Y, NUM_TRIALS, 10, clf, tuned_parameters, 'roc_auc')
best_parameter = best_estimator.get_params()

print(f'\nmax_score = {max_score}\n')
print(f'\nbest_estimator = {best_estimator}\n')
print(f'\nbest_parameter = {best_parameter}\n')
level_2_out_X.head()
(n_estimator, n_scoring) = CrossValidationGetModelsScores(level_2_out_X, Y, 10, best_estimator)
n_scoring
n_estimator
Y.head(15)
level_2_out_X.head(10)
x_1d_test_array = np.array([list(level_2_out_X.iloc[4]), list(level_2_out_X.iloc[5]), list(level_2_out_X.iloc[6])])
original_df_X = pd.DataFrame(data = x_1d_test_array, columns=level_2_out_X.columns)
original_df_X
x_1d_test_array2 = np.array([list(level_2_out_X.iloc[7]), list(level_2_out_X.iloc[8]), list(level_2_out_X.iloc[9])])
original_df_X2 = pd.DataFrame(data = x_1d_test_array2, columns=level_2_out_X.columns)
original_df_X2
a = {(str(x)+'shit'):2 for x in ['123', 456]}
a
Y.values
aa = [[x] for x in range(1, 4, 1)]
aa = np.array(aa)
aa.ravel()
y_pred = np.array([original_df_X2['Label_logistic_classifier']])
y_pred
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score

y_pred = np.array(original_df_X2['Label_logistic_classifier'])
y_pred[0] = 0
y_pred
y_true = np.array(Y['Label'][7:10])
#print(f'y_pred = \n{y_pred}')
#print(f'y_true = \n{y_true}')
accuracy_score(y_true, y_pred)
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score

y_pred = original_df_X2['Label_logistic_classifier']
y_pred[0] = 1
y_true = Y['Label'][7:10]
print(f'y_pred = \n{y_pred}')
print(f'y_true = \n{y_true}')
accuracy_score(y_true, y_pred)
roc_auc_score(y_true, y_pred)
original_df_X.values
original_df_X2.values
total_list = []
a = [[x] for x in range(10, 31, 10)]
b = [[x] for x in range(40, 61, 10)]

total_list.append(a)
total_list.append(b)
X2_featuer = original_df_X2.values
for list_test in total_list:
    #a_arr = np.array(list_test)
    a_arr = list_test
#    print(a_arr.shape)
    X2_featuer = np.concatenate((X2_featuer, a_arr), axis=1)
X2_featuer
np.concatenate((original_df_X.values, original_df_X2.values), axis=0)
#a = [PredictFunctionAggregation([x], n_estimator, voting=1, is_debug=0) for x in original_df_X.values]
total = []
a = original_df_X.values[0].tolist()
b = original_df_X.values[1].tolist()
total.append(a)
total.append(b)
total = np.array(total)
total
x_1d_test_array = np.array([list(level_2_out_X.iloc[4]), list(level_2_out_X.iloc[5]), list(level_2_out_X.iloc[6])])
cc = [x for x in x_1d_test_array]
cc[0].shape
ccc = np.array([cc[0]])
ccc.shape
x_1d_test_array = np.array([list(level_2_out_X.iloc[4]), list(level_2_out_X.iloc[5]), list(level_2_out_X.iloc[6])])

a = [PredictFunctionAggregation(np.array([x]), n_estimator, voting=1, is_debug=0) for x in x_1d_test_array]
a
x_1d_test_array = np.array([list(level_2_out_X.iloc[4])])
final_class = PredictFunctionAggregation(x_1d_test_array, n_estimator, voting=1, is_debug=1)
final_class_1_prob = PredictFunctionAggregation(x_1d_test_array, n_estimator, voting=0, is_debug=1)
final_class
final_class_1_prob
1-final_class_1_prob<jupyter_output><empty_output><jupyter_text>Feature Importance<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

feature_name = list(level_2_out_X.columns)
best_estimator.fit(level_2_out_X.values, Y.values.ravel())
importances = best_estimator.feature_importances_
indices = np.argsort(importances)[::-1]
std = np.array([0.5 for x in importances])
#std = np.std([tree.feature_importances_ for tree in best_estimator.], axis=0)

# Print the feature ranking
print("Feature ranking:")

for f in range(level_2_out_X.shape[1]):
    print("%d. feature %s (%f)" % (f + 1, feature_name[indices[f]], importances[indices[f]]))

# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(level_2_out_X.shape[1]), importances[indices],
       color="r", yerr=std[indices], align="center")
plt.xticks(range(level_2_out_X.shape[1]), indices)
plt.xlim([-1, level_2_out_X.shape[1]])
plt.show()<jupyter_output>Feature ranking:
1. feature FG%_A (217.000000)
2. feature Label_logistic_classifier (100.000000)
3. feature 3P%_A (33.000000)
4. feature Home/Away_A (22.000000)
5. feature FT%_A (9.000000)
6. feature FG%_B (8.000000)
7. feature FGM_A (4.000000)
8. feature FGA_A (4.000000)
9. feature 3P%_B (3.000000)
10. feature 3PA_A (3.000000)
11. feature AST_A (2.000000)
12. feature FT%_B (2.000000)
13. feature TOV_A (1.000000)
14. feature BLK_A (1.000000)
15. feature OREB_A (1.000000)
16. feature DREB_A (1.000000)
17. feature STL_A (0.000000)
18. feature FTA_A (0.000000)
19. feature REB_A (0.000000)
20. feature FTM_A (0.000000)
21. feature 3PM_A (0.000000)
22. feature PF_A (0.000000)
23. feature Label_xgb_classifier (0.000000)
24. feature FGM_B (0.000000)
25. feature Label_gbdt_classifier (0.000000)
26. feature Label_rf_classifer (0.000000)
27. feature Label_naive_bayse_gaussian_classifier (0.000000)
28. feature Label_svm_classifier (0.000000)
29. feature PF_B (0.000000)
30. feature TOV_B (0.000000)[...]
StarcoderdataJupyterStructuredCleanDedup