From af67e9e970b3bd9b6ebd4869917094243b59ac3d Mon Sep 17 00:00:00 2001
From: Nupur Lal \n",
+ " Attribution function in Vantage\n",
+ " Introduction Attribution refers to the process of assigning credit or responsibility to a specific event or entity that contributes to an outcome of interest.Specifically, the Attribution function is used for web page analysis, which refers to the process of assigning value or credit to different pages on a website for specific actions taken by visitors, such as making a purchase or filling out a form. The goal of attribution is to identify the most effective pages or content on a website that contribute to achieving business goals. By assigning weights or credit to different pages, organizations can optimize their website by improving or eliminating underperforming pages and investing more resources into the most effective ones. Attribution can be done using various methods, including rule-based attribution and data-driven attribution. In the section, we import the required libraries and set environment variables and environment paths (if required)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c5af5af3-29d5-4f6a-8334-9df6924e7787",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from teradataml import *\n",
+ "\n",
+ "# Modify the following to match the specific client environment settings\n",
+ "display.max_rows = 5"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ad3dd7b4-831c-4fb3-ab71-719c8c99a71c",
+ "metadata": {},
+ "source": [
+ " 1.1 Connect to Vantage You will be prompted to provide the password. Enter your password, press the Enter key, and then use the down arrow to go to the next cell. Begin running steps with Shift + Enter keys. 1.2 Getting Data for This Demo We have provided data for this demo on cloud storage. You can either run the demo using foreign tables to access the data without any storage on your environment or download the data to local storage, which may yield faster execution. Still, there could be considerations of available storage. Two statements are in the following cell, and one is commented out. You may switch which mode you choose by changing the comment string. Next is an optional step – if you want to see the status of databases/tables created and space used. Create a \"Virtual DataFrame\" that points to the data set in Vantage. Check the shape of the dataframe as check the datatype of all the columns of the dataframe. The Attribution function takes data and parameters from multiple tables and outputs attributions. Please refer to Teradata Vantage™ - Analytics Database Analytic Functions documentation for more on Attribution function or use help(Attribution) Attribution Input :\n",
+ "
\n",
+ " \n",
+ "
In this notebook we will see how we can use the Attribution function available in Vantage.
\n",
+ "1. Initiate a connection to Vantage"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2346857f-e0d3-488a-8a3f-ac6dff752c2b",
+ "metadata": {},
+ "source": [
+ "
\n",
+ "
\n",
+ "\n",
+ "
\n",
+ "2. Data Exploration\n",
+ "\n",
+ "
\n",
+ "
Attribution Syntax Elements:\n", + "
We will create the model and conversion tables that allow us to send large numbers of parameters programmatically to the Attribution analytic function.
\n", + "\n", + "Note: We are using SQLs to create the needed tables and insert the required values in these tables
" + ] + }, + { + "cell_type": "markdown", + "id": "502f4e4a-b5cc-4fea-b76e-c367ba487a33", + "metadata": {}, + "source": [ + "Detailed help can be found by passing function name to built-in help function.
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e6fa334-3aa1-407a-9788-e4a5a43c8013", + "metadata": {}, + "outputs": [], + "source": [ + "help(Attribution)" + ] + }, + { + "cell_type": "markdown", + "id": "f8111d7e-7404-4a7e-be5d-aca35d05861d", + "metadata": {}, + "source": [ + "For the input to our Attribution function, let's create Conversion event table i.e the events we want to track and the Model table for the type of attribution model we want to use.
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4fccb96f-6650-410a-b1f7-6f58bcddcf38", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "query1 = '''\n", + "CREATE MULTISET TABLE conversion_events (\n", + " conversion_event VARCHAR(55)\n", + ")\n", + "NO PRIMARY INDEX;\n", + "'''\n", + "query2 = '''\n", + "INSERT INTO conversion_events VALUES('ACCOUNT_BOOKED_ONLINE');\n", + "INSERT INTO conversion_events VALUES('ACCOUNT_BOOKED_OFFLINE');\n", + "'''\n", + "execute_sql(query1)\n", + "execute_sql(query2)" + ] + }, + { + "cell_type": "markdown", + "id": "ba1fdb98-0928-4a71-b15f-579f30f12ab5", + "metadata": {}, + "source": [ + "In our Conversion event table we have added 'ACCOUNT_BOOKED_ONLINE' and 'ACCOUNT_BOOKED_OFFLINE' events as we want to assign attribution score based on these events. Next is to assign attribution model
\n", + "Following methods available in Attribution:\n", + "
In the above Attribution Model the attribution score is assigned as followed. Attribution for a conversion event is divided among attributable events in 10 rows immediately preceding conversion event.
If conversion event is in row 11, first model specification applies to rows 10, 9, and 8; second applies to rows 7, 6, 5, and 4; and third applies to rows 3, 2, and 1.
\n",
+ "Half attribution (5/10) is uniformly divided among rows 10, 9, and 8; 3/10 to last click in rows 7, 6, 5, and 4 (that is, in row 7), and 2/10 to first click in rows 3, 2, and 1."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "76b40377-f96c-42c8-92ed-62765d3a3b69",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ConvEvent_df = DataFrame(\"conversion_events\")\n",
+ "FirstModel_df = DataFrame(\"attribution_model\") "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8329bfce-3c00-419b-8d45-867e94b3e1ef",
+ "metadata": {},
+ "source": [
+ "
Attribution function requires the datatype of the Event column to be Latin, hence we convert our data from Unicode to Latin.
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2d7528a-b158-47fe-912f-864b4673afd6", + "metadata": {}, + "outputs": [], + "source": [ + "from teradataml import ConvertTo\n", + "converted_data = ConvertTo(data = tdf,\n", + " target_columns = ['interaction_type'],\n", + " target_datatype = [\"VARCHAR(charlen=100,charset=LATIN,casespecific=NO)\"])\n", + "convert_tdf=converted_data.result\n", + "convert_tdf.to_sql('convert_tdf')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f24ca149-ed21-4aa9-a755-31282af91acd", + "metadata": {}, + "outputs": [], + "source": [ + "attribution_out = Attribution(data=DataFrame('convert_tdf'),\n", + " data_partition_column=\"customer_identifier\",\n", + " data_order_column=\"interaction_timestamp\",\n", + " event_column=\"interaction_type\",\n", + " conversion_data=ConvEvent_df,\n", + " timestamp_column = \"interaction_timestamp\",\n", + " window_size = \"rows:10\",\n", + " model1_type=FirstModel_df)\n", + "\n", + "Attrdf = attribution_out.result\n", + "Attrdf.sort('attribution',ascending=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "abd032b6-ea5b-4485-9f00-53a06e1025db", + "metadata": {}, + "outputs": [], + "source": [ + "print(attribution_out.show_query())" + ] + }, + { + "cell_type": "markdown", + "id": "151d5db4-29a9-49d9-8a61-d53f9627a294", + "metadata": {}, + "source": [ + "Databases and Tables
\n", + "The following code will clean up tables and databases created above.
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "baa36112-f1fe-4c1a-a47d-c7709ec0f051", + "metadata": {}, + "outputs": [], + "source": [ + "tables = ['conversion_events', 'attribution_model', 'convert_tdf']\n", + "\n", + "# Loop through the list of tables and execute the drop table command for each table\n", + "for table in tables:\n", + " try:\n", + " db_drop_table(table_name = table, schema_name = 'demo_user') \n", + " except:\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6b3935b-47c2-4a96-bec2-68106d172116", + "metadata": {}, + "outputs": [], + "source": [ + "%run -i ../../UseCases/run_procedure.py \"call remove_data('DEMO_Financial_cloud');\" # Takes 10 seconds" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157fe3d4-4e0e-4d92-b343-9f758f3bf690", + "metadata": {}, + "outputs": [], + "source": [ + "remove_context()" + ] + }, + { + "cell_type": "markdown", + "id": "4317a6cf-1479-4aa8-b30a-ee0a3b5231a8", + "metadata": {}, + "source": [ + "Links:
\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "b2dcca28-5de5-44d7-88cb-45a12153b3f8", + "metadata": {}, + "source": [ + "" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 83e20e3b5dd074f296bb3da6ef75023546f385ce Mon Sep 17 00:00:00 2001 From: Nupur Lal