Simplifying Exploratory Data Analysis with Lyzr Agent
Tags: ai, lyzr, tutorial, datascience
Published: 2024-03-19 02:29:47

In the era of data-driven decision-making, the demand for efficient data analysis tools is ever-growing. [Lyzr's](https://www.lyzr.ai/) **Automate EDA** (Exploratory Data Analysis) offers a revolutionary solution, making data analysis more intuitive and accessible to users of all skill levels. This article explores how Lyzr SDK simplifies the data analysis process by providing a conversational interface, empowering users to interact with their data effortlessly.

{% embed https://youtu.be/tcoKTz0h1Dw?si=Vp1Zu_DMB5NZT1jB %}

## Setting Up the Environment

To begin, let's set up our development environment:

```python
python3 -m venv venv
source venv/bin/activate
pip3 install lyzr  streamlit
```

Additionally, we'll configure environment variables, such as the OpenAI API key, which is essential for leveraging Lyzr's powerful natural language processing capabilities.

```python
# Importing necessary libraries and modules
import os
from PIL import Image
from pathlib import Path
import streamlit as st
from utils import utils
from dotenv import load_dotenv; load_dotenv()
from lyzr import DataConnector, DataAnalyzr

# Setting up environment variables
apikey = os.getenv('apikey')

# Creating directories for data and plots
data = "data"
plot = 'plot'
os.makedirs(data, exist_ok=True)
os.makedirs(plot, exist_ok=True)

```

## Data Uploader

This function provides a simple way for users to upload CSV files for analysis:

```python
def data_uploader():
    st.title("Data")
    st.subheader("Upload CSV file for analysis")
    # Upload csv file
    uploaded_file = st.file_uploader("Choose csv file", type=["csv"])
    if uploaded_file is not None:
        utils.save_uploaded_file(uploaded_file)
    else:
        utils.remove_existing_files(data)
        utils.remove_existing_files(plot)

```

## Automating EDA with Lyzr

Now, let's delve into the core functionality of our application—the Automate EDA powered by Lyzr:

```python
# Analyzr Functionality
def analyzr():
    path = utils.get_files_in_directory(data)
    path = path[0]

    dataframe = DataConnector().fetch_dataframe_from_csv(file_path=Path(path))
    analyzr = DataAnalyzr(df=dataframe, api_key=apikey)

    return analyzr

```

## Displaying Analysis Options

These functions provide interactive capabilities for users to explore dataset descriptions, recommended analyses, AI-generated queries, analysis insights, recommendations, and visualizations based on their custom queries:

```python
def display_description(analyzr):
    description = analyzr.dataset_description()
    if description is not None:
        st.subheader("Dataset Description:")
        st.write(description)

def display_recommended_analysis(analyzr):
    analysis = analyzr.analysis_recommendation()
    if analysis is not None:
        st.subheader("Analysis Recommendations:")
        st.write(analysis)

def display_queries(analyzr):
    queries = analyzr.ai_queries_df()
    if queries is not None:
        st.subheader("AI-Generated Queries:")
        st.write(queries)

def display_analysis(analyzr):
    query =  st.text_input("Write your query")
    if st.button("Submit"):
        analysis = analyzr.analysis_insights(user_input=query)
        if analysis is not None:
            st.subheader("Analysis Insights:")
            st.write(analysis)

def display_recommendation(analyzr):
    query =  st.text_input("Write your query")
    if st.button("Submit"):
        recommendation = analyzr.analysis_insights(user_input=query)
        if recommendation is not None:
            st.subheader("Recommendations:")
            st.write(recommendation)

def visualization_for_analysis(analyzr):
    query =  st.text_input("Write your analysis query")
    if st.button("Submit"):
        utils.remove_existing_files(plot)
        visualiation = analyzr.visualizations(user_input=query, dir_path=Path('./plot'))
        plot_files = os.listdir("./plot")
        for plot_file in plot_files:
            st.subheader(f'Visualization: {query}')
            st.image(f"./plot/{plot_file}")

```

## Executing the Application

This will orchestrate the navigation flow of the Automate EDA application. It allows users to seamlessly upload data for analysis and choose from various analysis options to explore and derive insights from their data.

```python
if __name__ == "__main__":
    st.sidebar.title("Automate EDA")
    selection = st.sidebar.radio("Go to", ["Data", "Analysis"])

    if selection == "Data":
        data_uploader()
    elif selection == "Analysis":
        file = file_checker()
        if len(file) > 0:
            analyzr = analyzr()
            # create buttons
            st.header("Select an Action")
            options = ['Select',"Description", "Recommended Analysis", "Queries", "Analysis", "Recommendation", "Visualization"]
            selected_option = st.radio("Select an option", options)

            if selected_option == "Description":
                display_description(analyzr)
            elif selected_option == "Recommended Analysis":
                display_recommended_analysis(analyzr)
            elif selected_option == "Queries":
                display_queries(analyzr)
            elif selected_option == "Analysis":
                display_analysis(analyzr)
            elif selected_option == "Recommendation":

```
Lyzr revolutionizes the data analysis workflow, making it accessible to everyone. Whether you're a seasoned data scientist or a novice enthusiast, Lyzr empowers you to derive valuable insights from your data with ease. Say goodbye to complexity and hello to simplicity with Lyzr Automate EDA.

## References
Ready to dive into the world of Lyzr? Explore further at:

- [Lyzr AI: The Simplest Agent Framework to Build GenAI Apps Faster](https://www.lyzr.ai/)

- [Book Demo - Lyzr.ai](https://www.lyzr.ai/book-demo/)

- [Lyzr Community Discord - A New Way to Chat with Friends & Communities](https://discord.com/invite/nm7zSyEFA2)

Join the conversation and revolutionize your data analysis journey with Lyzr! 💡✨

For a hands-on experience, check out our [GitHub repository](https://github.com/PrajjwalLyzr/Lyzr-AutomateEDA)