<jupyter_start><jupyter_text>Python Coding and Dataset<jupyter_code>import pandas as pd
df = pd.read_csv('breast-cancer.csv') #reading in breast cancer dataset
with open('field_names.txt') as f: #opening text file that will be header
    header = f.read().splitlines() 

df.columns = header #setting the list to be the header

df.head()
df.corr() #quick evaluation of correlation between features
M = df[df.diagnosis == 'M'] #dataframe with malginant tumors
B = df[df.diagnosis == 'B'] #dataframe of benign tumors
df.columns
print 'Malignant Smoothness Mean', M.smoothness_mean.mean()
print 'Malignant Smoothness Median', M.smoothness_mean.median()
print 'Benign Smoothness Mean', B.smoothness_mean.mean()
print 'Benign Smoothness Median', B.smoothness_mean.median()
print 'Malignant Compactness Mean', M.compactness_mean.mean()
print 'Malignant Compactness Median', M.compactness_mean.median()
print 'Benign Compactness Mean', B.compactness_mean.mean()
print 'Benign Compactness Median', B.compactness_mean.median()<jupyter_output>Malignant Compactness Mean 0.0322017393365
Malignant Compactness Median 0.02855
Benign Compactness Mean 0.0214382464986
Benign Compactness Median 0.01631<jupyter_text>The average smoothness and compactness is noticeably larger for malignant tumors.<jupyter_code>df.shape<jupyter_output><empty_output><jupyter_text>This will generate bootstrap samples of the data.<jupyter_code>#change n to change number of samples
n=50
df.iloc[np.random.randint(568, size=n)] #randomly select n-amount of samples from df. Randomly selects from index location of dataframe.<jupyter_output><empty_output><jupyter_text>Exploratory Analysis<jupyter_code>diff = M.mean()-B.mean()
#looking at the difference between the mean values of the columns between benign and malignant tumors
#to see which vary the largest
diff
import matplotlib.pyplot
%matplotlib inline

import seaborn as sns
sns.distplot(M.smoothness_mean, label = ['M'])
sns.distplot(B.smoothness_mean, label = ['B'])
plt.legend(loc='upper right')

sns.distplot(M.concave_points_worst, label = ['M'])
sns.distplot(B.concave_points_worst, label = ['B'])
plt.legend(loc='upper right')

sns.distplot(M.texture_mean, label = ['M'])
sns.distplot(B.texture_mean, label = ['B'])
plt.legend(loc='upper right')<jupyter_output><empty_output><jupyter_text>With these three features, one largely identifying trait is that the malignant tumors have a wider distribution.   Modeling<jupyter_code>X = df.loc[:, 'radius_mean' : 'fractal_dimension_worst']
y = df.diagnosis
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
X = std.fit_transform(X)
from sklearn.cross_validation import train_test_split, KFold
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, classification_report
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42, stratify=y)
def evaluate_model(model):
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)    
    a = accuracy_score(y_test, y_pred)    
    cm = confusion_matrix(y_test, y_pred)
    cr = classification_report(y_test, y_pred)    
    print cm
    print cr    
    return 'accuracy score', a
#I'm getting hungry, time for a free lunch
import numpy
import xgboost as xgb
from sklearn import model_selection
from sklearn.grid_search import GridSearchCV


params = {
 'max_depth':range(3,8,1),
 'min_child_weight':range(1,8,2),
 'n_estimators':range(100,400,50)
}
gsearch1 = GridSearchCV(estimator = xgb.XGBClassifier( learning_rate =0.1, n_estimators=140, max_depth=5,
 gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27), 
 param_grid = params, scoring='accuracy',n_jobs=-1,iid=False, cv=KFold(len(y), n_folds=3, shuffle=True))
gsearch1.fit(X, y)
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
model = xgb.XGBClassifier( learning_rate =0.1, max_depth=4, gamma=0, subsample=0.8, colsample_bytree=0.8, objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27, min_child_weight=1, n_estimators=100)
evaluate_model(model)<jupyter_output>[[118   0]
 [  3  67]]
             precision    recall  f1-score   support

          B       0.98      1.00      0.99       118
          M       1.00      0.96      0.98        70

avg / total       0.98      0.98      0.98       188<jupyter_text>Model has 98.4% accuracy , that free lunch is delicious nomnomnom<jupyter_code>import numpy as np
from sklearn import svm
model1 = svm.SVC()
gamma_range = 10.**np.arange(-5, 2)
C_range = 10.**np.arange(-2, 3)
kernel_range = ['rbf', 'sigmoid', 'linear', 'poly']
param_grid = dict(gamma=gamma_range, C=C_range, kernel=kernel_range)
grid = GridSearchCV(model1, param_grid, cv=KFold(len(y), n_folds=3, shuffle=True), scoring='accuracy')
grid.fit(X, y)

print grid.best_score_
grid.best_params_
grid.best_estimator_
evaluate_model(svm.SVC(C=0.10000000000000001, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=1.0000000000000001e-05,
  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False))<jupyter_output>[[118   0]
 [  3  67]]
             precision    recall  f1-score   support

          B       0.98      1.00      0.99       118
          M       1.00      0.96      0.98        70

avg / total       0.98      0.98      0.98       188<jupyter_text>Feature importances of XGBoost Model<jupyter_code>import matplotlib.pyplot as plt
print(model.feature_importances_)

plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
plt.xlabel('Features')
labels = list(X)
plt.xticks(range(X.shape[1]), labels, rotation=90)
plt.show()<jupyter_output>[ 0.03325416  0.11163896  0.          0.02375297  0.03325416  0.00712589
  0.01425178  0.06175772  0.01425178  0.01662708  0.00475059  0.
  0.00712589  0.07363421  0.01900238  0.02137767  0.00950119  0.01187648
  0.01187648  0.01425178  0.02850356  0.11163896  0.04275534  0.08788598
  0.06175772  0.00950119  0.06413302  0.06175772  0.03562946  0.00712589]
{'fraction_non_alphanumeric': 0.11116568434839555, 'fraction_numerical': 0.08316961362148002, 'mean_word_length': 4.774102079395085, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '603164', 'license': 'MIT', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2191, 'n_words': 507, 'path': 'Coding Challenge.ipynb', 'repo_name': 'ikekram/mylittlepony', 'size': '6108'}