<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Automatically selecting the number of clusters in a latent variable
This is just a quick demo to show how to automatically decide upon the number of clusters in a latent variable, where the number of clusters are unknown, using the Iris dataset.
The process is iterative, building and training the model multiple times, and then querying the trained model to extract the log likelihood. There are lots of other scoring functions; regression or classification accuracy, Bayesian Information Criterion (BIC; which penalises the complexity of the model against the accuracy) among others. One thing to watch is that the log likelihood cannot be used as a measure where the number of variables in the model is being adjusted at the same time, as the score will also change (however it's perfect when only changing the number of states).
It's easy to craft the iterative code, but there is a utility function in analysis.py to do it automatically and which uses cross validation.
Step1:  And finally plot the results
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import logging
import sys
sys.path.append("../../../bayesianpy")
import bayesianpy
import matplotlib.pyplot as plt
import os
logger = logging.getLogger()
bayesianpy.jni.attach(logger)
db_folder = bayesianpy.utils.get_path_to_parent_dir('')    
iris = pd.read_csv(os.path.join(db_folder, "data/iris.csv"), index_col=False)
analysis = bayesianpy.analysis.LogLikelihoodAnalysis(logger)
# create templates between with latent states from 1 -> 19
results = analysis.analyse(iris, [bayesianpy.template.MixtureNaiveBayes(logger,
                                  discrete=iris[['iris_class']],
                                  continuous=iris[['sepal_length', 'petal_width', 'petal_length', 'sepal_width']],
                                  latent_states=i) for i in range(1, 20)],
                           use_model_names=False,
                           names=list(range(1,20)))
Explanation: Automatically selecting the number of clusters in a latent variable
This is just a quick demo to show how to automatically decide upon the number of clusters in a latent variable, where the number of clusters are unknown, using the Iris dataset.
The process is iterative, building and training the model multiple times, and then querying the trained model to extract the log likelihood. There are lots of other scoring functions; regression or classification accuracy, Bayesian Information Criterion (BIC; which penalises the complexity of the model against the accuracy) among others. One thing to watch is that the log likelihood cannot be used as a measure where the number of variables in the model is being adjusted at the same time, as the score will also change (however it's perfect when only changing the number of states).
It's easy to craft the iterative code, but there is a utility function in analysis.py to do it automatically and which uses cross validation.
End of explanation
%matplotlib inline
plt.figure()
plt.plot(results.columns.tolist(), results.mean().tolist(), 'bo')
plt.show()
Explanation: And finally plot the results:
End of explanation
<END_TASK>