diff --git a/doc/pub/LogReg/html/._LogReg-bs000.html b/doc/pub/LogReg/html/._LogReg-bs000.html index 91034fec6..dfe90dfca 100644 --- a/doc/pub/LogReg/html/._LogReg-bs000.html +++ b/doc/pub/LogReg/html/._LogReg-bs000.html @@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source ('More classes', 2, None, '___sec14'), ('A simple classification problem', 2, None, '___sec15'), ('The Credit Card example', 2, None, '___sec16'), - ('Analysis of the credi card example', 2, None, '___sec17')]} + ('How to read the Credit Card data', 2, None, '___sec17')]} end of tocinfo -->
@@ -117,7 +117,7 @@ MathJax.Hub.Config({-
diff --git a/doc/pub/LogReg/html/._LogReg-bs001.html b/doc/pub/LogReg/html/._LogReg-bs001.html index 675312c5f..824c211c1 100644 --- a/doc/pub/LogReg/html/._LogReg-bs001.html +++ b/doc/pub/LogReg/html/._LogReg-bs001.html @@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source ('More classes', 2, None, '___sec14'), ('A simple classification problem', 2, None, '___sec15'), ('The Credit Card example', 2, None, '___sec16'), - ('Analysis of the credi card example', 2, None, '___sec17')]} + ('How to read the Credit Card data', 2, None, '___sec17')]} end of tocinfo -->
@@ -117,7 +117,7 @@ MathJax.Hub.Config({+For categorical data -Scikit-Learn- provides a so-called one-hot encoder. +This is called one-hot +encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold). +Scikit-Learn provides a OneHotEncoder encoder to convert integer categorical values into one-hot
-
import pandas as pd
-import os
-import numpy as np
-
-
-from sklearn.model_selection import train_test_split
-from sklearn.preprocessing import OneHotEncoder
-from sklearn.compose import ColumnTransformer
-from sklearn.preprocessing import StandardScaler, OneHotEncoder
-from sklearn.metrics import confusion_matrix, accuracy_score, roc_auc_score
-
-# Trying to set the seed
-np.random.seed(0)
-import random
-random.seed(0)
-
-# Reading file into data frame
-cwd = os.getcwd()
-filename = cwd + '/default of credit card clients.xls'
-nanDict = {}
-df = pd.read_excel(filename, header=1, skiprows=0, index_col=0, na_values=nanDict)
-
-df.rename(index=str, columns={"default payment next month": "defaultPaymentNextMonth"}, inplace=True)
-
-# Features and targets
-X = df.loc[:, df.columns != 'defaultPaymentNextMonth'].values
-y = df.loc[:, df.columns == 'defaultPaymentNextMonth'].values
-
-# Categorical variables to one-hot's
-onehotencoder = OneHotEncoder(categories="auto")
-
-X = ColumnTransformer(
- [("", onehotencoder, [3]),],
- remainder="passthrough"
-).fit_transform(X)
-
-y.shape
-
-# Train-test split
-trainingShare = 0.5
-seed = 1
-XTrain, XTest, yTrain, yTest=train_test_split(X, y, train_size=trainingShare, \
- test_size = 1-trainingShare,
- random_state=seed)
-
-# Input Scaling
-sc = StandardScaler()
-XTrain = sc.fit_transform(XTrain)
-XTest = sc.transform(XTest)
-
-# One-hot's of the target vector
-Y_train_onehot, Y_test_onehot = onehotencoder.fit_transform(yTrain), onehotencoder.fit_transform(yTest)
-
-# Remove instances with zeros only for past bill statements or paid amounts
-'''
-df = df.drop(df[(df.BILL_AMT1 == 0) &
- (df.BILL_AMT2 == 0) &
- (df.BILL_AMT3 == 0) &
- (df.BILL_AMT4 == 0) &
- (df.BILL_AMT5 == 0) &
- (df.BILL_AMT6 == 0) &
- (df.PAY_AMT1 == 0) &
- (df.PAY_AMT2 == 0) &
- (df.PAY_AMT3 == 0) &
- (df.PAY_AMT4 == 0) &
- (df.PAY_AMT5 == 0) &
- (df.PAY_AMT6 == 0)].index)
-'''
-df = df.drop(df[(df.BILL_AMT1 == 0) &
- (df.BILL_AMT2 == 0) &
- (df.BILL_AMT3 == 0) &
- (df.BILL_AMT4 == 0) &
- (df.BILL_AMT5 == 0) &
- (df.BILL_AMT6 == 0)].index)
-
-df = df.drop(df[(df.PAY_AMT1 == 0) &
- (df.PAY_AMT2 == 0) &
- (df.PAY_AMT3 == 0) &
- (df.PAY_AMT4 == 0) &
- (df.PAY_AMT5 == 0) &
- (df.PAY_AMT6 == 0)].index)
-
-from sklearn.linear_model import LogisticRegression
-from sklearn.model_selection import GridSearchCV
-
-lambdas=np.logspace(-5,7,13)
-parameters = [{'C': 1./lambdas, "solver":["lbfgs"]}]#*len(parameters)}]
-scoring = ['accuracy', 'roc_auc']
-logReg = LogisticRegression()
-gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')
-- - -
# "refit" gives the metric used deciding best model.
-# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
-gridSearch.fit(XTrain, yTrain.ravel())
-
-def gridSearchSummary(method, scoring):
- """Prints best parameters from Grid search
- and AUC with standard deviation for all
- parameter combos """
-
- method = eval(method)
- if scoring == 'accuracy':
- mean = 'mean_test_score'
- sd = 'std_test_score'
- elif scoring == 'auc':
- mean = 'mean_test_roc_auc'
- sd = 'std_test_roc_auc'
- print("Best: %f using %s" % (method.best_score_, method.best_params_))
- means = method.cv_results_[mean]
- stds = method.cv_results_[sd]
- params = method.cv_results_['params']
- for mean, stdev, param in zip(means, stds, params):
- print("%f (%f) with: %r" % (mean, stdev, param))
-
-def createConfusionMatrix(method, printOut=True):
- """
- Computes and prints confusion matrices, accuracy scores,
- and AUC for test and training sets
- """
- confusionArray = np.zeros(6, dtype=object)
- method = eval(method)
-
- # Train
- yPredTrain = method.predict(XTrain)
- yPredTrain = (yPredTrain > 0.5)
- cm = confusion_matrix(
- yTrain, yPredTrain)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[0] = cm
-
- accScore = accuracy_score(yTrain, yPredTrain)
- confusionArray[1] = accScore
-
- AUC = roc_auc_score(yTrain, yPredTrain)
- confusionArray[2] = AUC
-
- if printOut:
- print('\n################### Training ###############')
- print('\nTraining Confusion matrix: \n', cm)
- print('\nTraining Accuracy score: \n', accScore)
- print('\nTrain AUC: \n', AUC)
-
- # Test
- yPred = method.predict(XTest)
- yPred = (yPred > 0.5)
- cm = confusion_matrix(
- yTest, yPred)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[3] = cm
-
- accScore = accuracy_score(yTest, yPred)
- confusionArray[4] = accScore
-
- AUC = roc_auc_score(yTest, yPred)
- confusionArray[5] = AUC
-
- if printOut:
- print('\n################### Testing ###############')
- print('\nTest Confusion matrix: \n', cm)
- print('\nTest Accuracy score: \n', accScore)
- print('\nTestAUC: \n', AUC)
-
- return confusionArray
-
-
-import matplotlib.pyplot as plt
-import seaborn
-import scikitplot as skplt
-
-seaborn.set(style="white", context="notebook", font_scale=1.5,
- rc={"axes.grid": True, "legend.frameon": False,
-"lines.markeredgewidth": 1.4, "lines.markersize": 10})
-seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
-
-yPred = gridSearch.predict_proba(XTest)
-print(yTest.ravel().shape, yPred.shape)
-
-#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
-skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
-
-defaults = sum(yTest == 1)
-total = len(yTest)
-defaultRate = defaults/total
-def bestCurve(defaults, total, defaultRate):
- x = np.linspace(0, 1, total)
-
- y1 = np.linspace(0, 1, defaults)
- y2 = np.ones(total-defaults)
- y3 = np.concatenate([y1,y2])
- return x, y3
-
-x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
-plt.plot(x, best)
-
-
-plt.show()
+from sklearn.preprocessing import OneHotEncoder
+encoder = OneHotEncoder()
diff --git a/doc/pub/LogReg/html/._LogReg-bs018.html b/doc/pub/LogReg/html/._LogReg-bs018.html
index 035c9ef3a..5cc27a194 100644
--- a/doc/pub/LogReg/html/._LogReg-bs018.html
+++ b/doc/pub/LogReg/html/._LogReg-bs018.html
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
- ('Analysis of the credi card example', 2, None, '___sec17')]}
+ ('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
+ + +
import pandas as pd
+import os
+import numpy as np
+
+
+from sklearn.model_selection import train_test_split
+from sklearn.preprocessing import OneHotEncoder
+from sklearn.compose import ColumnTransformer
+from sklearn.preprocessing import StandardScaler, OneHotEncoder
+from sklearn.metrics import confusion_matrix, accuracy_score, roc_auc_score
+
+# Trying to set the seed
+np.random.seed(0)
+import random
+random.seed(0)
+
+# Reading file into data frame
+cwd = os.getcwd()
+filename = cwd + '/default of credit card clients.xls'
+nanDict = {}
+df = pd.read_excel(filename, header=1, skiprows=0, index_col=0, na_values=nanDict)
+
+df.rename(index=str, columns={"default payment next month": "defaultPaymentNextMonth"}, inplace=True)
+
+# Features and targets
+X = df.loc[:, df.columns != 'defaultPaymentNextMonth'].values
+y = df.loc[:, df.columns == 'defaultPaymentNextMonth'].values
+
+# Categorical variables to one-hot's
+onehotencoder = OneHotEncoder(categories="auto")
+
+X = ColumnTransformer(
+ [("", onehotencoder, [3]),],
+ remainder="passthrough"
+).fit_transform(X)
+
+y.shape
+
+# Train-test split
+trainingShare = 0.5
+seed = 1
+XTrain, XTest, yTrain, yTest=train_test_split(X, y, train_size=trainingShare, \
+ test_size = 1-trainingShare,
+ random_state=seed)
+
+# Input Scaling
+sc = StandardScaler()
+XTrain = sc.fit_transform(XTrain)
+XTest = sc.transform(XTest)
+
+# One-hot's of the target vector
+Y_train_onehot, Y_test_onehot = onehotencoder.fit_transform(yTrain), onehotencoder.fit_transform(yTest)
+
+# Remove instances with zeros only for past bill statements or paid amounts
+'''
+df = df.drop(df[(df.BILL_AMT1 == 0) &
+ (df.BILL_AMT2 == 0) &
+ (df.BILL_AMT3 == 0) &
+ (df.BILL_AMT4 == 0) &
+ (df.BILL_AMT5 == 0) &
+ (df.BILL_AMT6 == 0) &
+ (df.PAY_AMT1 == 0) &
+ (df.PAY_AMT2 == 0) &
+ (df.PAY_AMT3 == 0) &
+ (df.PAY_AMT4 == 0) &
+ (df.PAY_AMT5 == 0) &
+ (df.PAY_AMT6 == 0)].index)
+'''
+df = df.drop(df[(df.BILL_AMT1 == 0) &
+ (df.BILL_AMT2 == 0) &
+ (df.BILL_AMT3 == 0) &
+ (df.BILL_AMT4 == 0) &
+ (df.BILL_AMT5 == 0) &
+ (df.BILL_AMT6 == 0)].index)
+
+df = df.drop(df[(df.PAY_AMT1 == 0) &
+ (df.PAY_AMT2 == 0) &
+ (df.PAY_AMT3 == 0) &
+ (df.PAY_AMT4 == 0) &
+ (df.PAY_AMT5 == 0) &
+ (df.PAY_AMT6 == 0)].index)
+
+from sklearn.linear_model import LogisticRegression
+from sklearn.model_selection import GridSearchCV
+
+lambdas=np.logspace(-5,7,13)
+parameters = [{'C': 1./lambdas, "solver":["lbfgs"]}]#*len(parameters)}]
+scoring = ['accuracy', 'roc_auc']
+logReg = LogisticRegression()
+gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')
+
diff --git a/doc/pub/LogReg/html/LogReg-bs.html b/doc/pub/LogReg/html/LogReg-bs.html index 91034fec6..dfe90dfca 100644 --- a/doc/pub/LogReg/html/LogReg-bs.html +++ b/doc/pub/LogReg/html/LogReg-bs.html @@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source ('More classes', 2, None, '___sec14'), ('A simple classification problem', 2, None, '___sec15'), ('The Credit Card example', 2, None, '___sec16'), - ('Analysis of the credi card example', 2, None, '___sec17')]} + ('How to read the Credit Card data', 2, None, '___sec17')]} end of tocinfo -->
@@ -117,7 +117,7 @@ MathJax.Hub.Config({-
diff --git a/doc/pub/LogReg/html/LogReg-reveal.html b/doc/pub/LogReg/html/LogReg-reveal.html index 6c7b64008..101656602 100644 --- a/doc/pub/LogReg/html/LogReg-reveal.html +++ b/doc/pub/LogReg/html/LogReg-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({
-
@@ -668,7 +668,24 @@ methods.
+For categorical data -Scikit-Learn- provides a so-called one-hot encoder.
+This is called one-hot
+encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
+Scikit-Learn provides a OneHotEncoder encoder to convert integer categorical values into one-hot
+
+
+
+
@@ -764,120 +781,6 @@ scoring = ['accuracy', 5, scoring=scoring, refit='roc_auc')
The Credit Card example
Here we use the the credit card data.
-The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
+The data are from an extensive database from Taiwan and include more than ten predictors.
+
+from sklearn.preprocessing import OneHotEncoder
+encoder = OneHotEncoder()
+
How to read the Credit Card data
- - -
# "refit" gives the metric used deciding best model.
-# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
-gridSearch.fit(XTrain, yTrain.ravel())
-
-def gridSearchSummary(method, scoring):
- """Prints best parameters from Grid search
- and AUC with standard deviation for all
- parameter combos """
-
- method = eval(method)
- if scoring == 'accuracy':
- mean = 'mean_test_score'
- sd = 'std_test_score'
- elif scoring == 'auc':
- mean = 'mean_test_roc_auc'
- sd = 'std_test_roc_auc'
- print("Best: %f using %s" % (method.best_score_, method.best_params_))
- means = method.cv_results_[mean]
- stds = method.cv_results_[sd]
- params = method.cv_results_['params']
- for mean, stdev, param in zip(means, stds, params):
- print("%f (%f) with: %r" % (mean, stdev, param))
-
-def createConfusionMatrix(method, printOut=True):
- """
- Computes and prints confusion matrices, accuracy scores,
- and AUC for test and training sets
- """
- confusionArray = np.zeros(6, dtype=object)
- method = eval(method)
-
- # Train
- yPredTrain = method.predict(XTrain)
- yPredTrain = (yPredTrain > 0.5)
- cm = confusion_matrix(
- yTrain, yPredTrain)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[0] = cm
-
- accScore = accuracy_score(yTrain, yPredTrain)
- confusionArray[1] = accScore
-
- AUC = roc_auc_score(yTrain, yPredTrain)
- confusionArray[2] = AUC
-
- if printOut:
- print('\n################### Training ###############')
- print('\nTraining Confusion matrix: \n', cm)
- print('\nTraining Accuracy score: \n', accScore)
- print('\nTrain AUC: \n', AUC)
-
- # Test
- yPred = method.predict(XTest)
- yPred = (yPred > 0.5)
- cm = confusion_matrix(
- yTest, yPred)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[3] = cm
-
- accScore = accuracy_score(yTest, yPred)
- confusionArray[4] = accScore
-
- AUC = roc_auc_score(yTest, yPred)
- confusionArray[5] = AUC
-
- if printOut:
- print('\n################### Testing ###############')
- print('\nTest Confusion matrix: \n', cm)
- print('\nTest Accuracy score: \n', accScore)
- print('\nTestAUC: \n', AUC)
-
- return confusionArray
-
-
-import matplotlib.pyplot as plt
-import seaborn
-import scikitplot as skplt
-
-seaborn.set(style="white", context="notebook", font_scale=1.5,
- rc={"axes.grid": True, "legend.frameon": False,
-"lines.markeredgewidth": 1.4, "lines.markersize": 10})
-seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
-
-yPred = gridSearch.predict_proba(XTest)
-print(yTest.ravel().shape, yPred.shape)
-
-#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
-skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
-
-defaults = sum(yTest == 1)
-total = len(yTest)
-defaultRate = defaults/total
-def bestCurve(defaults, total, defaultRate):
- x = np.linspace(0, 1, total)
-
- y1 = np.linspace(0, 1, defaults)
- y2 = np.ones(total-defaults)
- y3 = np.concatenate([y1,y2])
- return x, y3
-
-x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
-plt.plot(x, best)
-
-
-plt.show()
--
@@ -570,7 +570,23 @@ methods.
+For categorical data -Scikit-Learn- provides a so-called one-hot encoder. +This is called one-hot +encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold). +Scikit-Learn provides a OneHotEncoder encoder to convert integer categorical values into one-hot +
+ + +
from sklearn.preprocessing import OneHotEncoder
+encoder = OneHotEncoder()
+
+
+
+
@@ -668,118 +684,6 @@ gridSearch = GridSearchCV(logReg, parameters, cv=5<
-
-
-
-
@@ -575,7 +575,23 @@ methods.
+For categorical data -Scikit-Learn- provides a so-called one-hot encoder.
+This is called one-hot
+encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
+Scikit-Learn provides a OneHotEncoder encoder to convert integer categorical values into one-hot
+
+
+
+
+
@@ -673,118 +689,6 @@ gridSearch = GridSearchCV(logReg, parameters
-
-
-# "refit" gives the metric used deciding best model.
-# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
-gridSearch.fit(XTrain, yTrain.ravel())
-
-def gridSearchSummary(method, scoring):
- """Prints best parameters from Grid search
- and AUC with standard deviation for all
- parameter combos """
-
- method = eval(method)
- if scoring == 'accuracy':
- mean = 'mean_test_score'
- sd = 'std_test_score'
- elif scoring == 'auc':
- mean = 'mean_test_roc_auc'
- sd = 'std_test_roc_auc'
- print("Best: %f using %s" % (method.best_score_, method.best_params_))
- means = method.cv_results_[mean]
- stds = method.cv_results_[sd]
- params = method.cv_results_['params']
- for mean, stdev, param in zip(means, stds, params):
- print("%f (%f) with: %r" % (mean, stdev, param))
-
-def createConfusionMatrix(method, printOut=True):
- """
- Computes and prints confusion matrices, accuracy scores,
- and AUC for test and training sets
- """
- confusionArray = np.zeros(6, dtype=object)
- method = eval(method)
-
- # Train
- yPredTrain = method.predict(XTrain)
- yPredTrain = (yPredTrain > 0.5)
- cm = confusion_matrix(
- yTrain, yPredTrain)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[0] = cm
-
- accScore = accuracy_score(yTrain, yPredTrain)
- confusionArray[1] = accScore
-
- AUC = roc_auc_score(yTrain, yPredTrain)
- confusionArray[2] = AUC
-
- if printOut:
- print('\n################### Training ###############')
- print('\nTraining Confusion matrix: \n', cm)
- print('\nTraining Accuracy score: \n', accScore)
- print('\nTrain AUC: \n', AUC)
-
- # Test
- yPred = method.predict(XTest)
- yPred = (yPred > 0.5)
- cm = confusion_matrix(
- yTest, yPred)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[3] = cm
-
- accScore = accuracy_score(yTest, yPred)
- confusionArray[4] = accScore
-
- AUC = roc_auc_score(yTest, yPred)
- confusionArray[5] = AUC
-
- if printOut:
- print('\n################### Testing ###############')
- print('\nTest Confusion matrix: \n', cm)
- print('\nTest Accuracy score: \n', accScore)
- print('\nTestAUC: \n', AUC)
-
- return confusionArray
-
-
-import matplotlib.pyplot as plt
-import seaborn
-import scikitplot as skplt
-
-seaborn.set(style="white", context="notebook", font_scale=1.5,
- rc={"axes.grid": True, "legend.frameon": False,
-"lines.markeredgewidth": 1.4, "lines.markersize": 10})
-seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
-
-yPred = gridSearch.predict_proba(XTest)
-print(yTest.ravel().shape, yPred.shape)
-
-#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
-skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
-
-defaults = sum(yTest == 1)
-total = len(yTest)
-defaultRate = defaults/total
-def bestCurve(defaults, total, defaultRate):
- x = np.linspace(0, 1, total)
-
- y1 = np.linspace(0, 1, defaults)
- y2 = np.ones(total-defaults)
- y3 = np.concatenate([y1,y2])
- return x, y3
-
-x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
-plt.plot(x, best)
-
-
-plt.show()
-
-
-Analysis of the credi card example
-
diff --git a/doc/pub/LogReg/html/LogReg.html b/doc/pub/LogReg/html/LogReg.html
index dd6403806..22e15425c 100644
--- a/doc/pub/LogReg/html/LogReg.html
+++ b/doc/pub/LogReg/html/LogReg.html
@@ -61,7 +61,7 @@ div { text-align: justify; text-justify: inter-word; }
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
- ('Analysis of the credi card example', 2, None, '___sec17')]}
+ ('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
@@ -103,7 +103,7 @@ MathJax.Hub.Config({
Oct 9, 2019
Oct 17, 2019
The Credit Card example
Here we use the the credit card data.
-The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
+The data are from an extensive database from Taiwan and include more than ten predictors.
+
+from sklearn.preprocessing import OneHotEncoder
+encoder = OneHotEncoder()
+
+
+How to read the Credit Card data
# "refit" gives the metric used deciding best model.
-# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
-gridSearch.fit(XTrain, yTrain.ravel())
-
-def gridSearchSummary(method, scoring):
- """Prints best parameters from Grid search
- and AUC with standard deviation for all
- parameter combos """
-
- method = eval(method)
- if scoring == 'accuracy':
- mean = 'mean_test_score'
- sd = 'std_test_score'
- elif scoring == 'auc':
- mean = 'mean_test_roc_auc'
- sd = 'std_test_roc_auc'
- print("Best: %f using %s" % (method.best_score_, method.best_params_))
- means = method.cv_results_[mean]
- stds = method.cv_results_[sd]
- params = method.cv_results_['params']
- for mean, stdev, param in zip(means, stds, params):
- print("%f (%f) with: %r" % (mean, stdev, param))
-
-def createConfusionMatrix(method, printOut=True):
- """
- Computes and prints confusion matrices, accuracy scores,
- and AUC for test and training sets
- """
- confusionArray = np.zeros(6, dtype=object)
- method = eval(method)
-
- # Train
- yPredTrain = method.predict(XTrain)
- yPredTrain = (yPredTrain > 0.5)
- cm = confusion_matrix(
- yTrain, yPredTrain)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[0] = cm
-
- accScore = accuracy_score(yTrain, yPredTrain)
- confusionArray[1] = accScore
-
- AUC = roc_auc_score(yTrain, yPredTrain)
- confusionArray[2] = AUC
-
- if printOut:
- print('\n################### Training ###############')
- print('\nTraining Confusion matrix: \n', cm)
- print('\nTraining Accuracy score: \n', accScore)
- print('\nTrain AUC: \n', AUC)
-
- # Test
- yPred = method.predict(XTest)
- yPred = (yPred > 0.5)
- cm = confusion_matrix(
- yTest, yPred)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[3] = cm
-
- accScore = accuracy_score(yTest, yPred)
- confusionArray[4] = accScore
-
- AUC = roc_auc_score(yTest, yPred)
- confusionArray[5] = AUC
-
- if printOut:
- print('\n################### Testing ###############')
- print('\nTest Confusion matrix: \n', cm)
- print('\nTest Accuracy score: \n', accScore)
- print('\nTestAUC: \n', AUC)
-
- return confusionArray
-
-
-import matplotlib.pyplot as plt
-import seaborn
-import scikitplot as skplt
-
-seaborn.set(style="white", context="notebook", font_scale=1.5,
- rc={"axes.grid": True, "legend.frameon": False,
-"lines.markeredgewidth": 1.4, "lines.markersize": 10})
-seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
-
-yPred = gridSearch.predict_proba(XTest)
-print(yTest.ravel().shape, yPred.shape)
-
-#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
-skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
-
-defaults = sum(yTest == 1)
-total = len(yTest)
-defaultRate = defaults/total
-def bestCurve(defaults, total, defaultRate):
- x = np.linspace(0, 1, total)
-
- y1 = np.linspace(0, 1, defaults)
- y2 = np.ones(total-defaults)
- y3 = np.concatenate([y1,y2])
- return x, y3
-
-x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
-plt.plot(x, best)
-
-
-plt.show()
-
-
-Analysis of the credi card example
-
diff --git a/doc/pub/LogReg/ipynb/LogReg.ipynb b/doc/pub/LogReg/ipynb/LogReg.ipynb
index cb31cc32e..2566f60e9 100644
--- a/doc/pub/LogReg/ipynb/LogReg.ipynb
+++ b/doc/pub/LogReg/ipynb/LogReg.ipynb
@@ -10,7 +10,7 @@
" \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
- "Date: **Oct 9, 2019**\n",
+ "Date: **Oct 17, 2019**\n",
"\n",
"Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -669,7 +669,12 @@
"source": [
"## The Credit Card example\n",
"Here we use the the [credit card data](https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients). \n",
- "The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added."
+ "The data are from an extensive database from Taiwan and include more than ten predictors.\n",
+ "\n",
+ "For categorical data -Scikit-Learn- provides a so-called **one-hot encoder**.\n",
+ "This is called one-hot\n",
+ "encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).\n",
+ "**Scikit-Learn** provides a OneHotEncoder encoder to convert integer categorical values into one-hot"
]
},
{
@@ -679,6 +684,25 @@
"collapsed": false
},
"outputs": [],
+ "source": [
+ "from sklearn.preprocessing import OneHotEncoder\n",
+ "encoder = OneHotEncoder()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## How to read the Credit Card data"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
"source": [
"import pandas as pd\n",
"import os\n",
@@ -771,129 +795,6 @@
"logReg = LogisticRegression()\n",
"gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')"
]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "\n",
- "# \"refit\" gives the metric used deciding best model. \n",
- "# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html\n",
- "gridSearch.fit(XTrain, yTrain.ravel())\n",
- "\n",
- "def gridSearchSummary(method, scoring):\n",
- " \"\"\"Prints best parameters from Grid search\n",
- " and AUC with standard deviation for all \n",
- " parameter combos \"\"\"\n",
- " \n",
- " method = eval(method)\n",
- " if scoring == 'accuracy':\n",
- " mean = 'mean_test_score'\n",
- " sd = 'std_test_score'\n",
- " elif scoring == 'auc':\n",
- " mean = 'mean_test_roc_auc'\n",
- " sd = 'std_test_roc_auc'\n",
- " print(\"Best: %f using %s\" % (method.best_score_, method.best_params_))\n",
- " means = method.cv_results_[mean]\n",
- " stds = method.cv_results_[sd]\n",
- " params = method.cv_results_['params']\n",
- " for mean, stdev, param in zip(means, stds, params):\n",
- " print(\"%f (%f) with: %r\" % (mean, stdev, param))\n",
- "\n",
- "def createConfusionMatrix(method, printOut=True):\n",
- " \"\"\"\n",
- " Computes and prints confusion matrices, accuracy scores,\n",
- " and AUC for test and training sets \n",
- " \"\"\"\n",
- " confusionArray = np.zeros(6, dtype=object)\n",
- " method = eval(method)\n",
- " \n",
- " # Train\n",
- " yPredTrain = method.predict(XTrain)\n",
- " yPredTrain = (yPredTrain > 0.5)\n",
- " cm = confusion_matrix(\n",
- " yTrain, yPredTrain) \n",
- " cm = np.around(cm/cm.sum(axis=1)[:,None], 2)\n",
- " confusionArray[0] = cm\n",
- " \n",
- " accScore = accuracy_score(yTrain, yPredTrain)\n",
- " confusionArray[1] = accScore\n",
- " \n",
- " AUC = roc_auc_score(yTrain, yPredTrain)\n",
- " confusionArray[2] = AUC\n",
- " \n",
- " if printOut:\n",
- " print('\\n################### Training ###############')\n",
- " print('\\nTraining Confusion matrix: \\n', cm)\n",
- " print('\\nTraining Accuracy score: \\n', accScore)\n",
- " print('\\nTrain AUC: \\n', AUC)\n",
- " \n",
- " # Test\n",
- " yPred = method.predict(XTest)\n",
- " yPred = (yPred > 0.5)\n",
- " cm = confusion_matrix(\n",
- " yTest, yPred) \n",
- " cm = np.around(cm/cm.sum(axis=1)[:,None], 2)\n",
- " confusionArray[3] = cm\n",
- " \n",
- " accScore = accuracy_score(yTest, yPred)\n",
- " confusionArray[4] = accScore\n",
- " \n",
- " AUC = roc_auc_score(yTest, yPred)\n",
- " confusionArray[5] = AUC\n",
- " \n",
- " if printOut:\n",
- " print('\\n################### Testing ###############')\n",
- " print('\\nTest Confusion matrix: \\n', cm)\n",
- " print('\\nTest Accuracy score: \\n', accScore)\n",
- " print('\\nTestAUC: \\n', AUC) \n",
- " \n",
- " return confusionArray\n",
- "\n",
- "\n",
- "import matplotlib.pyplot as plt\n",
- "import seaborn\n",
- "import scikitplot as skplt\n",
- "\n",
- "seaborn.set(style=\"white\", context=\"notebook\", font_scale=1.5, \n",
- " rc={\"axes.grid\": True, \"legend.frameon\": False,\n",
- "\"lines.markeredgewidth\": 1.4, \"lines.markersize\": 10})\n",
- "seaborn.set_context(\"notebook\", font_scale=1.5, rc={\"lines.linewidth\": 4.5})\n",
- "\n",
- "yPred = gridSearch.predict_proba(XTest) \n",
- "print(yTest.ravel().shape, yPred.shape)\n",
- "\n",
- "#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)\n",
- "skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)\n",
- "\n",
- "defaults = sum(yTest == 1)\n",
- "total = len(yTest)\n",
- "defaultRate = defaults/total\n",
- "def bestCurve(defaults, total, defaultRate):\n",
- " x = np.linspace(0, 1, total)\n",
- " \n",
- " y1 = np.linspace(0, 1, defaults)\n",
- " y2 = np.ones(total-defaults)\n",
- " y3 = np.concatenate([y1,y2])\n",
- " return x, y3\n",
- "\n",
- "x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate) \n",
- "plt.plot(x, best) \n",
- "\n",
- "\n",
- "plt.show()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Analysis of the credi card example"
- ]
}
],
"metadata": {},
diff --git a/doc/pub/LogReg/ipynb/ipynb-LogReg-src.tar.gz b/doc/pub/LogReg/ipynb/ipynb-LogReg-src.tar.gz
index 120b7e1c0..3aff17299 100644
Binary files a/doc/pub/LogReg/ipynb/ipynb-LogReg-src.tar.gz and b/doc/pub/LogReg/ipynb/ipynb-LogReg-src.tar.gz differ
diff --git a/doc/pub/LogReg/pdf/LogReg-minted.pdf b/doc/pub/LogReg/pdf/LogReg-minted.pdf
index 75e86bb0b..aebddcc90 100644
Binary files a/doc/pub/LogReg/pdf/LogReg-minted.pdf and b/doc/pub/LogReg/pdf/LogReg-minted.pdf differ
diff --git a/doc/src/LogisticRegression/LogReg.do.txt b/doc/src/LogisticRegression/LogReg.do.txt
index 972550f74..7114b7915 100644
--- a/doc/src/LogisticRegression/LogReg.do.txt
+++ b/doc/src/LogisticRegression/LogReg.do.txt
@@ -451,7 +451,19 @@ if __name__ == "__main__":
!split
===== The Credit Card example =====
Here we use the the "credit card data":"https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients".
-The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
+The data are from an extensive database from Taiwan and include more than ten predictors.
+
+For categorical data -Scikit-Learn- provides a so-called _one-hot encoder_.
+This is called one-hot
+encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
+_Scikit-Learn_ provides a OneHotEncoder encoder to convert integer categorical values into one-hot
+!bc pycod
+from sklearn.preprocessing import OneHotEncoder
+encoder = OneHotEncoder()
+!ec
+
+!split
+===== How to read the Credit Card data =====
!bc pycod
import pandas as pd
@@ -546,116 +558,3 @@ logReg = LogisticRegression()
gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')
!ec
-!bc pycod
-
-# "refit" gives the metric used deciding best model.
-# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
-gridSearch.fit(XTrain, yTrain.ravel())
-
-def gridSearchSummary(method, scoring):
- """Prints best parameters from Grid search
- and AUC with standard deviation for all
- parameter combos """
-
- method = eval(method)
- if scoring == 'accuracy':
- mean = 'mean_test_score'
- sd = 'std_test_score'
- elif scoring == 'auc':
- mean = 'mean_test_roc_auc'
- sd = 'std_test_roc_auc'
- print("Best: %f using %s" % (method.best_score_, method.best_params_))
- means = method.cv_results_[mean]
- stds = method.cv_results_[sd]
- params = method.cv_results_['params']
- for mean, stdev, param in zip(means, stds, params):
- print("%f (%f) with: %r" % (mean, stdev, param))
-
-def createConfusionMatrix(method, printOut=True):
- """
- Computes and prints confusion matrices, accuracy scores,
- and AUC for test and training sets
- """
- confusionArray = np.zeros(6, dtype=object)
- method = eval(method)
-
- # Train
- yPredTrain = method.predict(XTrain)
- yPredTrain = (yPredTrain > 0.5)
- cm = confusion_matrix(
- yTrain, yPredTrain)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[0] = cm
-
- accScore = accuracy_score(yTrain, yPredTrain)
- confusionArray[1] = accScore
-
- AUC = roc_auc_score(yTrain, yPredTrain)
- confusionArray[2] = AUC
-
- if printOut:
- print('\n################### Training ###############')
- print('\nTraining Confusion matrix: \n', cm)
- print('\nTraining Accuracy score: \n', accScore)
- print('\nTrain AUC: \n', AUC)
-
- # Test
- yPred = method.predict(XTest)
- yPred = (yPred > 0.5)
- cm = confusion_matrix(
- yTest, yPred)
- cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
- confusionArray[3] = cm
-
- accScore = accuracy_score(yTest, yPred)
- confusionArray[4] = accScore
-
- AUC = roc_auc_score(yTest, yPred)
- confusionArray[5] = AUC
-
- if printOut:
- print('\n################### Testing ###############')
- print('\nTest Confusion matrix: \n', cm)
- print('\nTest Accuracy score: \n', accScore)
- print('\nTestAUC: \n', AUC)
-
- return confusionArray
-
-
-import matplotlib.pyplot as plt
-import seaborn
-import scikitplot as skplt
-
-seaborn.set(style="white", context="notebook", font_scale=1.5,
- rc={"axes.grid": True, "legend.frameon": False,
-"lines.markeredgewidth": 1.4, "lines.markersize": 10})
-seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
-
-yPred = gridSearch.predict_proba(XTest)
-print(yTest.ravel().shape, yPred.shape)
-
-#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
-skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
-
-defaults = sum(yTest == 1)
-total = len(yTest)
-defaultRate = defaults/total
-def bestCurve(defaults, total, defaultRate):
- x = np.linspace(0, 1, total)
-
- y1 = np.linspace(0, 1, defaults)
- y2 = np.ones(total-defaults)
- y3 = np.concatenate([y1,y2])
- return x, y3
-
-x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
-plt.plot(x, best)
-
-
-plt.show()
-
-!ec
-
-
-!split
-===== Analysis of the credi card example =====