File size: 7,883 Bytes
6e8a7d4 e207857 f748c28 6e8a7d4 28e2398 e207857 f748c28 6e8a7d4 e207857 659fba8 28e2398 6e8a7d4 e207857 659fba8 6e8a7d4 28e2398 57776e0 28e2398 57776e0 28e2398 57776e0 6e8a7d4 e207857 659fba8 28e2398 6e8a7d4 28e2398 6e8a7d4 28e2398 6e8a7d4 28e2398 57776e0 6e8a7d4 e207857 659fba8 6e8a7d4 28e2398 6e8a7d4 57776e0 28e2398 6e8a7d4 57776e0 28e2398 6e8a7d4 57776e0 28e2398 6e8a7d4 e207857 57776e0 28e2398 16f65e2 28e2398 f748c28 28e2398 f748c28 28e2398 f748c28 28e2398 f748c28 28e2398 f748c28 28e2398 e207857 6e8a7d4 28e2398 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
import streamlit as st
import numpy as np
import pandas as pd
from langchain.tools import tool
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from typing import Union, List, Dict, Optional
import matplotlib.pyplot as plt
import seaborn as sns
import os
import base64
import io
# Set up LangChain with OpenAI (or any other LLM)
os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # Replace with your OpenAI API key
llm = ChatOpenAI(model="gpt-4", temperature=0.7)
@tool
def analyze_basic_stats(data: pd.DataFrame) -> str:
"""Calculate basic statistical measures for numerical columns in the dataset.
Args:
data (pd.DataFrame): The dataset to analyze. It should contain at least one numerical column.
Returns:
str: A string containing formatted basic statistics for each numerical column,
including mean, median, standard deviation, skewness, and missing value counts.
"""
stats = {}
numeric_cols = data.select_dtypes(include=[np.number]).columns
for col in numeric_cols:
stats[col] = {
'mean': float(data[col].mean()),
'median': float(data[col].median()),
'std': float(data[col].std()),
'skew': float(data[col].skew()),
'missing': int(data[col].isnull().sum())
}
return str(stats)
@tool
def generate_correlation_matrix(data: pd.DataFrame) -> str:
"""Generate a visual correlation matrix for numerical columns in the dataset.
Args:
data (pd.DataFrame): The dataset to analyze. It should contain at least two numerical columns.
Returns:
str: A base64 encoded string representing the correlation matrix plot image.
"""
numeric_data = data.select_dtypes(include=[np.number])
plt.figure(figsize=(10, 8))
sns.heatmap(numeric_data.corr(), annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
buf = io.BytesIO()
plt.savefig(buf, format='png')
plt.close()
return base64.b64encode(buf.getvalue()).decode()
@tool
def analyze_categorical_columns(data: pd.DataFrame) -> str:
"""Analyze categorical columns in the dataset for distribution and frequencies.
Args:
data (pd.DataFrame): The dataset to analyze. It should contain at least one categorical column.
Returns:
str: A string containing formatted analysis results for each categorical column,
including unique value counts, top categories, and missing value counts.
"""
categorical_cols = data.select_dtypes(include=['object', 'category']).columns
analysis = {}
for col in categorical_cols:
analysis[col] = {
'unique_values': int(data[col].nunique()),
'top_categories': data[col].value_counts().head(5).to_dict(),
'missing': int(data[col].isnull().sum())
}
return str(analysis)
@tool
def suggest_features(data: pd.DataFrame) -> str:
"""Suggest potential feature engineering steps based on data characteristics.
Args:
data (pd.DataFrame): The dataset to analyze. It can contain both numerical and categorical columns.
Returns:
str: A string containing suggestions for feature engineering based on
the characteristics of the input data.
"""
suggestions = []
numeric_cols = data.select_dtypes(include=[np.number]).columns
categorical_cols = data.select_dtypes(include=['object', 'category']).columns
if len(numeric_cols) >= 2:
suggestions.append("Consider creating interaction terms between numerical features")
if len(categorical_cols) > 0:
suggestions.append("Consider one-hot encoding for categorical variables")
for col in numeric_cols:
if data[col].skew() > 1 or data[col].skew() < -1:
suggestions.append(f"Consider log transformation for {col} due to skewness")
return '\n'.join(suggestions)
def main():
st.title("Data Analysis Assistant")
st.write("Upload your dataset and get automated analysis with natural language interaction.")
# Initialize session state
if 'data' not in st.session_state:
st.session_state['data'] = None
if 'agent' not in st.session_state:
st.session_state['agent'] = None
# Drag-and-drop file upload
uploaded_file = st.file_uploader("Drag and drop a CSV file here", type="csv")
try:
if uploaded_file is not None:
with st.spinner('Loading and processing your data...'):
# Load the dataset
data = pd.read_csv(uploaded_file)
st.session_state['data'] = data
# Initialize the LangChain agent with the tools
tools = [analyze_basic_stats, generate_correlation_matrix,
analyze_categorical_columns, suggest_features]
st.session_state['agent'] = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True
)
st.success(f'Successfully loaded dataset with {data.shape[0]} rows and {data.shape[1]} columns')
st.subheader("Data Preview")
st.dataframe(data.head())
if st.session_state['data'] is not None:
analysis_type = st.selectbox(
"Choose analysis type",
["Basic Statistics", "Correlation Analysis", "Categorical Analysis",
"Feature Engineering", "Custom Question"]
)
if analysis_type == "Basic Statistics":
with st.spinner('Analyzing basic statistics...'):
result = st.session_state['agent'].run(
f"Analyze the dataset and provide basic statistics: {st.session_state['data']}"
)
st.write(result)
elif analysis_type == "Correlation Analysis":
with st.spinner('Generating correlation matrix...'):
result = st.session_state['agent'].run(
f"Generate a correlation matrix for the dataset: {st.session_state['data']}"
)
if isinstance(result, str) and result.startswith('data:image') or ',' in result:
st.image(f"data:image/png;base64,{result.split(',')[-1]}")
else:
st.write(result)
elif analysis_type == "Categorical Analysis":
with st.spinner('Analyzing categorical columns...'):
result = st.session_state['agent'].run(
f"Analyze categorical columns in the dataset: {st.session_state['data']}"
)
st.write(result)
elif analysis_type == "Feature Engineering":
with st.spinner('Generating feature suggestions...'):
result = st.session_state['agent'].run(
f"Suggest feature engineering steps for the dataset: {st.session_state['data']}"
)
st.write(result)
elif analysis_type == "Custom Question":
question = st.text_input("What would you like to know about your data?")
if question:
with st.spinner('Analyzing...'):
result = st.session_state['agent'].run(question)
st.write(result)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main() |