%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.cluster import KMeans
import seaborn as sns
import numpy as np

df = pd.read_table('data/old-faithful.txt')
df.head()

df.plot.scatter('eruption', 'waiting')

y_pred = KMeans(n_clusters=2, random_state=0).fit_predict(df)

plt.scatter(df[[0]], df[[1]], c=y_pred)

"""
Explanation: Problem  1 [Testing  Faith]
Download    the “old     faithful”  data set    from    blackboard. This    contains samples    of  a   2-D random  variable: 
the  first  dimension   is  the duration    of  the old  faithful   geyser  eruptions.  The second  is  the waiting  time 
between eruptions. Generate a   2-D scatter plot    of  the data.   Run a   k-means clustering  routine on  the data  
for k=2.    Show    the two clusters    in  a   scatterplot
End of explanation
"""


def mixture_model(mu1,mu2,s1,s2,alpha):
    return alpha*np.random.normal(mu1, s1, 1000) + (1-alpha)*np.random.normal(mu2, s2, 1000)


mixture_samples = mixture_model(-1,1,1,1,0.4)
plt.scatter(range(1000), mixture_samples)

plt.hist(mixture_samples, bins=20)

y_pred = KMeans(n_clusters=2, random_state=0).fit_predict(mixture_samples.reshape(-1,1))

plt.scatter(range(1000), mixture_samples, c=y_pred)

"""
Explanation: k-means finds the two well separated clusters in this case
Problem  2   [Generating Mixed   Samples]
Implement   a   random  number  generator   for a   random  variable with   the following   mixture distribution:
$f(x) = 0.4N(-1,1)  + 0.6N(1,1)$
Generate    N=1000  samples and histogram   them.   Try out a   k-means clustering  routine (k=2)   on  the data.
End of explanation
"""


