<jupyter_start><jupyter_text>Goal: Take in the breast cancer set, use PCA to reduce the components. Visualise the results and then apply two different machine learning models to see which would be betterSteps1. Read in data set2. Standardise the data3. Use PCA to reduce down4. Data viz5. Apply Random Forest model6. Apply Logistical Regression model<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set_style('darkgrid')<jupyter_output><empty_output><jupyter_text>The data set is a part of sklearn datasets package<jupyter_code>from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
cancer.keys()<jupyter_output><empty_output><jupyter_text>Converting it into a dataframe using the keys we just found out<jupyter_code>df = pd.DataFrame(data=cancer['data'],columns=cancer['feature_names'])
df.head()<jupyter_output><empty_output><jupyter_text>Next I will standardise the data found in our data frame<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(df)
scaled_data=scaler.transform(df)<jupyter_output><empty_output><jupyter_text>The data is now scaled so we can use PCA to reduce the number of components<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(scaled_data)
x_pca = pca.transform(scaled_data)
x_pca.shape<jupyter_output><empty_output><jupyter_text>Let's see what this data looks like<jupyter_code>plt.figure(figsize=(8,6))
plt.scatter(x_pca[:,0],x_pca[:,1],c=cancer['target'],cmap='rainbow')
plt.xlabel('First Component')
plt.ylabel('Second Component')<jupyter_output><empty_output><jupyter_text>First, let's evaluate the Logisitc Regression machine learning model on this data<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_pca,cancer['target'],test_size=0.33)
# The target classification lies in the original data set
from sklearn.ensemble import RandomForestClassifier
rfc=RandomForestClassifier()
rfc.fit(X_train,y_train)
predictions = rfc.predict(X_test)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions))
print('\n')
print(classification_report(y_test,predictions))<jupyter_output>[[ 65   6]
 [ 10 107]]


             precision    recall  f1-score   support

          0       0.87      0.92      0.89        71
          1       0.95      0.91      0.93       117

avg / total       0.92      0.91      0.92       188<jupyter_text>Let's see if the logistical regression model will be more or less accurate Our data is already split, so we can skip this step<jupyter_code>from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(X_train,y_train)
predictions_lr = lr.predict(X_test)
print(confusion_matrix(y_test,predictions_lr))
print('\n')
print(classification_report(y_test,predictions_lr))<jupyter_output>[[ 70   1]
 [  5 112]]


             precision    recall  f1-score   support

          0       0.93      0.99      0.96        71
          1       0.99      0.96      0.97       117

avg / total       0.97      0.97      0.97       188
{'fraction_non_alphanumeric': 0.08626596075988789, 'fraction_numerical': 0.03270009342883837, 'mean_word_length': 4.605584642233857, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '303448', 'license': 'MIT', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1056, 'n_words': 275, 'path': 'Classifier Model for Breast Cancer Data.ipynb', 'repo_name': 'sallwright/Data-Science-Projects', 'size': '3211'}