<jupyter_start><jupyter_text>Sprawdzenie braków danych<jupyter_code>dataframe.head()<jupyter_output><empty_output><jupyter_text>age,integer,Age of client: numerical valuejob,string,"Type of job: admin., blue-collar, entrepreneur, housemaid, management, retired, self-employed, services, student, technician, unemployed, unknown"marital,string,"Marital status: divorced, married, single, unknown"education,string,"Level of education: primary, secondary, tertiary, unknown"default,string,"Has credit in default: no, yes, unknown"balance,integer,Average yearly balance in Euro: numerical valuehousing,string,"Has housing loan: no, yes, unknown"loan,string,"Has personal loan: no, yes, unknown"contact,string,"Communication type: unknown, telephone, cellular"day,integer,Day of the month: numerical value between 1 and 31month,string,"Month of the year: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"duration,float,Last contact duration: numerical value in secondscampaign,integer,Number of contacts made: numerical valuepdays,float,"Number of days passed since client was last contacted from a previous campaign: numerical value, -1 indicates client was not previously contacted"previous,integer,Number of contacts performed before this campaign and for this client: numerical valuepoutcome,string,"Outcome of previous marketing campaign: unknown, other, failure, success"y,string,"Predictor class: yes, no"<jupyter_code>dataframe.isna().any()
dataframe.isin(['unknown']).any()<jupyter_output><empty_output><jupyter_text>Zmiana daty na reprezentacje kątem   ![image.png](attachment:image.png)<jupyter_code>date = list(dataframe.day)
date = [str(item) for item in date]
date = [date[i] + '/' + list(dataframe.month)[i] for i in range(len(dataframe.day))] 

date = [int(datetime.strptime(item, '%d/%b').strftime('%j')) for item in date]
date_sin = [math.sin(date[i]/360) for i in range (len(date))]
date_cos = [math.cos(date[i]/360) for i in range (len(date))]
dataframe['date_sin'] = date_sin
dataframe['date_cos'] = date_cos

to_drop = ['month', 'day']
dataframe = dataframe.drop(to_drop, axis=1)

dataframe.head()<jupyter_output><empty_output><jupyter_text>Kodowanie zmiennych kategorycznych<jupyter_code>dataframe[["marital","education","default","housing","loan","contact","poutcome", "y"]] = dataframe[["marital","education","default","housing","loan","contact","poutcome", "y"]].apply(LabelEncoder().fit_transform)
a = ce.BaseNEncoder(base=2).fit_transform(dataframe["job"])
dataframe = pd.concat([dataframe, a], axis=1).drop(["job"], axis=1)
dataframe.head()<jupyter_output><empty_output><jupyter_text>Outliers<jupyter_code>dataframe_orgin = pd.read_csv('bank_marketing_weka_dataset.csv')
upper_lim_balance = dataframe_orgin['balance'].mean() + dataframe_orgin['balance'].std() * 3
lower_lim_balance = dataframe_orgin['balance'].mean() - dataframe_orgin['balance'].std() * 3

dataframe.loc[(dataframe['balance'] > upper_lim_balance),'balance'] = upper_lim_balance
dataframe.loc[(dataframe['balance'] < lower_lim_balance),'balance'] = lower_lim_balance

upper_lim_pdays = dataframe_orgin.loc[dataframe_orgin.pdays > -1, 'pdays'].mean() + dataframe_orgin.loc[dataframe_orgin.pdays > -1, 'pdays'].std() * 3

dataframe.loc[(dataframe['pdays'] > upper_lim_pdays),'pdays'] = upper_lim_pdays

upper_lim_duration = dataframe_orgin['duration'].mean() + dataframe_orgin['duration'].std() * 3

dataframe.loc[(dataframe['duration'] > upper_lim_pdays),'duration'] = upper_lim_duration
ig, axs = plt.subplots(1,2)

axs[0].boxplot(dataframe.balance)
axs[1].boxplot(dataframe_orgin.balance)

plt.show()
fig, axs = plt.subplots(1,2)

axs[0].boxplot(dataframe.pdays)
axs[1].boxplot(dataframe_orgin.pdays)

plt.show()
fig, axs = plt.subplots(1,2)

