library(reticulate)
#py_install("matplotlib")
#LOAD NECESSARY LIBRARIES
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn import datasets
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#LOAD AND VIEW IRIS DATASET
iris = datasets.load_iris()
df = pd.DataFrame(data = np.c_[iris['data'], iris['target']],
                 columns = iris['feature_names'] + ['target'])
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df.columns = ['s_length', 's_width', 'p_length', 'p_width', 'target', 'species']
print(df.head())
##    s_length  s_width  p_length  p_width  target species
## 0       5.1      3.5       1.4      0.2     0.0  setosa
## 1       4.9      3.0       1.4      0.2     0.0  setosa
## 2       4.7      3.2       1.3      0.2     0.0  setosa
## 3       4.6      3.1       1.5      0.2     0.0  setosa
## 4       5.0      3.6       1.4      0.2     0.0  setosa
len(df.index)
## 150
#DEFINE PREDICTOR AND RESPONSE VARIABLES
X = df[['s_length', 's_width', 'p_length', 'p_width']]
y = df['species']
#FIT LDA MODEL
model = LinearDiscriminantAnalysis()
model.fit(X, y)
## LinearDiscriminantAnalysis()
#DEFINE METHOD TO EVALUATE MODEL
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
cv
## RepeatedStratifiedKFold(n_repeats=3, n_splits=10, random_state=1)
#EVALUATE MODEL
#scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
#print(np.mean(scores))
#USE MODEL TO MAKE PREDICTION ON NEW OBSERVATION
new = [5.0, 3.0, 1.0, 0.4]
model.predict([new])
## array(['setosa'], dtype='<U10')
#CREATE LDA PLOT
X = iris.data
y = iris.target
model = LinearDiscriminantAnalysis()
X_r2 = model.fit(X, y).transform(X)
target_names = iris.target_names
plt.figure()
colors = ['red', 'green', 'blue']
lw = 2
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=0.8, color=color,
                label=target_name)
## <matplotlib.collections.PathCollection object at 0x0000000030C80FA0>
## <matplotlib.collections.PathCollection object at 0x0000000030C98430>
## <matplotlib.collections.PathCollection object at 0x0000000030C98880>
plt.legend(loc='best', shadow=False, scatterpoints=1)
## <matplotlib.legend.Legend object at 0x0000000030C98BE0>
plt.show()