from numpy import False_
import pandas as pd
from sklearn.preprocessing import StandardScaler

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

url = "CSV/N_DataSetCocaína.csv"
fields = ['zona', 'periodo', 'cocaina', 'hoja de coca',
          'pbc']
#   zona,periodo,cocaina,hoja de coca,pbc

df = pd.read_csv(url, skipinitialspace=True, usecols=fields)

features = ['cocaina', 'hoja de coca', 'pbc']

x = df.loc[:, features].values

y = df.loc[:, ['zona']].values

x = StandardScaler().fit_transform(x)

print(df)
print(x)

pca = PCA(n_components=2)

principalComponents = pca.fit_transform(x)

principalDF = pd.DataFrame(data=principalComponents, columns=[
                           'component1', 'component2'])
print(principalDF)

# unir con RegionNatural
finalDF = pd.concat([principalDF, df[['zona']]], axis=1)
print(finalDF)
finalDF.to_csv('CSV/PAC/CocainaConPAC2.csv')

# Visualize 2D Projection
#plt.scatter(principalDF.component1, principalDF.component2)
# plt.show()

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Principal Component 1', fontsize=13)
ax.set_ylabel('Princiapl Component 2', fontsize=13)
ax.set_title('2 component PCA', fontsize=15)

targets = ['Corredor Amazonico', 'Huallaga', 'VRAEM', 'Promedio Nacional']

colors = []

for i in range(len(targets)):
    colors.append('#%06X' % randint(0, 0xFFFFFF))
for target, color in zip(targets, colors):
    indicesToKeep = finalDF['zona'] == target
    ax.scatter(finalDF.loc[indicesToKeep, 'component1'],
               finalDF.loc[indicesToKeep, 'component2'],
               c=color,
               s=50)
ax.legend(targets)
ax.grid()
plt.show()
