<jupyter_start><jupyter_text>Random Forest<jupyter_code>from sklearn.model_selection import train_test_split

training_sample2, testing_sample2 = train_test_split(df_cancer, test_size=0.3, random_state=42)

print("Training data set size = ", len(training_sample2))
print("Testing data set size = ", len(testing_sample2))
train_labels = np.array(training_sample2['iscancerous'])
train_features = training_sample2.drop('iscancerous', axis=1)

training_sample2 = train_features
train_feature_list = list(train_features.columns)

train_features = np.array(train_features)

from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_estimators = 400, random_state = 42)

rf.fit(train_features, train_labels)
print('Num of classes')
print(rf.n_classes_)
print('--------------------')
print('Class labels')
print(rf.classes_)
print('--------------------')
print('Num of features when fit is perform')
print(rf.n_features_)
print('--------------------')
print('Num of outputs when fit is performed')
print(rf.n_outputs_)
print('--------------------')
print('Feature Importance')
print(rf.feature_importances_)
from sklearn.model_selection import cross_validate

test_labels = np.array(testing_sample2['iscancerous'])
test_features = testing_sample2.drop('iscancerous', axis=1)

testing_sample2 = test_features
test_feature_list = list(test_features.columns)

test_features = np.array(test_features)

accuracy_rf = cross_validate(rf, test_features, test_labels, cv=10)['test_score']
print('The accuracy is: ',sum(accuracy_rf)/len(accuracy_rf)*100,'%')
# Get numerical feature importances
import matplotlib.pyplot as plt

%matplotlib inline

importances = list(rf.feature_importances_)

# Set the style
plt.style.use('fivethirtyeight')
plt.figure(figsize=(16,8))

# list of x locations for plotting
x_values = list(range(len(importances)))

# Make a bar chart
plt.bar(x_values, importances, orientation = 'vertical')

# Tick labels for x axis
plt.xticks(x_values, train_feature_list, rotation='vertical')

# Axis labels and title
plt.ylabel('Importance'); plt.xlabel('Variable'); plt.title('Variable Importances');
importances = pd.Series(rf.feature_importances_)
names = pd.Series(train_feature_list)
import_df = pd.concat([importances, names], axis=1)
skim_df = import_df.sort_values(by=[0] , ascending = False) 
skim_df.reset_index(inplace = True)
del skim_df["index"]
thin_df = skim_df.loc[0:11]
col_list = ['iscancerous']
col_list = col_list + (list(thin_df.loc[:,1]))
df_slim = df_cancer[col_list]
df_slim.corr()<jupyter_output><empty_output><jupyter_text>Principal Component Analysis<jupyter_code># PCA 
from sklearn.decomposition import PCA 
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import scale

cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

X = df_cancer.drop('iscancerous', axis = 1).values
y = df_cancer['iscancerous']

X_scale = scale(X)

pca_model = PCA()
X_PCA = pca_model.fit_transform(X_scale)

plt.scatter(X_PCA[:, 0], X_PCA[:, 1], c=y, cmap=cmap_bold,s=30)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
#eval of PCA components
var_ratio = pca_model.explained_variance_ratio_
print(var_ratio)

#creating df with var_ratio
df_pca = pd.DataFrame(X_PCA)

#plotting df_cancer with PCA
plt.plot(var_ratio, '-o')

plt.ylabel('Proportion of Variance Explained')
plt.xlabel('Principal Component')
plt.xticks(range(len(var_ratio)))
plt.show()<jupyter_output>[4.42720256e-01 1.89711820e-01 9.39316326e-02 6.60213492e-02
 5.49576849e-02 4.02452204e-02 2.25073371e-02 1.58872380e-02
 1.38964937e-02 1.16897819e-02 9.79718988e-03 8.70537901e-03
 8.04524987e-03 5.23365745e-03 3.13783217e-03 2.66209337e-03
 1.97996793e-03 1.75395945e-03 1.64925306e-03 1.03864675e-03
 9.99096464e-04 9.14646751e-04 8.11361259e-04 6.01833567e-04
 5.16042379e-04 2.72587995e-04 2.30015463e-04 5.29779290e-05
 2.49601032e-05 4.43482743e-06]<jupyter_text>Modeling with high importance variables<jupyter_code>#logit models
import statsmodels.formula.api as sm
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score, cross_validate

#splitting into training test
X = df_slim.drop('iscancerous', axis = 1).values
y = df_slim['iscancerous'].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