axs[0].boxplot(dataframe.duration)
axs[1].boxplot(dataframe_orgin.duration)

plt.show()
dataframe
dataframe.columns
dataframe.var()
y = dataframe['y']
X = dataframe.drop('y', axis = 1)
print(y.sum(), y.shape)<jupyter_output>521 (4521,)<jupyter_text>KM3  <NAME><jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
                                         X, y, test_size=0.33, random_state=42, stratify = y)
scores = pd.DataFrame({'score':[],'f1':[],'roc':[], 'recall':[]})
from sklearn.metrics import f1_score
from sklearn.feature_selection import RFE
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn import datasets, metrics, model_selection, svm
from sklearn.metrics import recall_score

def importance(estimator, n):
    selector = RFE(estimator, n_features_to_select=n, step=1)
    selector = selector.fit(X_train, y_train)
    selector.score(X_test, y_test)
    istot = [x for _,x in sorted(zip(selector.ranking_,X.columns))]
    return istot
importance(random.best_estimator_,3)<jupyter_output><empty_output><jupyter_text>DTC<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import RFE

tree_model = DecisionTreeClassifier()
tree_model
max_depth=[3, 5, 6]
criterion=["gini","entropy"]
min_samples_split=[5,10,15]
param_grid = dict(max_depth=max_depth,criterion=criterion, min_samples_split=min_samples_split)

random = RandomizedSearchCV(estimator=tree_model, param_distributions=param_grid, cv = 3, n_jobs=-1, scoring='f1')

random_result = random.fit(X_train, y_train)
# Summarize results
print("Best: %f using %s" % (random_result.best_score_, random_result.best_params_))

random.fit(X_train, y_train)

score = accuracy_score(y_test, random.predict(X_test))
f1 = f1_score(y_test, random.predict(X_test))
roc = roc_auc_score(y_test,random.predict(X_test))
recall = recall_score(y_test,random.predict(X_test))

print('Score:', score)
print('F1:', f1)
print('ROC:', roc)
print('Recall:', recall)

row_df = pd.DataFrame({'score':score, 'f1':f1, "roc":roc, "recall":recall}, index = ["DTC"])
scores = pd.concat([row_df, scores])
cm = confusion_matrix(y_test, random.predict(X_test))

ax= plt.subplot()
sns.heatmap(cm, annot=True, ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['no', 'yes']); ax.yaxis.set_ticklabels(['no', 'yes']);
metrics.plot_roc_curve(random, X_test, y_test)
plt.show()
plt.figure(figsize=(12,10))
model = random.best_estimator_
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()<jupyter_output>[0.08258421 0.00830744 0.         0.         0.06056386 0.
 0.0019881  0.04244205 0.38527368 0.00541674 0.19969897 0.0065777
 0.10306954 0.04302787 0.03821196 0.         0.         0.00930433
 0.01353357 0.        ]<jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier()
rfc
max_depth=[3, 5, 6]
criterion=["gini","entropy"]
min_samples_split=[5,10,15]
param_grid = dict(max_depth=max_depth,criterion=criterion, min_samples_split=min_samples_split)

random = RandomizedSearchCV(estimator=tree_model, param_distributions=param_grid, cv = 3, n_jobs=-1, scoring='f1')

random_result = random.fit(X_train, y_train)
# Summarize results
print("Best: %f using %s" % (random_result.best_score_, random_result.best_params_))

random.fit(X_train, y_train)

score = accuracy_score(y_test, random.predict(X_test))
f1 = f1_score(y_test, random.predict(X_test))
roc = roc_auc_score(y_test,random.predict(X_test))
recall = recall_score(y_test,random.predict(X_test))

print('Score:', score)
print('F1:', f1)
print('ROC:', roc)
print('Recall:', recall)

row_df = pd.DataFrame({'score':score, 'f1':f1, "roc":roc, "recall":recall}, index = ["RFC"])
scores = pd.concat([row_df, scores])
cm = confusion_matrix(y_test, random.predict(X_test))

