"""
# Introduction:
### In this report, I am going to compare the accuracy of the prediction of breast_cancer of methods of feature engineering. Feature selection and feature extraction will be included in the process.
"""
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import sklearn.datasets
dt=sklearn.datasets.load_breast_cancer()
print(dt.DESCR)
breast_data=pd.DataFrame(dt.data,columns=dt.feature_names)
breast_data
dt.feature_names
"""
<br>
<br>

# Feature Extraction: PCA
"""
from sklearn.decomposition import PCA
X=breast_data
y=dt.target
cls=PCA(n_components=2)
cls.fit(X)
X_pca=cls.transform(X)
X_pca=pd.DataFrame(X_pca,columns=["PCA1","PCA2"])
X_pca["y"]=y
X_pca
from collections import Counter
Counter(y)
"""
Confirm that this is not a sparse data.
<br>
<br>
# Feature Selection: Random Forest
"""
from  sklearn.ensemble import RandomForestClassifier as rfc
cls=rfc(n_estimators=500,random_state=666)
cls.fit(X,y)
result=pd.DataFrame(cls.feature_importances_,index=[dt.feature_names],columns=["Significance"])
result_sort=result.sort_values(by=["Significance"],ascending=False)
result_sort
plt.bar(range(0,30),result_sort["Significance"])
plt.xticks(range(30), 
           result_sort.index, rotation=90)
plt.xlim([-1, 30])
plt.tight_layout()
plt.title("Significance of each variable")
plt.show()
"""
The two most significant variable is "worst perimeter" and "worst concave points".

<br>
<br>

# Logistic Regression:

In this part, I will use the extracted and seleced variable above to construct the logistic regression model and observe the accuracy of each model.
"""
sig=breast_data[["worst perimeter","worst concave points"]]
reg_data=pd.concat([sig,X_pca],axis=1)
reg_data
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(reg_data.drop(columns=["y"]),reg_data["y"],
                                               train_size=0.8,random_state=666)
from sklearn.linear_model import LogisticRegression as LR
X_train=pd.DataFrame(X_train,columns=["worst perimeter","worst concave points","PCA1","PCA2"])
X_test=pd.DataFrame(X_test,columns=["worst perimeter","worst concave points","PCA1","PCA2"])
cls=LR()
cls.fit(X_train[["PCA1","PCA2"]],y_train) #Feature Extraction: PCA
cls.score(X_test[["PCA1","PCA2"]],y_test)
cls.fit(X_train[["worst perimeter","worst concave points"]],y_train) # Feature Selection: Random Forest
cls.score(X_test[["worst perimeter","worst concave points"]],y_test)
"""
# Conclusion:
In this case, the final result shows that using feature selection to reduce the dimension of data is more effective and have a higher accuracy.
"""