#stats modeling
logit_1 = sm.logit(formula="iscancerous~concave_points_worst+area_worst+concave_points_mean+perimeter_worst+radius_worst+concavity_mean+perimeter_mean+concavity_worst+radius_mean+area_se+area_mean+radius_se", 
                   data=df_slim).fit()

logit_1.summary()
#sklearn modeling
model_log = LogisticRegression(random_state=0, solver='lbfgs',max_iter = 200) 
model_log.fit(X_train, y_train)
y_pred_log = model_log.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_log))
print(model_log.score(X_test,y_test))
# SVM modeling
from sklearn import svm

model_svm = svm.SVC(kernel='linear', C=6)
model_svm.fit(X_train, y_train)    
y_pred_svm = model_svm.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_svm))
print(model_svm.score(X_test,y_test))
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import scale

X_scale = scale(X)

X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size=0.3, random_state=42)

model_mlp = MLPClassifier(hidden_layer_sizes=(40),
                          solver='lbfgs',  
                          tol=.1, 
                          random_state=1)

model_mlp.fit(X_train, y_train)    
y_pred_mlp = model_mlp.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_mlp))
print(model_mlp.score(X_test, y_test))<jupyter_output>[[106   2]
 [  7  56]]
0.9473684210526315<jupyter_text>Modeling with High Correlation Variables<jupyter_code>#logit models

#splitting into training test
X_sig = df_sig.drop('iscancerous', axis = 1).values
y_sig = df_sig['iscancerous'].values

X_train, X_test, y_train, y_test = train_test_split(X_sig, y_sig, test_size=0.3, random_state=42)

#stats modeling
logit_1_sig = sm.logit(formula="iscancerous~concave_points_worst+perimeter_worst+concave_points_mean+radius_worst+perimeter_mean+area_worst+radius_mean+area_mean+concavity_mean+concavity_worst+compactness_mean+compactness_worst+radius_se+perimeter_se+area_se", 
                   data=df_sig).fit()

logit_1_sig.summary()
#sklearn Logit modeling
model_log = LogisticRegression(random_state=0, solver='lbfgs', max_iter = 200) 
model_log.fit(X_train, y_train)
y_pred_log = model_log.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_log))
print(model_log.score(X_test,y_test))
# SVM modeling
from sklearn import svm

model_svm = svm.SVC(kernel='linear', C=6)
model_svm.fit(X_train, y_train)    
y_pred_svm = model_svm.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_svm))
print(model_svm.score(X_test,y_test))
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import scale

X_scale = scale(X_sig)

X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size=0.3, random_state=42)

model_mlp = MLPClassifier(hidden_layer_sizes=(40),
                          solver='lbfgs',  
                          tol=.1, 
                          random_state=1)

model_mlp.fit(X_train, y_train)    
y_pred_mlp = model_mlp.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_mlp))
print(model_mlp.score(X_test, y_test))<jupyter_output>[[106   2]
 [  4  59]]
0.9649122807017544<jupyter_text>Modeling with PCA<jupyter_code>df_pca.head(10)
#logit models

#splitting into training test
X_pca = df_pca.loc[:,0:6].values
y = df_sig['iscancerous'].values

X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.3, random_state=42)
#sklearn Logit modeling
model_log = LogisticRegression(random_state=0, solver='lbfgs', max_iter = 200) 
model_log.fit(X_train, y_train)
y_pred_log = model_log.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_log))
print(model_log.score(X_test,y_test))
# SVM modeling
from sklearn import svm

model_svm = svm.SVC(kernel='linear', C=6)
model_svm.fit(X_train, y_train)    
y_pred_svm = model_svm.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_svm))
print(model_svm.score(X_test,y_test))
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import scale

# X_scale = scale(X_sig)

# X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size=0.3, random_state=42)

model_mlp = MLPClassifier(hidden_layer_sizes=(10),
                          solver='lbfgs',  
                          tol=.1, 
                          random_state=1)

model_mlp.fit(X_train, y_train)    
y_pred_mlp = model_mlp.predict(X_test)

print(confusion_matrix(y_true = y_test, y_pred = y_pred_mlp))
print(model_mlp.score(X_test, y_test))<jupyter_output>[[106   2]
 [  2  61]]
0.9766081871345029
{'fraction_non_alphanumeric': 0.1223521624007061, 'fraction_numerical': 0.0572594880847308, 'mean_word_length': 5.685103244837758, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '232780', 'license': 'MIT', 'n_tokens_mistral': 3809, 'n_tokens_neox': 3475, 'n_words': 581, 'path': 'Cancer_project.ipynb', 'repo_name': 'ronmexico7811/Cancer-Project', 'size': '9064'}