{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fit a Sparse Tensor Decomposition Model\n",
"\n",
"This notebook demonstrates how to fit a sparse tensor decomposition model to data, using the `SparseCP` class of the `Barnacle` library. First we'll generate a simulated data tensor with added noise, then fit a sparse tensor decomposition model to this simulation. We'll evaluate the result using tools from Barnacle and the [tlviz](http://tensorly.org/viz/stable/index.html) library to compare factor matrices between the model and the simulation ground truth. Finally we'll explore how multiple random initializations is good practice when fitting CP tensor decomposition models, and can result in improved model solutions."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import numpy as np\n",
"import scipy\n",
"import tensorly as tl\n",
"import tlviz\n",
"from barnacle import (\n",
" SparseCP, \n",
" visualize_3d_tensor, \n",
" simulated_sparse_tensor, \n",
" plot_factors_heatmap\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "axis0=%{x}
axis1=%{y}
axis2=%{z}
abs_exp=%{marker.size}
abundance=%{marker.color}