### Apply Path Analysis Tools Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/quick_start.ipynb Provides examples of visualizing user behavior through transition graphs, step matrices, and Sankey diagrams. ```python stream.transition_graph() stream.step_matrix( max_steps=16, threshold=0.2, centered={'event': 'cart', 'left_gap': 5, 'occurrence': 1}, targets=['payment_done'] ) stream.step_sankey(max_steps=6, threshold=0.05) ``` -------------------------------- ### Install and Import Retentioneering Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/stattests.ipynb Initializes the environment by installing the retentioneering package and importing necessary libraries. ```python !pip install retentioneering import retentioneering import pandas as pd ``` -------------------------------- ### Load Sample Data and Initialize TransitionGraph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb Imports necessary libraries, loads a simple shop dataset, and initializes the TransitionGraph object from the loaded data stream. This is the starting point for graph visualization. ```python import retentioneering import pandas as pd from retentioneering import datasets stream = datasets.load_simple_shop() ``` -------------------------------- ### Install Retentioneering Library Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/step_matrix.ipynb Installs the retentioneering library using pip. This is a prerequisite for using the other functionalities in this guide. ```python !pip install retentioneering ``` -------------------------------- ### Preprocess Event Streams Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/quick_start.ipynb Demonstrates splitting sessions based on a timeout and filtering events using a lambda function to refine the dataset. ```python stream \ .split_sessions(timeout=(30, 'm')) \ .filter_events(func=lambda df_, schema: df_['session_id'].str.endswith('_1')) \ .to_dataframe() \ .head() ``` -------------------------------- ### Load and prepare data for clustering Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/clusters.rst Initializes the dataset using the simple_shop example and applies session splitting to prepare the stream for cluster analysis. ```python import numpy as np import pandas as pd from retentioneering import datasets stream = datasets.load_simple_shop()\ .split_sessions(timeout=(30, 'm')) ``` -------------------------------- ### Load and Inspect Data Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/quick_start.ipynb Loads a sample user behavior dataset and displays the first few rows of the underlying dataframe. ```python from retentioneering import datasets # load sample user behavior data: stream = datasets.load_simple_shop() stream.to_dataframe().head() ``` -------------------------------- ### Load Sample Eventstream Data with Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/quick_start.rst Loads the sample 'simple_shop' dataset provided by the Retentioneering library. This dataset contains user behavior events and is used for demonstration purposes. ```python from retentioneering import datasets # load sample user behavior data: stream = datasets.load_simple_shop() ``` -------------------------------- ### Perform Clustering and Advanced Analytics Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/quick_start.ipynb Shows how to extract features, perform K-Means clustering, and analyze funnels, cohorts, and sequences. ```python from retentioneering.tooling.clusters import Clusters clusters = Clusters(stream) features = clusters.extract_features(feature_type='tfidf', ngram_range=(1, 2)) clusters.fit(method='kmeans', n_clusters=8, X=features) clusters.plot(targets=['payment_done', 'cart']) stream.funnel(stages=['catalog', 'cart', 'payment_done']) stream.cohorts(cohort_start_unit='M', cohort_period=(1, 'M'), average=False) stream.sequences(ngram_range=(2, 3), threshold=['count', 1200], sample_size=3) ``` -------------------------------- ### Install Retentioneering via Pip (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/installation.rst Installs the retentioneering library using pip. This command can be run from the command line or within a Python script. ```python pip install retentioneering ``` -------------------------------- ### Generate transition graph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/quick_start.rst Visualizes user paths as a Markov random walk model using the transition_graph method. This tool provides an interactive interface for exploring event-to-event transitions. ```python stream.transition_graph() ``` -------------------------------- ### Initialize and Order Eventstream Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Demonstrates creating an Eventstream from a pandas DataFrame and applying a custom event order using the events_order parameter. ```python import pandas as pd from retentioneering import Eventstream df4 = pd.DataFrame( [ ['user_1', 'A', '2023-01-01 00:00:00'], ['user_1', 'B', '2023-01-01 00:00:00'], ['user_2', 'B', '2023-01-01 00:00:03'], ['user_2', 'A', '2023-01-01 00:00:03'], ['user_2', 'A', '2023-01-01 00:00:04'] ], columns=['user_id', 'event', 'timestamp'] ) stream4 = Eventstream(df4) stream4.to_dataframe() # Reordering events Eventstream(df4, events_order=["B", "A"]).to_dataframe() ``` -------------------------------- ### Load Sample Dataset Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/funnel.ipynb Loads the built-in simple_shop dataset provided by the retentioneering library for analysis purposes. ```python from retentioneering import datasets stream = datasets.load_simple_shop() ``` -------------------------------- ### Install retentioneering Library Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/sequences.ipynb Installs the retentioneering library using pip. This is a prerequisite for using the library's functionalities. ```python !pip install retentioneering ``` -------------------------------- ### Configure Cohort Analysis with Monthly Start and Period Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/cohorts.rst Performs cohort analysis using a monthly start unit ('M') and a monthly cohort period of 1 month (1, 'M'). This sets the granularity for cohort grouping and analysis. Requires a loaded eventstream. ```python stream.cohorts( cohort_start_unit='M', cohort_period=(1, 'M') ) ``` -------------------------------- ### Initialize Eventstream with sample data Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb Imports necessary libraries and loads a sample shop dataset into an Eventstream object. This serves as the foundation for performing data processing tasks. ```python import pandas as pd from retentioneering import datasets from retentioneering.eventstream import Eventstream stream = datasets.load_simple_shop() ``` -------------------------------- ### Import and Export Transition Graph Layout Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb Demonstrates how to dump the current layout parameters of a transition graph to a JSON file and how to load a layout from a specified path. This is useful for saving and reusing graph visualizations. ```python path_link = '/path/to/node_params.json' stream.transition_graph(layout_dump=path_link) ``` -------------------------------- ### Generate Basic Transition Graph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb Generates a basic transition graph from the event stream data. This provides an initial visualization of user flow without any specific customizations. ```python stream.transition_graph(); ``` -------------------------------- ### Label new users with LabelNewUsers Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst Demonstrates how to label specific users as 'new_user' within a stream. It accepts a list of user IDs or 'all' to mark all users as new, returning a dataframe with the synthetic event applied. ```python new_users = [219483890, 964964743, 965024600] res = stream.label_new_users(new_users_list=new_users).to_dataframe() res[res['user_id'] == 219483890].head() ``` -------------------------------- ### Inject and Reindex Events Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Shows how to add positive target events to an existing stream and perform a reindex operation based on a custom sorting list. ```python add_events_stream = stream4.add_positive_events(targets=['B']) custom_sorting = [ 'profile', 'path_start', 'new_user', 'existing_user', 'cropped_left', 'session_start', 'session_start_cropped', 'group_alias', 'positive_target', 'raw', 'raw_sleep', None, 'synthetic', 'synthetic_sleep', 'negative_target', 'session_end_cropped', 'session_end', 'session_sleep', 'cropped_right', 'absent_user', 'lost_user', 'path_end' ] add_events_stream.index_order = custom_sorting add_events_stream.index_events() add_events_stream.to_dataframe() ``` -------------------------------- ### Configure Cohort Analysis with Weekly Start and Period Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/cohorts.rst Performs cohort analysis using a weekly start unit ('W') and a weekly cohort period of 3 weeks (3, 'W'). This provides a more detailed view by analyzing cohorts over a 3-week interval. Requires a loaded eventstream. ```python stream.cohorts( cohort_start_unit='W', cohort_period=(3, 'W') ) ``` -------------------------------- ### Load Dataset and Define User Groups Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/stattests.ipynb Loads a sample dataset and partitions unique users into two distinct groups for comparative analysis. ```python import numpy as np from retentioneering import datasets stream = datasets.load_simple_shop() data = stream.to_dataframe() users = data['user_id'].unique() index_separator = int(users.shape[0]/2) user_groups = users[:index_separator], users[index_separator:] ``` -------------------------------- ### Load Sampled Data with Eventstream Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Loads the 'simple_shop' dataset and creates an Eventstream object, sampling 10% of users with a fixed seed for reproducibility. It then prints the original and sampled number of events to show the effect of sampling. ```python from retentioneering import datasets from retentioneering.eventstream import Eventstream simple_shop_df = datasets.load_simple_shop(as_dataframe=True) sampled_stream = Eventstream( simple_shop_df, user_sample_size=0.1, user_sample_seed=42 ) print('Original number of the events:', len(simple_shop_df)) print('Sampled number of the events:', len(sampled_stream.to_dataframe())) ``` -------------------------------- ### Label users as new or existing Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb Uses the label_new_users method to categorize users based on a provided list of IDs. This allows for filtering and analyzing behavioral differences between new and returning user cohorts. ```python new_users = [219483890, 964964743, 965024600] res = stream.label_new_users(new_users_list=new_users).to_dataframe() res[res['user_id'] == 219483890].head() # Check existing user res[res['user_id'] == 501098384].head() ``` -------------------------------- ### Sample BigQuery data for analysis Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/quick_start.rst SQL snippet to filter BigQuery event data to a 10% sample of users using a hash of the visitor ID. This helps manage large datasets during the initial data extraction phase. ```sql and ABS(MOD(FARM_FINGERPRINT(fullVisitorId), 10)) = 0) ``` -------------------------------- ### Analyze Event Statistics with Retentioneering Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Provides methods to generate descriptive statistics for user events. The describe_events method calculates time-to-first-occurrence and step-wise metrics, which can be filtered by specific event lists. ```python stream.describe_events() stream.describe_events(event_list=['payment_done', 'cart']).T ``` -------------------------------- ### Truncate event paths in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb Truncates a stream of events by dropping data before a specific event trigger and applying a shift to the starting point. This is useful for isolating specific user journeys starting from a known action. ```python res = stream.truncate_paths( drop_before='cart', shift_before=-2 ).to_dataframe() ``` -------------------------------- ### JavaScript: Initialize and Render Retentioneering App in Iframe Source: https://github.com/retentioneering/retentioneering-tools/blob/master/retentioneering/templates/transition_graph/template.html This JavaScript code initializes an HTML document for an iframe, setting up essential meta tags, scripts for application state and logic, and a root div. It then writes the complete HTML structure into the iframe's content document. Dependencies include the parent window's environment and potentially external script URLs. ```javascript (() => { const TITLE = 'Retentioneering Transition Graph Application'; const iframeDocument = document.implementation.createHTMLDocument(TITLE); const h = tag => iframeDocument.createElement(tag); const toHead = element => iframeDocument.head.appendChild(element); const toBody = element => iframeDocument.body.appendChild(element); iframeDocument.documentElement.lang = 'en'; try { window.__RETE_HELPERS.proxyNotebookEnv(iframeDocument, window.top.document); } catch (error) { console.warn('Can not proxy environment', error); } const metaCharset = h('meta'); metaCharset.setAttribute('charset', 'UTF-8'); toHead(metaCharset); const metaViewport = h('meta'); metaViewport.name = 'viewport'; metaViewport.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'; toHead(metaViewport); const metaThemeColor = h('meta'); metaThemeColor.name = 'theme-color'; metaThemeColor.content = '#000000'; toHead(metaThemeColor); const stateScript = h('script'); stateScript.id = 'rete-state-script'; stateScript.type = 'application/json'; stateScript.text = `{{ state }}`; toHead(stateScript); const noScript = h('noscript'); noScript.innerText = 'You need to enable JavaScript to run this app.'; toBody(noScript); const appRoot = h('div'); appRoot.id = 'root'; toBody(appRoot); const appScript = h('script'); appScript.id = 'rete-app-script'; appScript.src = `{{ script_url }}`; toBody(appScript); const iframeElement = document.getElementById('iframe-{{ id }}'); if (iframeElement) { iframeElement.title = TITLE; const iframeContentDocument = iframeElement.contentDocument; iframeContentDocument.open(); iframeContentDocument.write('' + iframeDocument.documentElement.outerHTML); iframeContentDocument.close(); } })(); ``` -------------------------------- ### Apply custom transformations to stream Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb Uses the pipe method to apply custom transformations to the event stream. This example adds a new column to the dataframe, demonstrating how to extend the stream schema dynamically. ```python stream.pipe(lambda _df: _df.assign(new_column=100))\ .to_dataframe() ``` -------------------------------- ### Get Maximum Timestamp in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst This Python code snippet retrieves the maximum timestamp from a pandas DataFrame column named 'timestamp'. It's commonly used to determine the end point of an event stream or dataset. ```python res['timestamp'].max() ``` -------------------------------- ### Split user paths into sessions using Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst The SplitSessions processor divides user trajectories into sessions by inserting synthetic 'session_start' and 'session_end' events. This example demonstrates splitting sessions based on a 10-minute inactivity timeout. ```python res = stream.split_sessions(timeout=(10, 'm')).to_dataframe() res[res['user_id'] == 219483890] ``` -------------------------------- ### Initialize and fit clustering model Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/clusters.rst Demonstrates the standard workflow of creating a Clusters instance, extracting features, fitting a K-Means model, and generating a summary plot. ```python from retentioneering.tooling.clusters import Clusters clusters = Clusters(eventstream=stream) features = clusters.extract_features(feature_type='tfidf', ngram_range=(1, 1)) clusters.fit(method='kmeans', n_clusters=4, X=features, random_state=42) clusters.plot() ``` -------------------------------- ### Get Event Stream Timestamp Range (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb This code prints the minimum and maximum timestamps from an event stream DataFrame. It's useful for understanding the time span of the data. It requires a pandas DataFrame with a 'timestamp' column. ```python print('Eventstream start: {}'.format(res.timestamp.min())) print('Eventstream end: {}'.format(res.timestamp.max())) ``` -------------------------------- ### Get Unique User IDs as Integers in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb This Python code snippet extracts the unique values from the 'user_id' column of a DataFrame and converts them to integers. It's a common operation for understanding the distinct users present in a dataset. It relies on the pandas library. ```python res['user_id'].unique().astype(int) ``` -------------------------------- ### Configure transition graph weights in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/transition_graph.rst Demonstrates how to initialize a transition graph using the transition_graph method. It specifies the normalization type and the column used for calculating edge weights. ```python stream.transition_graph( edges_norm_type='node', edges_weight_col='user_id' ) ``` -------------------------------- ### Display Specific User Events (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb This code demonstrates how to filter a DataFrame to show all events for a particular user ID. It's useful for reviewing the complete event history of a single user, including path starts, raw events, and path ends. ```python res[res['user_id'] == 495985018] ``` -------------------------------- ### Launch preprocessing graph GUI Source: https://context7.com/retentioneering/retentioneering-tools/llms.txt Initializes an interactive visual interface for constructing and managing data preprocessing pipelines. Allows users to chain processors like session splitting and filtering. ```python pg = stream.preprocessing_graph( width=960, height=600 ) ``` -------------------------------- ### Timedelta Histogram with Synthetic Events (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Calculates and plots histograms of time differences between events, including synthetic start and end events created by splitting sessions. It demonstrates how to analyze time differences for both all events and raw events only, using logarithmic scaling and a specified time unit. ```python stream_with_synthetic = datasets\ .load_simple_shop()\ .add_start_end_events()\ .split_sessions(timeout=(30, 'm')) stream_with_synthetic.timedelta_hist(log_scale=True, timedelta_unit='m') stream_with_synthetic.timedelta_hist( raw_events_only=True, log_scale=True, timedelta_unit='m' ); ``` -------------------------------- ### Apply a function to an eventstream using Pipe in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst The Pipe data processor allows arbitrary modification of an input eventstream by applying a given function. The function must accept a DataFrame and return a modified DataFrame. This example demonstrates adding a new column 'new_column' with a value of 100. ```python stream.pipe(lambda _df: _df.assign(new_column=100)) .to_dataframe() .head(3) ``` -------------------------------- ### Load Data and Initialize Clusters Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/clusters.ipynb Loads a sample shop dataset and initializes the Clusters object. It prepares the event stream by splitting sessions based on a timeout. ```python import numpy as np import pandas as pd import retentioneering from retentioneering import datasets from retentioneering.tooling.clusters import Clusters stream = datasets.load_simple_shop().split_sessions(timeout=(30, 'm')) clusters = Clusters(eventstream=stream) ``` -------------------------------- ### Process and Analyze Event Streams with Retentioneering Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Demonstrates loading a sample dataset, splitting the stream into sessions with a 30-minute timeout, and retrieving a summary dataframe. It also covers generating descriptive statistics for session metrics and event-level distributions. ```python stream_with_sessions = datasets.load_simple_shop().split_sessions(timeout=(30, 'm')) stream_with_sessions.to_dataframe().head() # Describe session metrics stream_with_sessions.describe() # Describe event-level statistics stream = datasets.load_simple_shop() stream.describe_events() ``` -------------------------------- ### Visualize User Paths with Transition Graphs Source: https://context7.com/retentioneering/retentioneering-tools/llms.txt Creates interactive Markov-based transition graphs to visualize user movement between events. Supports customization of node thresholds, edge weights, and target highlighting. ```python from retentioneering import datasets stream = datasets.load_simple_shop() # Advanced transition graph with customization tg = stream.transition_graph( edges_norm_type='node', targets={ 'positive': ['payment_done'], 'negative': ['path_end'], 'source': ['path_start'] }, nodes_threshold={'user_id': 10}, edges_threshold={'user_id': 5}, edges_weight_col='user_id', width='100%', height='60vh', show_weights=True, show_percents=False, show_nodes_names=True ) # Access modified eventstream after GUI interactions modified_stream = tg.recalculation_result ``` -------------------------------- ### Create and Plot Transition Graph - Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/transition_graph.rst Demonstrates creating a separate TransitionGraph instance and plotting it with various customization options. This allows for more control over visualization parameters like normalization, thresholds, and target events. ```python from retentioneering.tooling.transition_graph import TransitionGraph tg = TransitionGraph(stream) tg.plot( edges_norm_type='node', edges_weight_col='user_id', edges_threshold={'user_id': 0.12}, nodes_threshold={'event_id': 500}, targets={'positive': ['payment_done', 'cart']} ) ``` -------------------------------- ### Query user status in dataframe Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst Shows how to filter the processed dataframe to verify if a specific user was labeled as an existing user. ```python res[res['user_id'] == 501098384].head() ``` -------------------------------- ### Create and link graph nodes with data processors Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/preprocessing.rst Demonstrates how to instantiate nodes using specific parameter classes like GroupEventsParams and AddStartEndEventsParams, and how to connect them to the graph structure. ```python node2 = EventsNode(GroupEvents(params=GroupEventsParams(**group_events_params))) from retentioneering.data_processors_lib import AddStartEndEvents, AddStartEndEventsParams node1 = EventsNode(AddStartEndEvents(params=AddStartEndEventsParams())) pgraph.add_node(node=node1, parents=[pgraph.root]) pgraph.add_node(node=node2, parents=[node1]) ``` -------------------------------- ### Perform Chi-Square Contingency Test Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/stattests.ipynb Calculates categorical differences between two non-overlapping user groups based on a custom event count function. ```python def cart_count(df): cart_count = len(df[df['event'] == 'cart']) if cart_count <= 2: return str(cart_count) return '>2' stream.stattests( groups=(user_group_1, user_group_2), func=cart_count, group_names=('product_1_group', 'product_2_group'), test='chi2_contingency' ) ``` -------------------------------- ### Import Preprocessing Graph from JSON Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/preprocessing.rst Demonstrates how to initialize a PreprocessingGraph instance and restore a saved configuration from a JSON file path. ```python # load data and create new PreprocessingGraph instance new_stream = datasets.load_simple_shop() new_pgraph = new_stream.preprocessing_graph() # restore the saved preprocessing configuration path_link = '/path/to/pgraph_config.json' new_pgraph.import_from_file(path_link) ``` -------------------------------- ### Load Simple Shop Dataset and Generate Step Matrix Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/step_matrix.ipynb Loads a pre-defined simple shop dataset and generates a step matrix. This demonstrates using built-in datasets for analysis. Dependencies include retentioneering.datasets. ```python from retentioneering import datasets stream = datasets.load_simple_shop() stream.step_matrix(max_steps=12, threshold=0) ``` -------------------------------- ### Analyze event distribution in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst Utility snippets for inspecting event counts within a DataFrame, useful for verifying the results of filtering operations. ```python res['user_id'].unique().astype(int) stream.to_dataframe()\ ['event']\ .value_counts()\ [lambda s: s.index.isin(['catalog', 'main'])] ``` -------------------------------- ### Define and Visualize Simple Example Step Matrix (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/step_matrix.rst This snippet demonstrates how to create a simple Pandas DataFrame representing an event stream and then use the Eventstream class to generate a step matrix visualization. It requires the pandas and retentioneering libraries. ```python import pandas as pd from retentioneering.eventstream import Eventstream simple_example = pd.DataFrame( [ ['user1', 'main', 0], ['user2', 'main', 0], ['user3', 'main', 0], ['user4', 'catalog', 0], ['user1', 'catalog', 1], ['user3', 'catalog', 1], ['user4', 'product', 1], ['user1', 'product', 2], ['user3', 'catalog', 2], ['user4', 'main', 2], ['user1', 'cart', 3], ['user3', 'product', 3], ['user4', 'catalog', 3], ['user1', 'catalog', 5], ['user3', 'cart', 5], ['user3', 'order', 6] ], columns=['user_id', 'event', 'timestamp'] ) Eventstream(simple_example)\ .step_matrix(max_steps=7, threshold=0); ``` -------------------------------- ### Load Simple Shop Dataset in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/datasets/simple_shop.rst Demonstrates how to load the Simple Shop dataset using the retentioneering library. It shows loading as an Eventstream object and as a pandas DataFrame using the `as_dataframe=True` flag. ```python from retentioneering import datasets stream = datasets.load_simple_shop() dataframe = datasets.load_simple_shop(as_dataframe=True) ``` -------------------------------- ### Fit clusters using pre-defined algorithms Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/clusters.rst Demonstrates how to initialize a Clusters object, extract features using TF-IDF, and fit the model using K-means clustering. ```python clusters = Clusters(eventstream=stream) features = clusters.extract_features(ngram_range=(1, 2), feature_type='tfidf') clusters.fit(method='kmeans', n_clusters=4, X=features, random_state=42) ``` -------------------------------- ### Upgrade Retentioneering via Pip (Bash) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/installation.rst Upgrades the retentioneering library to the latest version using pip from the command line. This ensures you have the most recent features and bug fixes. ```bash pip install --upgrade retentioneering ``` -------------------------------- ### Manual StepMatrix Instance Management Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/step_matrix.rst Shows how to instantiate the StepMatrix class independently of the eventstream shortcut method, allowing for explicit control over the fit and plot lifecycle. ```python from retentioneering.tooling.step_matrix import StepMatrix step_matrix = StepMatrix(stream) step_matrix.fit(max_steps=12, targets=['payment_done'], threshold=0) step_matrix.plot() ``` -------------------------------- ### Create EventsNode for Adding Start/End Events Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/preprocessing_graph.ipynb Creates an `EventsNode` using the `AddStartEndEvents` data processor. This node is used to add start and end events to the data stream. ```python from retentioneering.data_processors_lib import AddStartEndEvents, AddStartEndEventsParams node1 = EventsNode(AddStartEndEvents(params=AddStartEndEventsParams())) ``` -------------------------------- ### Add Negative Events in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/dataprocessors.rst This Python code illustrates how to use the add_negative_events data processor. It labels specific events, such as 'delivery_courier', as negative. The result is then converted to a DataFrame. ```python negative_events = ['delivery_courier'] res = stream.add_negative_events( targets=negative_events ).to_dataframe() ``` -------------------------------- ### Create Step Matrix Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/quick_start.rst Creates a step matrix to visualize event distribution with respect to step ordinal numbers. It allows customization of maximum steps, a threshold for event folding, and centering around a specific event. The output shows event distribution before and after the centered event. ```python stream.step_matrix( max_steps=16, threshold=0.2, centered={ 'event': 'cart', 'left_gap': 5, 'occurrence': 1 }, targets=['payment_done'] ) ``` -------------------------------- ### Label new users and add to graph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/user_guides/preprocessing.rst Shows how to fetch user data from a CSV source and use the LabelNewUsers processor to create a node in the preprocessing graph. ```python from retentioneering.data_processors_lib import LabelNewUsers, LabelNewUsersParams google_spreadsheet_id = '1iggpIT5CZcLILLZ94wCZPQv90tERwi1IB5Y1969C8zc' link = f'https://docs.google.com/spreadsheets/u/1/d/{google_spreadsheet_id}/export?format=csv&id={google_spreadsheet_id}' new_users = pd.read_csv(link, header=None)[0].tolist() node3 = EventsNode(LabelNewUsers(params=LabelNewUsersParams(new_users_list=new_users))) pgraph.add_node(node=node3, parents=[node2]) ``` -------------------------------- ### Truncate user paths in Retentioneering Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/dataprocessors.ipynb Truncates event paths after a specific event occurrence. This is useful for isolating user journeys up to a conversion point like a cart event. ```python res = stream.truncate_paths( drop_after='cart', occurrence_after="last" ).to_dataframe() res[res['user_id'] == 219483890] ``` -------------------------------- ### Plot Transition Graph with Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb This Python code snippet demonstrates how to initialize and plot a TransitionGraph. It uses the 'retentioneering' library to create a graph from a data stream and visualizes it with specified thresholds for edges and nodes, and defines target events for analysis. The output is an HTML object. ```python from retentioneering.tooling.transition_graph import TransitionGraph tg = TransitionGraph(stream) tg.plot( edges_norm_type='node', edges_weight_col='user_id', edges_threshold={'user_id': 0.12}, nodes_threshold={'event_id': 500}, targets={'positive': ['payment_done', 'cart']} ) ``` -------------------------------- ### Export Modified Event Stream as Transition Graph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb Creates a TransitionGraph object from an event stream, which can then be exported or further manipulated. This step is typically followed by saving or visualizing the graph. ```python tg = stream.transition_graph() ``` -------------------------------- ### Create a Simple Pandas DataFrame Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/cohorts.ipynb Creates a sample pandas DataFrame with user events and timestamps. This DataFrame serves as a basic input for demonstrating retentioneering functionalities, such as cohort analysis. ```python simple_df = pd.DataFrame( [ [1, "event", "2021-01-28 00:01:00"], [2, "event", "2021-01-30 00:01:00"], [1, "event", "2021-02-03 00:01:00"], [3, "event", "2021-02-04 00:01:00"], [4, "event", "2021-02-05 00:01:00"], [4, "event", "2021-03-06 00:01:00"], [1, "event", "2021-03-07 00:01:00"], [2, "event", "2021-03-07 00:01:00"], [3, "event", "2021-03-29 00:01:00"], [5, "event", "2021-03-30 00:01:00"], [4, "event", "2021-04-06 00:01:00"] ], columns=["user_id", "event", "timestamp"] ) simple_df ``` -------------------------------- ### Create and Fit StepMatrix Object (Python) Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/step_matrix.ipynb This code demonstrates how to create a `StepMatrix` object from a stream, fit it with specific targets and a maximum number of steps, and then plot the resulting matrix. The `fit` method calculates the transition probabilities between steps, and `plot` visualizes this data. ```python from retentioneering.tooling.step_matrix import StepMatrix step_matrix = StepMatrix(stream) step_matrix.fit(max_steps=12, targets=['payment_done'], threshold=0) step_matrix.plot(); ``` -------------------------------- ### Access Funnel Parameters Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/funnel.ipynb Retrieves the internal configuration and segment data used for a funnel calculation. This is useful for auditing the stage definitions and the specific user segments involved in the analysis. ```python stream.funnel( stages=['catalog', 'cart', 'payment_done'], show_plot=False ).params ``` -------------------------------- ### Configure Display Options for Transition Graph Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/transition_graph.ipynb Configures various display options for the transition graph, including showing weights, percentages, node names, and controlling the visibility of edges and nodes without links. This enhances the readability and detail of the visualization. ```python stream.transition_graph( edges_norm_type='node', show_weights=True, show_percents=True, show_nodes_names=True, show_all_edges_for_targets=False, show_nodes_without_links=False ) ``` -------------------------------- ### Visualize User Lifetime Histograms Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Generates a histogram visualization representing user lifetime metrics within the stream. This helps in understanding the duration or frequency distribution of user interactions. ```python stream.user_lifetime_hist(); ``` -------------------------------- ### Display EventStream as DataFrame Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Converts an EventStream object into a pandas DataFrame for tabular visualization. This allows for easier inspection of event attributes like event_id, event_type, timestamp, and user_id. ```python stream3.to_dataframe() ``` -------------------------------- ### Filter and Visualize Cluster Transitions Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/clusters.ipynb Filters a specific cluster, adds start and end events, and generates a transition graph. This is useful for understanding user flow within a particular segment. ```python clusters .filter_cluster(cluster_id=0) .add_start_end_events() .transition_graph( targets={ 'positive': 'payment_done', 'negative': 'path_end' } ); ``` -------------------------------- ### Perform Funnel Analysis Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/funnel.ipynb Executes funnel analysis on the stream object. Supports simple sequences, parallel stages, custom stage labels, and segmented analysis based on user cohorts. ```python # Basic funnel stream.funnel(stages=['catalog', 'cart', 'payment_done']); # Funnel with parallel stages stream.funnel(stages=['catalog', ['product1', 'product2'], 'cart', 'payment_done']); # Funnel with custom stage names stream.funnel( stages=['catalog', ['product1', 'product2'], 'cart', 'payment_done'], stage_names=['catalog', 'product', 'cart', 'payment_done'] ); # Segmented funnel analysis stream_df = stream.to_dataframe() cohorts = stream_df.groupby('user_id').first()['timestamp'].dt.strftime('%Y-%m') segment1 = cohorts[cohorts == '2020-01'].index segment2 = cohorts[cohorts == '2020-02'].index stream.funnel( stages=['catalog', ['product1', 'product2'], 'cart', 'payment_done'], stage_names=['catalog', 'product', 'cart', 'payment_done'], funnel_type='closed', segments=(segment1, segment2), segment_names=('converted', 'not_converted') ); ``` -------------------------------- ### Load Simple Shop Dataset Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/cohorts.ipynb Loads a sample dataset named 'simple_shop' using the retentioneering datasets module. This dataset can be used for practicing or demonstrating various retentioneering features. ```python from retentioneering import datasets stream = datasets.load_simple_shop() ``` -------------------------------- ### Constructing and executing a PreprocessingGraph in Python Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/getting_started/eventstream_concept.rst Demonstrates how to instantiate individual event nodes, link them within a PreprocessingGraph, and execute the calculation schema using the combine method. ```python from retentioneering.preprocessing_graph import PreprocessingGraph, EventsNode from retentioneering.data_processors_lib import SplitSessions, SplitSessionsParams from retentioneering.data_processors_lib import AddStartEndEvents, AddStartEndEventsParams # creating single nodes node1 = EventsNode(AddStartEndEvents(params=AddStartEndEventsParams())) node2 = EventsNode(SplitSessions(params=SplitSessionsParams(timeout=(1, 'h')))) # creating a preprocessing graph and linking the nodes pgraph = PreprocessingGraph(source_stream=stream) pgraph.add_node(node=node1, parents=[pgraph.root]) pgraph.add_node(node=node2, parents=[node1]) # run the calculation from the root node to SplitSessions node processed_stream = pgraph.combine(node=node2) ``` -------------------------------- ### Compare unique user counts in EventStream Source: https://github.com/retentioneering/retentioneering-tools/blob/master/docs/source/_static/user_guides_notebooks/eventstream.ipynb Calculates and prints the number of unique users in both the original and sampled datasets. This is useful for validating that sampling processes maintain representative user distributions. ```python unique_users_original = simple_shop_df['user_id'].nunique() unique_users_sampled = sampled_stream.to_dataframe()['user_id'].nunique() print('Original unique users number: ', unique_users_original) print('Sampled unique users number: ', unique_users_sampled) ```