{"id":740,"date":"2023-07-17T15:02:07","date_gmt":"2023-07-17T15:02:07","guid":{"rendered":"https:\/\/tbekk.com\/devstream\/?p=740"},"modified":"2023-07-17T15:02:07","modified_gmt":"2023-07-17T15:02:07","slug":"chatgpt-powered-data-exploration-unlock-hidden-insights-in-your-dataset","status":"publish","type":"post","link":"https:\/\/tbekk.com\/devstream\/2023\/07\/17\/chatgpt-powered-data-exploration-unlock-hidden-insights-in-your-dataset\/","title":{"rendered":"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Link: <\/em><\/strong><a href=\"https:\/\/www.kdnuggets.com\/2023\/07\/chatgptpowered-data-exploration-unlock-hidden-insights-dataset.html?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=chatgpt-powered-data-exploration-unlock-hidden-insights-in-your-dataset\"><em>ChatGPT-Powered Data Exploration<\/em><\/a> <\/li>\n\n\n\n<li><em><strong>Author:<\/strong><\/em>\u00a0<strong><a href=\"https:\/\/www.kdnuggets.com\/author\/bala-priya\"><em>Bala Priya C<\/em><\/a><\/strong><\/li>\n\n\n\n<li><em><strong>Publication data: <\/strong>July 17th, 2023<\/em><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-1024x576.png\" alt=\"\" class=\"wp-image-741\" srcset=\"https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-1024x576.png 1024w, https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-300x169.png 300w, https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-768x432.png 768w, https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-1536x864.png 1536w, https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image-624x351.png 624w, https:\/\/tbekk.com\/devstream\/wp-content\/uploads\/2023\/07\/image.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Analyzing a dataset to understand it better and gain insights is an important step in any data science project. And ChatGPT can help simplify and speed up such analysis.<\/p>\n\n\n\n<p>Here we\u2019ll perform exploratory data analysis on a sample dataset using ChatGPT. We\u2019ll understand distributions of features, how a feature of interest varies with other features, and much more.&nbsp;<\/p>\n\n\n\n<p>To follow along and access ChatGPT, you need an OpenAI account. If you don&#8217;t have one, you can&nbsp;<a href=\"https:\/\/platform.openai.com\/signup\" rel=\"noreferrer noopener\" target=\"_blank\">sign up for a free account<\/a>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Loading the Dataset and Getting Basic Info<\/h1>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/seaborn.pydata.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">seaborn library<\/a>&nbsp;has some ready-to-use datasets that we can analyze. We\u2019ll use the&nbsp;<strong>tips<\/strong>&nbsp;dataset.&nbsp;<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: Tell me more about the `tips` dataset built into the seaborn library.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_3.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>Knowing what the dataset is about is a good starting point. Let&#8217;s start by doing the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load the dataset into a pandas dataframe, and&nbsp;<\/li>\n\n\n\n<li>Get basic info and summary statistics of the columns to understand more about the dataset.<\/li>\n<\/ul>\n\n\n\n<p><strong>Prompt<\/strong>: Let\u2019s use the `tips` dataset. Write the code to read the dataset into a pandas dataframe and get the basic info and summary statistics of the available fields.<img decoding=\"async\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_14.png\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import seaborn as sns\nimport pandas as pd\n\n# Load the 'tips' dataset from Seaborn\ntips_data = sns.load_dataset('tips')\n\n# Create a Pandas DataFrame from the loaded dataset\ntips_df = pd.DataFrame(tips_data)\n\n# Display the first few rows of the DataFrame\nprint(\"First few rows of the 'tips' dataset:\")\nprint(tips_df.head())\n\n# Get basic information about the fields\nprint(\"\\nInformation about the 'tips' dataset:\")\nprint(tips_df.info())\n\n# Get summary statistics of the numeric fields\nprint(\"\\nSummary statistics of the numeric fields:\")\nprint(tips_df.describe())<\/code><\/pre>\n\n\n\n<p>We see that we have quite an informative summary of the dataset:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>First few rows of the 'tips' dataset:\n   total_bill   tip     sex smoker  day    time  size\n0       16.99  1.01  Female     No  Sun  Dinner     2\n1       10.34  1.66    Male     No  Sun  Dinner     3\n2       21.01  3.50    Male     No  Sun  Dinner     3\n3       23.68  3.31    Male     No  Sun  Dinner     2\n4       24.59  3.61  Female     No  Sun  Dinner     4\n\nInformation about the 'tips' dataset:\n\nRangeIndex: 244 entries, 0 to 243\nData columns (total 7 columns):\n #   Column      Non-Null Count  Dtype   \n---  ------      --------------  -----   \n 0   total_bill  244 non-null    float64 \n 1   tip         244 non-null    float64 \n 2   sex         244 non-null    category\n 3   smoker      244 non-null    category\n 4   day         244 non-null    category\n 5   time        244 non-null    category\n 6   size        244 non-null    int64   \ndtypes: category(4), float64(2), int64(1)\nmemory usage: 7.4 KB\nNone\n\nSummary statistics of the numeric fields:\n       total_bill         tip        size\ncount  244.000000  244.000000  244.000000\nmean    19.785943    2.998279    2.569672\nstd      8.902412    1.383638    0.951100\nmin      3.070000    1.000000    1.000000\n25%     13.347500    2.000000    2.000000\n50%     17.795000    2.900000    2.000000\n75%     24.127500    3.562500    3.000000\nmax     50.810000   10.000000    6.000000<\/code><\/pre>\n\n\n\n<p>From the summary statistics, we have an idea of the numerical features in the dataset. We know the minimum and maximum values, mean and median values, and percentile values for the numerical features. There are no missing values so we can proceed with the next steps.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Exploring the Dataset \u2013 The What, the Why, and the How<\/h1>\n\n\n\n<p>Now that we have an idea of the dataset, let&#8217;s go further.<\/p>\n\n\n\n<p>The goal of this exploratory data analysis is to understand the tipping behavior better. For this we can come up with helpful visualizations. These should help us understand the relationship of the tip amount to the various categorical variables in the dataset.<\/p>\n\n\n\n<p>Because this is a simple dataset to analyze, let&#8217;s prompt ChatGPT to give us a set of steps to go about analyzing this data set further.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: The goal of the analysis is to get some insights into the tipping behavior, starting with some simple visualizations. Suggest steps for the same.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_1.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>The data exploration steps suggested by ChatGPT all seem valid. So we will use these steps\u2014to better understand a dataset\u2014one step at a time. We\u2019ll prompt ChatGPT to generate code, try to run the&nbsp; generated code, and modify them as needed.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Exploring the Distribution of Tip Amounts<\/h1>\n\n\n\n<p>As a first step, let&#8217;s visualize the distribution of the tip amount prompt.&nbsp;<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: Write the code to plot the distribution of tip amounts.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_10.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>ChatGPT gave the following code to generate a histogram and a kernel density plot that help us get an idea of the distribution of the tip amounts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\n\n# Create a histogram of tip amounts\nplt.figure(figsize=(8, 6))\nsns.histplot(data=tips_data, x='tip', kde=True)\nplt.title(\"Distribution of Tip Amounts\")\nplt.xlabel(\"Tip Amount\")\nplt.ylabel(\"Frequency\")\nplt.show()<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_8.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"50%\"><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Analyzing Tipping Behavior Based on Categorical Variables<\/h1>\n\n\n\n<p>We have many categorical variables in the dataset. It will help to have a simple bar plot showing the average tip amount for the different values of these category variables.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: Use all the available categorical variables (\u2018sex\u2019, \u2018smoker\u2019, \u2018day\u2019, and \u2018time\u2019). Write the code to generate bar plots to understand tipping behavior. Use subplots so we can have all the four bar plots in a single figure.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_12.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>And here\u2019s the code. The code ran without errors; I only had to modify the figure size:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define the categorical variables to analyze\ncategorical_vars = &#91;'sex', 'smoker', 'day', 'time']\n\n# Create subplots for each categorical variable\nfig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 5))\nfig.suptitle(\"Tipping Behavior based on Categorical Variables\", fontsize=16)\n\n# Generate bar plots for each categorical variable\nfor ax, var in zip(axes.flatten(), categorical_vars):\n    sns.barplot(data=tips_data, x=var, y='tip', ax=ax)\n    ax.set_xlabel(var.capitalize())\n    ax.set_ylabel(\"Average Tip Amount\")\n\nplt.tight_layout()\nplt.show()<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"50%\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_9.png\"><\/p>\n\n\n\n<p>From the plots, we see that features like sex and smoking behavior don\u2019t influence tipping behavior (which is expected). While days and times seem to. The average tip amount on weekends and dinner seem to be slightly higher.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Visualizing the Relationship Between Total Bill and Tip Amount&nbsp;<\/h1>\n\n\n\n<p>Now, let\u2019s see how the total bill influences the tip amount paid.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: I\u2019d like to understand the relationship between total bill and the tip amount. Please give me the code to generate a suitable plot for this. I believe a simple scatter plot will be helpful.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_5.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>Here\u2019s the code to generate the required scatter plot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a scatter plot of total bill vs. tip amount\nplt.figure(figsize=(6, 4))\nsns.scatterplot(data=tips_data, x='total_bill', y='tip')\nplt.title(\"Total Bill vs. Tip Amount\")\nplt.xlabel(\"Total Bill\")\nplt.ylabel(\"Tip Amount\")\nplt.show()<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_11.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"50%\"><\/p>\n\n\n\n<p>We see that there\u2019s some positive correlation between the total bill and the tip amounts.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Visualizing the Relationship Between Party Size and Tip Amount&nbsp;<\/h1>\n\n\n\n<p>As a next step, let\u2019s try visualizing the relationship between the party size and the tip amount.&nbsp;<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: Now, I&#8217;d like to understand how the tip amount varies with the number of people who dine (party size). Which is a better choice: a bar plot or a violin plot?<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_2.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>As we\u2019d also like to understand the distribution of the tip amounts, let\u2019s go ahead with creating a violin plot.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: Cool! Please write the code to generate a violin plot for this visualization.<\/p>\n\n\n\n<p>And here\u2019s the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a violin plot for tip amount by party size\nplt.figure(figsize=(6, 4))\nsns.violinplot(data=tips_data, x='size', y='tip')\nplt.title(\"Tip Amount by Party Size\")\nplt.xlabel(\"Party Size\")\nplt.ylabel(\"Tip Amount\")\nplt.show()<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_7.jpg\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"50%\"><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Understanding Tipping Behavior Based on Both Time and Day<\/h1>\n\n\n\n<p>Next, let\u2019s understand how the time&nbsp;<em>and<\/em>&nbsp;the day influence the tipping behavior.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: I want to understand how the tipping behavior varies depending on both time and day. What is a good visualization? Also, write the code to generate the required visualization.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_13.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>This code snippet shows how to pivot using the day as the index and aggregate the mean value of the tip amounts for lunch and dinner (the time of the day):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a pivot table of average tip amount by time and day\npivot_table = tips_data.pivot_table(values='tip', index='day', columns='time', aggfunc='mean')\n\n# Create a heatmap of tipping behavior based on time and day\nplt.figure(figsize=(8, 6))\nsns.heatmap(pivot_table, cmap='YlGnBu', annot=True, fmt=\".2f\", cbar=True)\nplt.title(\"Tipping Behavior based on Time and Day\")\nplt.xlabel(\"Time\")\nplt.ylabel(\"Day\")\nplt.show()<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_6.jpg\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"50%\"><\/p>\n\n\n\n<p>From the heat map, we see that the average tip amounts are marginally higher for dinner on Sunday than other time-day pairs. In general, for a given day, on an average, tip amounts for dinner seem to be higher than those for lunch.&nbsp;<\/p>\n\n\n\n<p>We also see that there is not enough data for lunch on Saturdays and Sundays. Because we only have a total of 244 records, we may not have any record for lunches on weekends. Let\u2019s go ahead and verify this.<\/p>\n\n\n\n<p><strong>Prompt<\/strong>: I want to get the number of parties who ordered lunch on weekends. Write the code to filter the `tips_data` dataframe and get this count.<img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/c_chatgptpowered_data_exploration_unlock_hidden_insights_dataset_15.png\" alt=\"ChatGPT-Powered Data Exploration: Unlock Hidden Insights in Your Dataset\" width=\"100%\"><\/p>\n\n\n\n<p>Here\u2019s the code to filter the dataframe and obtain a subset containing the records for lunches on weekends:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Filter the data for lunch on weekends\nweekend_lunch_data = tips_data&#91;(tips_data&#91;'time'] == 'Lunch') &amp; (tips_data&#91;'day'].isin(&#91;'Sat', 'Sun']))]\n\n# Get the count of parties who ordered lunch on weekends\ncount = weekend_lunch_data&#91;'size'].count()\nprint(\"Number of parties who ordered lunch on weekends:\", count)<\/code><\/pre>\n\n\n\n<p>And we see that we don\u2019t have any records for the same, so our analysis is correct thus far:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number of parties who ordered lunch on weekends: 0<\/code><\/pre>\n\n\n\n<p>And that\u2019s a wrap! We explored the `tips` dataset and generated some helpful visualizations by prompting ChatGPT.&nbsp;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Wrapping Up<\/h1>\n\n\n\n<p>In this article, we learned how to leverage ChatGPT for data exploration. If you\u2019re interested in integrating ChatGPT into your data science workflow, check out&nbsp;<a href=\"https:\/\/www.kdnuggets.com\/2023\/05\/integrating-chatgpt-data-science-workflows-tips-best-practices.html\" rel=\"noreferrer noopener\" target=\"_blank\">this guide<\/a>. It walks through an example project\u2014along with tips and best practices\u2014to effectively use ChatGPT for data science experiments.<br>&nbsp;<br>&nbsp;<br><strong><a href=\"https:\/\/www.linkedin.com\/in\/bala-priya\/\" target=\"_blank\" rel=\"noreferrer noopener\">Bala Priya C<\/a><\/strong>&nbsp;is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she&#8217;s working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Analyzing a dataset to understand it better and gain insights is an important step in any data science project. And ChatGPT can help simplify and speed up such analysis. Here&#8230; <a class=\"read-more-link\" href=\"https:\/\/tbekk.com\/devstream\/2023\/07\/17\/chatgpt-powered-data-exploration-unlock-hidden-insights-in-your-dataset\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,51,44,115,197],"tags":[162,198,199],"class_list":["post-740","post","type-post","status-publish","format-standard","hentry","category-ai-2","category-article","category-data-analysis","category-data-science","category-generative-ai","tag-chatgpt","tag-generative-ai","tag-prompt-engineering"],"_links":{"self":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/comments?post=740"}],"version-history":[{"count":1,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/740\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/740\/revisions\/742"}],"wp:attachment":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/media?parent=740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/categories?post=740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/tags?post=740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}