import numpy as np import pandas as pd from time import time from IPython.display import display # Allows the use of display() for DataFrames
import visuals as vs
%matplotlib inline
data = pd.read_csv("census.csv")
display(data.head(n=1))
n_records = len(data)
n_greater_50k = (data['income']>50K).sum()
n_at_most_50k = n_records-n_greater_50k
greater_percent = (n_greater_50k)*100/(n_records)
print("Total number of records: {}".format(n_records)) print("Individuals making more than $50,000: {}".format(n_greater_50k)) print("Individuals making at most $50,000: {}".format(n_at_most_50k)) print("Percentage of individuals making more than $50,000: {}%".format(greater_percent))
income_raw = data['income'] features_raw = data.drop('income', axis = 1)
vs.distribution(data)
skewed = ['capital-gain', 'capital-loss'] features_log_transformed = pd.DataFrame(data = features_raw) features_log_transformed[skewed] = features_raw[skewed].apply(lambda x: np.log(x + 1))
vs.distribution(features_log_transformed, transformed = True)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler() # default=(0, 1) numerical = ['age', 'education-num', 'capital-gain', 'capital-loss', 'hours-per-week']
features_log_minmax_transform = pd.DataFrame(data = features_log_transformed) features_log_minmax_transform[numerical] = scaler.fit_transform(features_log_transformed[numerical])
display(features_log_minmax_transform.head(n = 5))
features_final = pd.get_dummies(features_log_minmax_transform)
income = income_raw.apply(lambda x:1 if x==">50K" else 0)
encoded = list(features_final.columns) print("{} total features after one-hot encoding.".format(len(encoded)))
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features_final, income, test_size = 0.2, random_state = 0)
print("Training set has {} samples.".format(X_train.shape[0])) print("Testing set has {} samples.".format(X_test.shape[0])) ''' TP = np.sum(income) # Counting the ones as this is the naive case. Note that 'income' is the 'income_raw' data encoded to numerical values done in the data preprocessing step. FP = income.count() - TP # Specific to the naive case TN = 0 # No predicted negatives in the naive case FN = 0 # No predicted negatives in the naive case '''
accuracy = n_greater_50k / n_records recall = n_greater_50k/(n_greater_50k+0) precision = n_at_most_50k/(n_greater_50k+n_at_most_50k)
TODO: Calculate F-score using the formula above for beta = 0.5 and correct values for precision and recall.
fscore = (1+0.50.5)(precisionrecall/((0.50.5*precision)+recall))
print("Naive Predictor: [Accuracy score: {:.4f}, F-score: {:.4f}]".format(accuracy, fscore))
from sklearn.metrics import fbeta_score,accuracy_score def train_predict(learner, sample_size, X_train, y_train, X_test, y_test): ''' inputs: - learner: the learning algorithm to be trained and predicted on - sample_size: the size of samples (number) to be drawn from training set - X_train: features training set - y_train: income training set - X_test: features testing set - y_test: income testing set '''
results = {}
# TODO: Fit the learner to the training data using slicing with 'sample_size' using .fit(training_features[:], training_labels[:])
start = time() # Get start time
learner = learner.fit(X_train[:sample_size],y_train[sample_size])
end = time() # Get end time
# TODO: Calculate the training time
results['train_time'] = end-start
# TODO: Get the predictions on the test set(X_test),
# then get predictions on the first 300 training samples(X_train) using .predict()
start = time() # Get start time
predictions_test = learner.predict(X_test)
predictions_train = learner.predict(X_train[:300])
end = time() # Get end time
# TODO: Calculate the total prediction time
results['pred_time'] = end-start
# TODO: Compute accuracy on the first 300 training samples which is y_train[:300]
results['acc_train'] = accuracy_score(y_train[:300],predictions_train)
# TODO: Compute accuracy on test set using accuracy_score()
results['acc_test'] = accuracy_score(y_test,predictions_test)
# TODO: Compute F-score on the the first 300 training samples using fbeta_score()
results['f_train'] = fbeta_score(y_train[:300],predictions_train,0.5)
# TODO: Compute F-score on the test set which is y_test
results['f_test'] = fbeta_score(y_test,predictions_test,0.5)
# Success
print("{} trained on {} samples.".format(learner.__class__.__name__, sample_size))
# Return the results
return results
# TODO: Import the three supervised learning models from sklearn
from sklearn.tree import DecisionTreeClassifier from sklearn.svm import SVC from sklearn.ensemble import AdaBoostClassifier
clf_A = DecisionTreeClassifier() clf_B = SVC() clf_C = AdaBoostClassifier()
HINT: samples_10 is 10% of samples_100 (ensure to set the count of the values to be int
and not float
)
HINT: samples_1 is 1% of samples_100 (ensure to set the count of the values to be int
and not float
)
samples_100 = len(y_train) samples_10 = int(round(len(y_train)/10)) samples_1 = int(round(len(y_train)/100))
results = {}
for clf in [clf_A, clf_B, clf_C]:
clf_name = clf.class.name
results[clf_name] = {}
for i, samples in enumerate([samples_1, samples_10, samples_100]):
results[clf_name][i] =
train_predict(clf, samples, X_train, y_train, X_test, y_test)
vs.evaluate(results, accuracy, fscore)
from sklearn.grid_search import GridSearchCV from sklearn.metrics import make_scorer
clf = AdaBoostClassifier(base_estimator=DecisionTreeClassifier())
parameters = {'n_estimators':[50, 120], 'learning_rate':[0.1, 0.5, 1.],'base_estimator__min_samples_split' : np.arange(2, 8, 2),'base_estimator__max_depth' : np.arange(1, 4, 1)}
scorer = make_scorer(fbeta_score,beta=0.5)
TODO: Perform grid search on the classifier using 'scorer' as the scoring method using GridSearchCV()
grid_obj = GridSearchCV(clf,parameters,scorer)
grid_fit = grid_obj.fit(X_train,y_train)
best_clf = grid_fit.best_estimator_
predictions = (clf.fit(X_train, y_train)).predict(X_test) best_predictions = best_clf.predict(X_test)
print("Unoptimized model\n------") print("Accuracy score on testing data: {:.4f}".format(accuracy_score(y_test, predictions))) print("F-score on testing data: {:.4f}".format(fbeta_score(y_test, predictions, beta = 0.5))) print("\nOptimized Model\n------") print("Final accuracy score on the testing data: {:.4f}".format(accuracy_score(y_test, best_predictions))) print("Final F-score on the testing data: {:.4f}".format(fbeta_score(y_test, best_predictions, beta = 0.5)))
model = AdaBoostClassifier().fit(X_train,y_train)
importances = model.feature_importances_
vs.feature_plot(importances, X_train, y_train)
from sklearn.base import clone
X_train_reduced = X_train[X_train.columns.values[(np.argsort(importances)[::-1])[:5]]] X_test_reduced = X_test[X_test.columns.values[(np.argsort(importances)[::-1])[:5]]]
clf = (clone(best_clf)).fit(X_train_reduced, y_train)
reduced_predictions = clf.predict(X_test_reduced)
print("Final Model trained on full data\n------") print("Accuracy on testing data: {:.4f}".format(accuracy_score(y_test, best_predictions))) print("F-score on testing data: {:.4f}".format(fbeta_score(y_test, best_predictions, beta = 0.5))) print("\nFinal Model trained on reduced data\n------") print("Accuracy on testing data: {:.4f}".format(accuracy_score(y_test, reduced_predictions))) print("F-score on testing data: {:.4f}".format(fbeta_score(y_test, reduced_predictions, beta = 0.5)))