### Install Pomegranate from GitHub Source: https://pomegranate.readthedocs.io/en/latest/_sources/install.rst Installs the bleeding-edge version of pomegranate directly from its GitHub repository. This involves cloning the repository and running the setup script. ```bash git clone https://github.com/jmschrei/pomegranate cd pomegranate python setup.py install ``` -------------------------------- ### Environment Setup and Version Check (Python, PyTorch) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/C_Feature_Tutorial_3_Out_Of_Core_Learning This snippet sets up the Python environment for out-of-core learning examples. It imports necessary libraries like torch and pomegranate, sets random seeds for reproducibility, and displays version information for torch and pomegranate, along with system details. ```python %pylab inline import torch numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p torch,pomegranate ``` -------------------------------- ### Setup and Environment Information Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_2_General_Mixture_Models.ipynb Imports necessary libraries (numpy, matplotlib, seaborn, torch) and loads the pomegranate library. It also displays environment and package versions using %watermark. This setup is crucial for running the subsequent examples. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch from pomegranate.gmm import GeneralMixtureModel from pomegranate.distributions import * numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p numpy,scipy,torch,pomegranate ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_5_Markov_Chains Imports necessary libraries like torch and pomegranate, and sets up the environment for displaying plots and version information. This is a standard setup for running examples. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch %load_ext watermark %watermark -m -n -p torch,pomegranate ``` -------------------------------- ### Install Pomegranate using Pip Source: https://pomegranate.readthedocs.io/en/latest/_sources/install.rst Installs the pomegranate Python package and its dependencies using pip. This is the recommended and simplest installation method. ```bash pip install pomegranate ``` -------------------------------- ### Initialize Pomegranate and Watermark Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/C_Feature_Tutorial_3_Out_Of_Core_Learning.ipynb Initializes the Pomegranate environment and displays version information for PyTorch and Pomegranate. This setup is crucial for reproducible results and understanding the software environment. ```python %pylab inline import torch numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p torch,pomegranate ``` -------------------------------- ### Setup Environment and Load Watermark Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_6_Bayesian_Networks.ipynb This code snippet sets up the Python environment by importing necessary libraries like numpy and matplotlib, and then loads the watermark extension to display package versions. ```python from pomegranate.distributions import Categorical from pomegranate.distributions import ConditionalCategorical from pomegranate.bayesian_network import BayesianNetwork d1 = Categorical([[0.1, 0.9]]) d2 = ConditionalCategorical([[[0.4, 0.6], [0.3, 0.7]]]) model = BayesianNetwork([d1, d2], [(d1, d2)]) ``` -------------------------------- ### Calculate Probability of Data Examples (Python) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_7_Factor_Graphs Calculates the probability of each data example in X given the fitted FactorGraph model. The probability is factorized across factors and marginal distributions. ```python print(model.probability(X[:1])) ``` -------------------------------- ### Import Libraries and Setup for HMMs Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models This snippet imports necessary libraries such as seaborn, torch, and numpy, and sets up the environment for numerical operations and plotting. It also loads the watermark extension to display package versions and system information. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p numpy,scipy,torch,pomegranate ``` -------------------------------- ### Set up environment and display package versions Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models.ipynb This code snippet sets up the environment for analysis by importing necessary libraries like numpy and matplotlib, and then uses the %watermark magic command to display the versions of installed packages including numpy, scipy, torch, and pomegranate. It also configures numpy for better output formatting and sets a random seed for reproducibility. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p numpy,scipy,torch,pomegranate ``` -------------------------------- ### KMeans Model Fitting Timing - Python Source: https://pomegranate.readthedocs.io/en/latest/tutorials/C_Feature_Tutorial_1_GPU_Usage Compares the time required to fit a KMeans model on CPU versus GPU. This example initializes two KMeans models, one for CPU and one for GPU, and times their respective fit methods using a large dataset. ```python from pomegranate.kmeans import KMeans model1 = KMeans(512) model2 = KMeans(512) %timeit -n 1 -r 1 model1.fit(X) %timeit -n 1 -r 1 model2.cuda().fit(X.cuda()) ``` -------------------------------- ### Accessing Model Probabilities (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_5_Markov_Chains.ipynb Provides examples of accessing the probability distributions within a Markov chain model. This allows inspection of the learned transition probabilities between states. The output shows the probability tensors for different distributions within the model. ```python model.distributions[0].probs ``` ```python model.distributions[1].probs[0] ``` ```python model.distributions[2].probs[0] ``` -------------------------------- ### Batched Training for Normal Distribution (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/C_Feature_Tutorial_3_Out_Of_Core_Learning.ipynb This example illustrates batched training for a Normal distribution using Pomegranate's out-of-core API. It iterates through data batches, summarizes each batch, and then reconstructs the distribution from the accumulated summaries, suitable for large datasets. ```python dist = Normal() for i in range(10): X_batch = torch.randn(1000, 20) # Mimics loading a batch of data dist.summarize(X_batch) del X_batch # Discard the batch after summarization dist.from_summaries() ``` -------------------------------- ### Calculate Log Probability of Data Examples (Python) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_7_Factor_Graphs Computes the log probability for each data example in X using the FactorGraph model. This is often preferred for numerical stability compared to direct probability calculations. ```python print(model.log_probability(X)) ``` -------------------------------- ### Makefile for Development (Makefile) Source: https://pomegranate.readthedocs.io/en/latest/_sources/whats_new.rst A top-level Makefile is provided for development convenience, supporting two conda environments (py2.7 and py3.6 by default). It includes targets for installation, testing, cleaning, and notebook testing, with options to override environment names and control error handling during notebook tests. ```makefile # Example usage: # make PY3_ENV=py3.6.2 biginstall # make ALLOW_ERRORS=--allow-errors nbtest install: t# Installation commands test: # Testing commands bigclean: # Clean build commands nbtest: # Jupyter notebook testing commands ``` -------------------------------- ### Initialize and Fit Bayesian Network with Known Structure Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_6_Bayesian_Networks.ipynb Initializes a Bayesian network with a predefined structure and then fits it to the data `X`. This is useful when the network's architecture is known beforehand, and only the parameters need to be learned. The example demonstrates learning the parameters for a network with a single dependency. ```python model3 = BayesianNetwork(structure=[(), (0,)]) model3.fit(X) model3.distributions[1].probs[0] ``` -------------------------------- ### Initialize DenseHMM with Explicit Parameters Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models Creates a DenseHMM by explicitly providing a list of distributions, a dense transition matrix, and optional start and end probabilities. This method is suitable for defining a known HMM structure. ```python from pomegranate.hmm import DenseHMM from pomegranate.distributions import Normal import torch import matplotlib.pyplot as plt # Assuming d1 and d2 are pre-defined distributions, and X is pre-defined data # Example placeholder distributions and data: d1 = Normal(0, 1) d2 = Normal(1, 1) X = torch.randn(100, 10) # Example data model = DenseHMM([d1, d2], edges=[[0.89, 0.1], [0.1, 0.9]], starts=[0.5, 0.5], ends=[0.01, 0.0]) plt.plot(model.predict_proba(X)[0], label=['background', 'CG island']) plt.legend() plt.show() ``` -------------------------------- ### Initialize DenseHMM with Priors in Pomegranate Source: https://pomegranate.readthedocs.io/en/latest/tutorials/C_Feature_Tutorial_4_Priors_and_Semi-supervised_Learning This code snippet demonstrates how to initialize a DenseHMM model in Pomegranate. It defines two Normal distributions and sets up the transition matrix and starting probabilities for the HMM. The priors are also specified as a 3D tensor. ```python from pomegranate.hmm import DenseHMM from pomegranate.distributions import Normal d1 = Normal([1.0], [1.0], covariance_type='diag') d2 = Normal([3.0], [1.0], covariance_type='diag') model = DenseHMM([d1, d2], [[0.7, 0.3], [0.3, 0.7]], starts=[0.4, 0.6]) ``` -------------------------------- ### Import Libraries and Setup for Bayes Classifier Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_3_Bayes_Classifier This snippet imports necessary libraries like torch, seaborn, and components from pomegranate for building and visualizing probabilistic models. It also sets up the environment for displaying plots and watermarking the session with package versions. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch from pomegranate.bayes_classifier import BayesClassifier from pomegranate.distributions import * numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p numpy,scipy,torch,pomegranate ``` -------------------------------- ### Import Libraries and Setup for Mixture Models Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_2_General_Mixture_Models This code block imports necessary libraries such as torch, seaborn, and components from pomegranate for implementing mixture models. It also sets up random seeds and display options for reproducibility and clear output. The watermark extension is used to display environment and package versions. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch from pomegranate.gmm import GeneralMixtureModel from pomegranate.distributions import * numpy.random.seed(0) numpy.set_printoptions(suppress=True) %load_ext watermark %watermark -m -n -p numpy,scipy,torch,pomegranate ``` -------------------------------- ### Fit DenseHMM with Default Iterations Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models Demonstrates fitting a DenseHMM model with default iteration settings. It initializes categorical distributions, transition edges, start and end probabilities, and then fits the model to data `X`. The output shows the iterative improvement in the model's likelihood. ```python d1 = Categorical([[0.25, 0.25, 0.25, 0.25]]) d2 = Categorical([[0.10, 0.40, 0.40, 0.10]]) edges = [[0.89, 0.1], [0.1, 0.9]] starts = [0.5, 0.5] ends = [0.01, 0.0] model = DenseHMM([d1, d2], edges=edges, starts=starts, ends=ends, verbose=True) model.fit(X) ``` -------------------------------- ### Initialize and Fit Model with Data (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/faq.rst Demonstrates how to initialize a model with hyperparameters and then fit it to data using the `fit` function, following the scikit-learn API. Some models may allow blank initialization, while others require specific parameters like distributions for mixture models or order for Markov chains. ```python from pomegranate import NormalDistribution # Example: Initialize a model and fit it to data model = NormalDistribution().fit(X) ``` -------------------------------- ### log_probability() Method Source: https://pomegranate.readthedocs.io/en/latest/api Calculates the log probability of each example given the distribution's parameters. Assumes examples are in a 2D format. ```APIDOC ## Method: log_probability(_X_) ### Description Calculate the log probability of each example. This method calculates the log probability of each example given the parameters of the distribution. The examples must be given in a 2D format. For a Bernoulli distribution, each entry in the data must be either 0 or 1. Note: This differs from some other log probability calculation functions, like those in torch.distributions, because it is not returning the log probability of each feature independently, but rather the total log probability of the entire example. ### Method (Implicitly called by fit, no direct HTTP method) ### Endpoint (N/A) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **X** (list, tuple, numpy.ndarray, torch.Tensor) - Required - A set of examples to evaluate. Shape should be (-1, self.d). ### Request Example ```json { "X": [[0.1, 0.2], [0.3, 0.4]] } ``` ### Response #### Success Response (200) * **logp** (torch.Tensor) - The log probability of each example. Shape is (-1,). #### Response Example ```json { "logp": [-1.23, -0.45] } ``` ``` -------------------------------- ### Initialize a Markov Chain Model Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_5_Markov_Chains Demonstrates how to initialize a MarkovChain model. You can either pass pre-fit distributions or specify the 'k' parameter to let pomegranate automatically construct the necessary distributions. ```python from pomegranate.markov_chain import MarkovChain model = MarkovChain(k=3) ``` -------------------------------- ### log_probability(_X_) Source: https://pomegranate.readthedocs.io/en/latest/api Calculate the log probability of each example given the parameters of the distribution. The examples must be given in a 2D format. ```APIDOC ## POST /log_probability ### Description Calculate the log probability of each example given the parameters of the distribution. The examples must be given in a 2D format. For a gamma distribution, the data must be non-negative. Note: This differs from some other log probability calculation functions, like those in torch.distributions, because it is not returning the log probability of each feature independently, but rather the total log probability of the entire example. ### Method POST ### Endpoint /log_probability ### Parameters #### Request Body - **X** (list, tuple, numpy.ndarray, torch.Tensor) - Required - A set of examples to evaluate. Shape: (-1, self.d) ### Request Example ```json { "X": "[[1.0, 2.0], [3.0, 4.0]]" } ``` ### Response #### Success Response (200) - **logp** (torch.Tensor) - The log probability of each example. Shape: (-1,) #### Response Example ```json { "logp": "[-1.23, -4.56]" } ``` ``` -------------------------------- ### Categorical.log_probability() Source: https://pomegranate.readthedocs.io/en/latest/api Calculates the log probability of each example. The examples must be given in a 2D format. For a categorical distribution, each entry in the data must be an integer in the range [0, n_keys). ```APIDOC ## POST /Categorical/log_probability ### Description Calculate the log probability of each example. The examples must be given in a 2D format. For a categorical distribution, each entry in the data must be an integer in the range [0, n_keys). Note: This differs from some other log probability calculation functions, like those in torch.distributions, because it is not returning the log probability of each feature independently, but rather the total log probability of the entire example. ### Method POST ### Endpoint /Categorical/log_probability ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **X** (list, tuple, numpy.ndarray, torch.Tensor) - Required - A set of examples to evaluate. Shape should be (-1, self.d). ### Request Example ```json { "X": [[0, 1], [1, 0]] } ``` ### Response #### Success Response (200) - **logp** (torch.Tensor) - The log probability of each example. Shape is (-1,). #### Response Example ```json { "logp": [-0.5, -0.7] } ``` ``` -------------------------------- ### Initialize Bayesian Network with Distributions and Edges Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_6_Bayesian_Networks Demonstrates initializing a Bayesian Network by providing a list of distributions and a list of edges representing dependencies. This method directly defines the network's structure and components. ```python from pomegranate.distributions import Categorical from pomegranate.distributions import ConditionalCategorical from pomegranate.bayesian_network import BayesianNetwork d1 = Categorical([[0.1, 0.9]]) d2 = ConditionalCategorical([[[0.4, 0.6], [0.3, 0.7]]]) model = BayesianNetwork([d1, d2], [(d1, d2)]) ``` -------------------------------- ### Calculate Probability of Examples Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_6_Bayesian_Networks.ipynb Computes the probability of each example in the input data `X` according to the learned Bayesian network model. This leverages the factorization of the joint probability distribution along the graph structure. ```python model.probability(X) ``` -------------------------------- ### Initialize GeneralMixtureModel with Distributions and Priors Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_2_General_Mixture_Models.ipynb This code demonstrates how to initialize a GeneralMixtureModel with pre-defined probability distributions (Exponential in this case) and their corresponding prior probabilities. This is useful when you have prior knowledge about the distributions and their weights. ```python d1 = Exponential([1.3, 3.3]) d2 = Exponential([2.2, 0.4]) model = GeneralMixtureModel([d1, d2], priors=[0.2, 0.8]) ``` -------------------------------- ### Define HMM Transition Probabilities and Start Probabilities (Python) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models Sets the transition probabilities between hidden states and the initial probabilities for starting in each state. This defines the HMM's structure and how it moves between states based on observations. ```python model.add_edge(model.start, d1, 0.5) model.add_edge(model.start, d2, 0.5) model.add_edge(d1, d1, 0.9) model.add_edge(d1, d2, 0.1) model.add_edge(d2, d1, 0.1) model.add_edge(d2, d2, 0.9) ``` -------------------------------- ### Calculate Log Probability for Bernoulli Distribution - Pomegranate Source: https://pomegranate.readthedocs.io/en/latest/api Calculates the log probability of each example given the Bernoulli distribution's parameters. Input data `X` must be 2D, with each entry being 0 or 1. This method returns the total log probability for each example, not feature-wise. ```python import torch from pomegranate import Bernoulli probs = [0.7, 0.3] bernoulli_dist = Bernoulli(probs=probs) X = torch.tensor([[1, 0], [0, 1], [1, 1]]) log_probs = bernoulli_dist.log_probability(X) print(log_probs) ``` -------------------------------- ### Out-of-Core Learning with Summarize and FromSummaries (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/faq.rst Illustrates the process of out-of-core learning in pomegranate. This involves using the `summarize` method on data chunks to obtain sufficient statistics, which are then added and used to update model parameters via `from_summaries`. ```python from pomegranate import NormalDistribution # Assume model is initialized model = NormalDistribution() # Process chunks of data summaries = [] for chunk in data_chunks: summaries.append(model.summarize(chunk)) # Add summaries and update model parameters combined_summaries = add_summaries(summaries) # Function to add summaries model.from_summaries(combined_summaries) ``` -------------------------------- ### Sample from Markov Models (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_5_Markov_Chains.ipynb Illustrates how to generate samples from a trained Markov chain model. This is useful for simulating future sequences or understanding the model's generative capabilities. The sampling process can produce a large number of samples for analysis. Dependencies include PyTorch. ```python X_sample = model.sample(100000).type(torch.float32) ``` -------------------------------- ### Calculate Log Probability (Pomegranate Normal) Source: https://pomegranate.readthedocs.io/en/latest/api Computes the log probability of each example given the Normal distribution's parameters. Requires input data in a 2D format. ```python import numpy as np from pomegranate.distributions import Normal dist = Normal(means=[0], covs=[[1]]) X = np.array([[1.0], [-1.0]]) log_probs = dist.log_probability(X) print(log_probs) ``` -------------------------------- ### Initialize Bayesian Network Programmatically Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_6_Bayesian_Networks Shows an alternative way to create a Bayesian Network by first initializing an empty network and then adding distributions and edges using dedicated methods. This offers more flexibility in building the network step-by-step. ```python from pomegranate.distributions import Categorical from pomegranate.distributions import ConditionalCategorical from pomegranate.bayesian_network import BayesianNetwork d1 = Categorical([[0.1, 0.9]]) d2 = ConditionalCategorical([[[0.4, 0.6], [0.3, 0.7]]]) model2 = BayesianNetwork() model2.add_distributions([d1, d2]) model2.add_edge(d1, d2) ``` -------------------------------- ### Generate Random Data (Python, PyTorch) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/C_Feature_Tutorial_3_Out_Of_Core_Learning Generates a tensor of random numbers using PyTorch for use in out-of-core learning examples. The data is a 1000x5 tensor, suitable for training a multivariate distribution. ```python X = torch.randn(1000, 5) ``` -------------------------------- ### Initialize Bayesian Network Programmatically Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_6_Bayesian_Networks.ipynb Demonstrates an alternative method to create a Bayesian Network by first adding distributions and then defining edges between them using `add_distributions` and `add_edge` methods. ```python model2 = BayesianNetwork() model2.add_distributions([d1, d2]) model2.add_edge(d1, d2) ``` -------------------------------- ### Rebuildconda Script for Conda Environments (Shell) Source: https://pomegranate.readthedocs.io/en/latest/_sources/whats_new.rst A convenience script `rebuildconda` is available for development to remove and recreate conda environments. It defaults to rebuilding the 'py2.7' environment but can be configured for other environments. This is useful for testing different environment setups. ```bash # Example usage: # ./rebuildconda 2.7.9 ; make PY2_ENV=py2.7.9 bigclean py2build py2test py2install nbtest ; source deactivate ; conda env remove --name py2.7.9 # Default rebuilds 'py2.7' environment ./rebuildconda ``` -------------------------------- ### Summarize Data for Normal Distribution (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/C_Feature_Tutorial_3_Out_Of_Core_Learning.ipynb This code snippet demonstrates how to initialize a Normal distribution, summarize data (X) from it, and then reconstruct the distribution parameters from these summaries. It's useful for understanding the internal state of the distribution after processing data. ```python dist = Normal() dist.summarize(X) dist.from_summaries() dist.means, dist.covs ``` -------------------------------- ### Predict Most Likely Value - Python Source: https://pomegranate.readthedocs.io/en/latest/_sources/api.rst Returns the most likely inferred value for each example in the data. For Bayesian networks with incomplete data, it infers the most likely variable values. For other models, it identifies the most likely component explaining the data. ```python >>> model.predict(X) ``` -------------------------------- ### Add return_history to fit/from_samples (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/whats_new.rst Introduces the `return_history` parameter to the `fit` and `from_samples` methods in various Pomegranate models. This parameter allows the methods to return the history callback along with the fitted model, enhancing the tracking of model fitting progress. ```python model.fit(X, return_history=True) model.from_samples(X, return_history=True) ``` -------------------------------- ### Calculate Log Probability of Data - Python Source: https://pomegranate.readthedocs.io/en/latest/_sources/api.rst Calculates the log probability of a given set of examples using a trained model. This method is numerically more stable than `probability` and is used internally for its calculation. It accepts 2D or 3D data. ```python >>> model.log_probability(X) ``` -------------------------------- ### Calculate Probability of Data - Python Source: https://pomegranate.readthedocs.io/en/latest/_sources/api.rst Calculates the probability of a given set of examples using a trained model. This method takes 2D or 3D data and returns a vector of probabilities. It is derived from log probabilities for numerical stability. ```python >>> model.probability(X) ``` -------------------------------- ### Initialize and Fit Naive Bayes Classifier Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_3_Bayes_Classifier.ipynb Demonstrates how to initialize and fit a Naive Bayes classifier in Pomegranate. It shows two methods: providing pre-learned distributions or providing uninitialized distributions that will be fitted to the data. ```python # Initialize with learned distributions clf = BayesClassifier.from_samples(Normal, X, y) # Initialize with uninitialized distributions clf = BayesClassifier(n_components=2) clf.fit(X, y, n_iter=10, tol=0.01) ``` -------------------------------- ### Format Date in JavaScript Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_2_General_Mixture_Models.ipynb Formats a Date object into a human-readable string. This example shows a basic format, but can be extended to include various date and time components. It utilizes the built-in `Date` object methods. ```javascript function formatDate(date) { const options = { year: 'numeric', month: 'long', day: 'numeric' }; return date.toLocaleDateString(undefined, options); } ``` -------------------------------- ### Summarize and Reconstruct Markov Chain Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_5_Markov_Chains Explains how to use the 'summarize' and 'from_summaries' methods for Markov chains. This requires data to be three-dimensional and of consistent length within each summarization call. ```python X = torch.randint(2, size=(30, 8, 1)) model.summarize(X[:10]) model.summarize(X[10:]) model.from_summaries() ``` -------------------------------- ### Initialize a Simple Factor Graph with Categorical Distributions Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_7_Factor_Graphs.ipynb This Python code demonstrates the initialization of a basic factor graph using pomegranate. It sets up two marginal categorical distributions and prepares for their integration into a factor graph structure. This is a foundational step for building more complex factor graph models. ```python from pomegranate.distributions import Categorical from pomegranate.distributions import JointCategorical from pomegranate.factor_graph import FactorGraph m1 = Categorical([[0.5, 0.5]]) m2 = Categorical([[0.5, 0.5]]) ``` -------------------------------- ### Manual Log Probability Calculation Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_6_Bayesian_Networks.ipynb Manually calculates the log probability of examples by summing the log probabilities of individual variables based on their parents, as defined by the model's structure and distributions. This serves as a verification method for the `log_probability` method. ```python model.distributions[0].log_probability(X[:,:1]) + model.distributions[1].log_probability(X[:, :, None]) ``` -------------------------------- ### Import Libraries and Check Versions Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_5_Markov_Chains.ipynb Imports necessary libraries like pylab, seaborn, and torch, and loads the watermark extension to display package versions. This is useful for reproducibility and debugging. ```python %pylab inline import seaborn; seaborn.set_style('whitegrid') import torch %load_ext watermark %watermark -m -n -p torch,pomegranate ``` -------------------------------- ### DenseHMM Initialization Parameters Source: https://pomegranate.readthedocs.io/en/latest/api This section details the parameters used for initializing the DenseHMM class. It covers distributions, prior probabilities, maximum iterations, convergence tolerance, inertia for parameter updates, freezing parameters, data checking, and verbosity during training. ```python class DenseHMM( _distributions=None, _edges=None, _starts=None, _ends=None, _init='random', _max_iter=1000, _tol=0.1, _sample_length=None, _return_sample_paths=False, _inertia=0.0, _frozen=False, _check_data=True, _random_state=None, _verbose=False ) ``` -------------------------------- ### Initialize Pomegranate Distributions with Parameters (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_1_Distributions.ipynb Demonstrates initializing various pomegranate probability distributions (Normal, Exponential, Categorical) with predefined parameters. It shows that parameters can be provided as lists, tuples, NumPy arrays, or PyTorch tensors, and highlights the 'diag' covariance type for Normal distributions. ```python d1 = Normal([0.3, 0.7, 1.1], [1.1, 0.3, 1.8], covariance_type='diag') d2 = Exponential([0.8, 1.4, 4.1]) d3 = Categorical([[0.3, 0.2, 0.5], [0.2, 0.1, 0.7]]) d11 = Normal((0.3, 0.7, 1.1), (1.1, 0.3, 1.8), covariance_type='diag') d12 = Normal(numpy.array([0.3, 0.7, 1.1]), numpy.array([1.1, 0.3, 1.8]), covariance_type='diag') d13 = Normal(torch.tensor([0.3, 0.7, 1.1]), torch.tensor([1.1, 0.3, 1.8]), covariance_type='diag') ``` -------------------------------- ### Initialize SparseHMM with Edges and Distributions (Python) Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models.ipynb This snippet demonstrates initializing a SparseHMM model by directly providing distributions and a list of edges. The edges define the transitions between states, including their probabilities. This method is suitable for defining HMMs with known transition structures. ```python from pomegranate.hmm import SparseHMM edges = [ [d1, d1, 0.89], [d1, d2, 0.1], [d2, d1, 0.1], [d2, d2, 0.9] ] model = SparseHMM([d1, d2], edges=edges, starts=[0.5, 0.5], ends=[0.01, 0.0]) plt.plot(model.predict_proba(X)[0], label=['background', 'CG island']) plt.legend() plt.show() ``` -------------------------------- ### Initialize DenseHMM Distributions with K-Means Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_4_Hidden_Markov_Models Creates a DenseHMM with uninitialized distributions and then initializes them using k-means clustering when provided with data via the `_initialize` method. Transition edges can be initialized to uniform probabilities if not provided. ```python from pomegranate.hmm import DenseHMM from pomegranate.distributions import Normal import torch X3 = torch.randn(100, 50, 2) # Distributions will be initialized using k-means model3 = DenseHMM([Normal(), Normal(), Normal()], verbose=True) model3._initialize(X3) # Accessing the means of the initialized distributions print(model3.distributions[0].means, model3.distributions[1].means, model3.distributions[2].means) ``` -------------------------------- ### Summarize and Update Pomegranate Model Parameters Source: https://pomegranate.readthedocs.io/en/latest/_sources/tutorials/B_Model_Tutorial_7_Factor_Graphs.ipynb This example illustrates the summarization API in Pomegranate for training. It shows how to summarize batches of data using `summarize` and then update the model's factor parameters using `from_summaries`. This process only affects factor parameters, not marginal distributions. ```python model.summarize(X[:3]) model.from_summaries() ``` -------------------------------- ### Initialize Mixture Model with Existing Distributions (Python) Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_2_General_Mixture_Models Initializes a GeneralMixtureModel using pre-defined probability distributions and prior probabilities. This is useful when you have existing distribution objects and their associated prior weights. ```python from pomegranate import GeneralMixtureModel, Exponential d1 = Exponential([1.3, 3.3]) d2 = Exponential([2.2, 0.4]) model = GeneralMixtureModel([d1, d2], priors=[0.2, 0.8]) ``` -------------------------------- ### Recreate Model Parameters from Summaries Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_1_Distributions Demonstrates the `from_summaries` method for Pomegranate `Normal` distribution objects. It shows how to recreate the model's parameters (means) from the summarized statistics, confirming that batch summarization and full summarization yield the same results. ```python d.from_summaries() d2.from_summaries() d.means, d2.means ``` -------------------------------- ### SparseHMM Initialization Parameters Source: https://pomegranate.readthedocs.io/en/latest/api Defines the parameters for initializing a SparseHMM model. These include distributions, transition matrix (edges), start and end probabilities, inertia for parameter updates, and flags for freezing parameters and checking data. The `_class_pomegranate.hmm.SparseHMM` constructor accepts these arguments to configure the HMM. ```python class _pomegranate.hmm.SparseHMM(_distributions=None, _edges=None, _starts=None, _ends=None, _init='random', _max_iter=1000, _tol=0.1, _sample_length=None, _return_sample_paths=False, _inertia=0.0, _frozen=False, _check_data=True, _random_state=None, _verbose=False) ``` -------------------------------- ### Predict Probabilities with Mixture Model Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_2_General_Mixture_Models This snippet shows how to get the probability of each data point belonging to each component in the mixture model using the `predict_proba` method. This returns the responsibility matrix, which are the likelihoods multiplied by priors and normalized. It requires a fitted model and input data `X`. ```python model.predict_proba(X)[::15] ``` -------------------------------- ### Inspect Markov Chain Distributions Source: https://pomegranate.readthedocs.io/en/latest/tutorials/B_Model_Tutorial_5_Markov_Chains Illustrates how to inspect the fitted distributions within the MarkovChain model. This allows verification of the learned probabilities for different states and transitions. ```python print(model.distributions[0].probs[0]) print(model.distributions[1].probs[0]) print(model.distributions[2].probs[0]) ```