ax= plt.subplot()
sns.heatmap(cm, annot=True, ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['no', 'yes']); ax.yaxis.set_ticklabels(['no', 'yes']);
metrics.plot_roc_curve(random, X_test, y_test)
plt.show()
plt.figure(figsize=(12,10))
model = random.best_estimator_
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()<jupyter_output>[0.08258421 0.00830744 0.         0.00620289 0.05558032 0.
 0.0019881  0.04244205 0.38527368 0.00774282 0.19969897 0.0065777
 0.10306954 0.03555768 0.05144104 0.         0.         0.
 0.01353357 0.        ]<jupyter_text>Regresja logistyczna<jupyter_code>from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()
lr
C = [0.001,0.01,0.1,1,10,100,1000]
penalty = ['l1', 'l2', 'none']
dual = [True, False]
tol = [1e-4, 1e-5, 1e-6, 1e-3, 1e-2]
solver = ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']

random_grid = {'C': C,
               'penalty': penalty,
               'dual': dual,
               'tol': tol,
               'solver': solver}




random = RandomizedSearchCV(estimator=lr, param_distributions=random_grid, cv = 3, n_jobs=-1, scoring='f1')

random_result = random.fit(X_train, y_train)
# Summarize results
print("Best: %f using %s" % (random_result.best_score_, random_result.best_params_))

random.fit(X_train, y_train)

score = accuracy_score(y_test, random.predict(X_test))
f1 = f1_score(y_test, random.predict(X_test))
roc = roc_auc_score(y_test,random.predict(X_test))
recall = recall_score(y_test,random.predict(X_test))

print('Score:', score)
print('F1:', f1)
print('ROC:', roc)
print('Recall:', recall)

row_df = pd.DataFrame({'score':score, 'f1':f1, "roc":roc, "recall":recall}, index = ["LR"])
scores = pd.concat([row_df, scores])
cm = confusion_matrix(y_test, random.predict(X_test))

ax= plt.subplot()
sns.heatmap(cm, annot=True, ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['no', 'yes']); ax.yaxis.set_ticklabels(['no', 'yes']);
metrics.plot_roc_curve(random, X_test, y_test)
plt.show()
plt.figure(figsize=(12,10))

# logistic regression for feature importance
from sklearn.datasets import make_classification

model = random.best_estimator_
# get importance
importance = model.coef_[0]
# summarize feature importance
for i,v in enumerate(importance):
	print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
plt.bar([x for x in range(len(importance))], importance, tick_label = X.columns)
plt.xticks(rotation='vertical')
plt.show()<jupyter_output>Feature: 0, Score: 0.00034
Feature: 1, Score: -0.04217
Feature: 2, Score: 0.02660
Feature: 3, Score: 0.27544
Feature: 4, Score: 0.00003
Feature: 5, Score: -0.63895
Feature: 6, Score: -0.80299
Feature: 7, Score: -0.49652
Feature: 8, Score: 0.00337
Feature: 9, Score: -0.08078
Feature: 10, Score: 0.00371
Feature: 11, Score: 0.18775
Feature: 12, Score: 0.34711
Feature: 13, Score: -0.63357
Feature: 14, Score: -1.94430
Feature: 15, Score: 0.00000
Feature: 16, Score: 0.65581
Feature: 17, Score: -0.00570
Feature: 18, Score: 0.30695
Feature: 19, Score: 0.30561<jupyter_text>Naiwny klasyfikator Bayesowski<jupyter_code>from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
var_smoothing = [1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-10, 1e-11, 1e-12, 1e-13]

random_grid = {'var_smoothing': var_smoothing}


random = RandomizedSearchCV(estimator=gnb, param_distributions=random_grid, cv = 3, n_jobs=-1, scoring='f1')

random_result = random.fit(X_train, y_train)
# Summarize results
print("Best: %f using %s" % (random_result.best_score_, random_result.best_params_))

random.fit(X_train, y_train)

score = accuracy_score(y_test, random.predict(X_test))
f1 = f1_score(y_test, random.predict(X_test))
roc = roc_auc_score(y_test,random.predict(X_test))
recall = recall_score(y_test,random.predict(X_test))

print('Score:', score)
print('F1:', f1)
print('ROC:', roc)
print('Recall:', recall)

