Amyloid beta 

##Library
```python
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

from tqdm import tqdm

from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
import graphviz 
from sklearn.tree import export_graphviz

from sklearn.metrics import confusion_matrix, accuracy_score, f1_score, mean_squared_error
```

##Data
```python
print ('av45 shape: ', df_av45.shape)
df_av45.head()
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/274c6b83-4134-0957-9fef-a069957d05c9.png)

```python
def mapping(x):
  if x==1:
    return 'HC'
  elif x==2:
    return 'MCI'
  else:
    return 'AD'

df_data = df_av45.iloc[:, 1:5]
df_data['DX'] = df_av45['DX'].apply(mapping)

print ('shape: ', df_data.shape)
df_data.head(10)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/408c8926-7233-bc94-d2c7-95c43d279342.png)

```python
print (df_data['DX'].value_counts())
df_data.describe()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/8abe6eb2-db3f-dff9-6e58-754d474813b2.png)

```python
plt.figure(figsize=(5, 3))
sns.pairplot(data=df_data, hue='DX', diag_kind='kde')
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/da7da380-6f49-2880-8b0d-594e8292ca3c.png)

```python
df_corr = df_data.corr()

plt.figure(figsize=(5, 3))
sns.heatmap(df_corr, annot=True, linewidths=0.1, linecolor='white', )
plt.title('Correlation matrix')
plt.xticks(rotation=45)
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/d8955de6-3136-ba30-2eff-aefb42000ada.png)

```python
x_data = df_data.drop('DX', axis=1).values
y_data = df_data['DX'].values

ms = MinMaxScaler()
x_data_norm = ms.fit_transform(x_data)
```

##Clustering
```python
# Plot the PCA with either 2 or 3 reduced components
def plotPCA(data, nComponents):
  pca = PCA(n_components = nComponents)
  principalComponents = pca.fit_transform(data)
  principalDf = pd.DataFrame(data = principalComponents, columns = ['Principal Component ' + str(i) for i in range(1, nComponents + 1)])
  
  fig = plt.figure(figsize = (15, 15))
  if (nComponents == 3):
    ax = fig.add_subplot(111, projection = '3d')
    ax.set_xlabel('Principal Component 1', fontsize = 15)
    ax.set_ylabel('Principal Component 2', fontsize = 15)
    ax.set_zlabel('Principal Component 3', fontsize = 15)
    ax.set_title('3 component PCA', fontsize = 20)
    ax.scatter(xs = principalDf.iloc[:, 0], ys = principalDf.iloc[:, 1], zs = principalDf.iloc[:, 2])
  else:
    ax = fig.add_subplot(111)
    ax.set_xlabel('Principal Component 1', fontsize = 15)
    ax.set_ylabel('Principal Component 2', fontsize = 15)
    ax.set_title('2 component PCA', fontsize = 20)
    ax.scatter(x = principalDf.iloc[:, 0], y = principalDf.iloc[:, 1])
  plt.show()

plotPCA(x_data_norm, 2) # 2D PCA
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/653fc0c6-4340-fc6f-f92c-f992bd0158d1.png)


```python
plotPCA(x_data_norm, 3) # 3D PCA
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/242a79cb-dbb4-df8a-ca99-516776f6509c.png)

##Classification (Decision Tree)
```python
max_depth = 4

dt = DecisionTreeClassifier(max_depth=max_depth)
dt.fit(x_train, y_train)
```

```python
def print_cmx(y_true, y_pred):
  #labels = sorted(list(set(y_true)))
  labels = ['HC', 'MCI', 'AD']
  cmx_data = confusion_matrix(y_true, y_pred, labels=labels)

  df_cmx = pd.DataFrame(cmx_data, index=labels, columns=labels)

  plt.figure(figsize=(5, 3))
  sns.heatmap(df_cmx, annot=True, fmt = 'd', linewidth=0.1, linecolor='white')
  plt.title('Confusion Matrix')
  plt.xlabel('Predicted label')
  plt.ylabel('True label')
  plt.show()
```

```python
y_train_pred = dt.predict(x_train)

print ('Train')
print ('Accuracy: ', accuracy_score(y_train, y_train_pred))
print_cmx(y_train, y_train_pred)
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/926e7875-658f-4610-d306-af4d6e4d4cc7.png)

```python
feat_labels = df_data.drop('DX', axis=1).columns

importances = dt.feature_importances_

indices = np.argsort(importances)
plt.figure(figsize = (5, 3))
plt.barh(y = range(len(indices)), width = importances[indices])
plt.yticks(range(len(indices)), feat_labels[indices])
plt.title('Feature importances')
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/635be598-ae32-7b90-d986-f50f6b6fc076.png)


```python
y_val_pred = dt.predict(x_val)

print ('Validation')
print ('Accuracy: ', accuracy_score(y_val, y_val_pred))
print_cmx(y_val, y_val_pred)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/f5249e18-c49c-6e6e-b430-3d855e30eb7b.png)


```python
dot_data = export_graphviz(dt, out_file=None, feature_names = feat_labels, 
                        class_names = ['MCI', 'HC', 'AD'])  
graph = graphviz.Source(dot_data)

graph
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/2cc30f99-8e21-b531-621d-82c854c911c9.png)


##Classification (Random Forest)
```python
rf = RandomForestClassifier(random_state=1218)
rf.fit(x_train, y_train)
```