<jupyter_start><jupyter_text>We have now installed and loaded the modules necessary for our analyses.  Importation of the datasetsThe breast cancer dataset (`breast-cancer-wisconsin.csv`) has 11 columns and 699 rows.Column descriptions: | n° | attribute   |Domain||------|------|------||   1  | Sample number|id number ||   2  | Clump thickness|1-10||   3  | Uniformity of Cell Size|1-10  ||   4  | Uniformity of Cell Shape|1-10  ||   5  | Marginal Adhesion |1-10  ||   6  | Single Epithelial Cell Size|1-10  ||   7  | Bare Nuclei   |1-10  ||   8  | Bland Chromatin |1-10  ||   9  | Normal Nucleoli |1-10  ||   10  | Mitoses |1-10  ||   11  | Class|2,4  |For the cancer class (column "Class"):- 2: benign cancer- 4: malignant cancer<jupyter_code># import dataset
df = pd.read_csv(filepath_or_buffer="breast-cancer-wisconsin.csv",header=None)

# rename columns
col_names = ["CodeNumber", "ClumpThickness", "UniformityCellSize", "UniformityCellShape", "MarginalAdhesion",
               "SingleEpithelialCellSize", "BareNuclei","BlandChromatin", "NormalNucleoli", "Mitoses",
               "CancerType"]
df.columns= col_names
df.head()
# drop the BareNuclei column (since it contains missing values coded as "?")
del df["BareNuclei"]
df.head()<jupyter_output><empty_output><jupyter_text>Split the original data into a train and a test datasetWe are now going to use our original dataset to:- train a Random Forest (RF) model- test our RF model on a test dataset.We are going to split our dataset (`breast-cancer-wisconsin`) into a train and a test dataset.<jupyter_code># specify the label (=y) variable (cancer class)
label = df.CancerType

# specify the features (=x) variables (measured variables)
features = df.iloc[:,1:9]

# Split dataset into a random train and test subsets
# We need to indicate which subset corresponds to the features/variables and which column corresponds to the class we are trying to predict. 
# Finally, we also indicate the percentage of the dataset to include into the train split (proportion of samples used to train the model)
train_x, test_x, train_y, test_y = train_test_split(features,label,train_size=0.7,test_size=0.3)
# Let's do some "sanity checks"

# Compute number of samples in training
print(train_x.shape[0])
# Compute number of samples in test
print(test_x.shape[0])
# Compute total number of samples<jupyter_output>489
210<jupyter_text>Random Forest classifier Training the classifier<jupyter_code># Set Random Forest parameters
n_estimators=30
criterion='gini'
max_depth=30
min_samples_split=5
min_samples_leaf=5
max_features='auto'
max_leaf_nodes=None
bootstrap=True
oob_score=True
n_jobs=1
random_state=None
verbose=0
class_weight='balanced'

# build the Random Forest classifier
forest = RandomForestClassifier(n_estimators=n_estimators, criterion=criterion, max_depth=max_depth,
                             min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf,
                             max_features=max_features, max_leaf_nodes=max_leaf_nodes, bootstrap=bootstrap, oob_score=oob_score,
                             n_jobs=n_jobs, random_state=random_state, verbose=verbose,class_weight=class_weight)

# train the Random Forest classifier on our dataset
RF_classifier = forest.fit(train_x, train_y)<jupyter_output><empty_output><jupyter_text>Benchmarking the performance of the trained RF classifier on the test dataset<jupyter_code># Validation
mypredtest=RF_classifier.predict(test_x)

print(classification_report(test_y, mypredtest)) 
# Calculation and graphical representation of the confusion matrix

# definition of a helper function (taken from 
# http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html)

# get the target labels (it is an output from the classifier)
target_names=RF_classifier.classes_

def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(target_names))
    plt.xticks(tick_marks, target_names, rotation=45)
    plt.yticks(tick_marks, target_names)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')


# Compute confusion matrix
cm = confusion_matrix(test_y, mypredtest)
np.set_printoptions(precision=2)
print('Confusion matrix, without normalization')
print(cm)
plt.figure()
plot_confusion_matrix(cm)

cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print('Normalized confusion matrix')
print(cm_normalized)
plt.figure()
plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')

plt.show()<jupyter_output>Confusion matrix, without normalization
[[139   6]
 [  1  64]]
Normalized confusion matrix
[[ 0.96  0.04]
 [ 0.02  0.98]]<jupyter_text>Getting the best predictors of the cancer class (benign=2/malign=4)<jupyter_code># Get feature importance

importances=RF_classifier.feature_importances_
indices = np.argsort(importances)[::-1]

# Get the name of each features
feature_list=np.array(features.columns[0:9])

# Visualize
plt.figure()
plt.title("Feature importances")
plt.bar(range(train_x.shape[1]), importances[indices],
       color="r", align="center")
plt.xticks(range(train_x.shape[1]), feature_list[indices],rotation=45,horizontalalignment='right')
plt.xlim([-1, train_x.shape[1]])
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10660744031093837, 'fraction_numerical': 0.01943364797334814, 'mean_word_length': 5.283720930232558, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '219721', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1719, 'n_words': 461, 'path': 'lessons/20180529_Machine_Learning_Zsofia_Stefania_Marc/Preview_ML_Lesson.ipynb', 'repo_name': 'JoachimGoedhart/studyGroup', 'size': '5403'}