row_df = pd.DataFrame({'score':score, 'f1':f1, "roc":roc, "recall":recall}, index = ["GNB"])
scores = pd.concat([row_df, scores])
cm = confusion_matrix(y_test, random.predict(X_test))

ax= plt.subplot()
sns.heatmap(cm, annot=True, ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['no', 'yes']); ax.yaxis.set_ticklabels(['no', 'yes']);
metrics.plot_roc_curve(random, X_test, y_test)
plt.show()
X_y = X.copy()
X_y['Target'] = y

plt.figure(figsize=(15,10))
cor = X_y.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()<jupyter_output><empty_output><jupyter_text>XGB Classifier<jupyter_code>from xgboost import XGBClassifier

model=XGBClassifier()

random_state = [1, 10, 100]
learning_rate = [0.01, 0.05, 0.1, 0.25, 0.001, 0.005]
booster = ['gbtree', 'gblinear']
nround = [100,200, 50, 10]
max_depth = [1,4,5,10]

random_grid = {'random_state': random_state,
              'learning_rate': learning_rate,
              'booster': booster,
              'nround': nround,
              'max_depth': max_depth}



random = RandomizedSearchCV(estimator=model, param_distributions=random_grid, cv = 3, n_jobs=-1, scoring='f1')

random_result = random.fit(X_train, y_train)
# Summarize results
print("Best: %f using %s" % (random_result.best_score_, random_result.best_params_))

random.fit(X_train, y_train)

score = accuracy_score(y_test, random.predict(X_test))
f1 = f1_score(y_test, random.predict(X_test))
roc = roc_auc_score(y_test,random.predict(X_test))
recall = recall_score(y_test,random.predict(X_test))

print('Score:', score)
print('F1:', f1)
print('ROC:', roc)
print('Recall:', recall)

row_df = pd.DataFrame({'score':score, 'f1':f1, "roc":roc, "recall":recall}, index = ["XGBC"])
scores = pd.concat([row_df, scores])
cm = confusion_matrix(y_test, random.predict(X_test))

ax= plt.subplot()
sns.heatmap(cm, annot=True, ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['no', 'yes']); ax.yaxis.set_ticklabels(['no', 'yes']);
metrics.plot_roc_curve(random, X_test, y_test)
plt.show()
plt.figure(figsize=(12,10))

model = random.best_estimator_

print(model.feature_importances_)
# plot
plt.bar(range(len(model.feature_importances_)), model.feature_importances_, tick_label = X.columns)
plt.xticks(rotation = '90')
plt.show()<jupyter_output>[0.04416317 0.03420692 0.03709838 0.05433132 0.03611972 0.03754946
 0.04107123 0.09329443 0.11433203 0.03351653 0.08734518 0.04791636
 0.15748744 0.05593821 0.         0.         0.03549795 0.03143152
 0.03069975 0.02800033]<jupyter_text>Podsumowanie<jupyter_code>import matplotlib.pyplot as plt

plt.figure(figsize=(15,10))

# set width of bar
barWidth = 0.2
 
# set height of bar
bars1 = scores['score']
bars2 = scores['f1']
bars3 = scores['roc']
bars4 = scores['recall']
 
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
r4 = [x + barWidth for x in r3]
 
# Make the plot
plt.bar(r1, bars1, color='red', width=barWidth, edgecolor='white', label='Accuracy')
plt.bar(r2, bars2, color='blue', width=barWidth, edgecolor='white', label='F1')
plt.bar(r3, bars3, color='green', width=barWidth, edgecolor='white', label='ROC')
plt.bar(r4, bars4, color='yellow', width=barWidth, edgecolor='white', label='Recall')

 
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], scores.index)
 
# Create legend & Show graphic
plt.legend()
plt.show()

scores<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15240147783251232, 'fraction_numerical': 0.05147783251231527, 'mean_word_length': 6.539925719591458, 'pattern_counts': {'":': 10, '<': 46, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '125954', 'license': 'Apache-2.0', 'n_tokens_mistral': 6601, 'n_tokens_neox': 5999, 'n_words': 1213, 'path': 'Projekty/Projekt1/Grupa2/PaczoskiRozek/Projekt3.ipynb', 'repo_name': 'niladrem/2020L-WUM', 'size': '16249'}