KI-Analysewerkzeug
Entwickeln Sie KI-gestützte Werkzeuge für umfassende Datenanalyse, Text-Mining, Sentimentanalyse, Mustererkennung und automatische Berichterstellung.
Einführung
KI verwandelt Datenanalyse von einem manuellen, zeitaufwändigen Prozess in eine automatisierte Pipeline zur Erkenntnisgewinnung. Anstatt komplexe Abfragen zu schreiben oder statistische Modelle zu erstellen, können Sie in einfacher Sprache beschreiben, was Sie wissen möchten.
In diesem Leitfaden erstellen Sie ein vollständiges KI-Analysewerkzeug, das strukturierte Daten verarbeiten, Text analysieren, Sentiments und Muster erkennen und professionelle Berichte automatisch generieren kann.
Datenanalyse mit KI
KI kann strukturierte Daten (JSON, CSV, Datenbanken) analysieren und Erkenntnisse generieren, für die ein menschlicher Analyst Stunden bräuchte:
from openai import OpenAI
import json
client = OpenAI()
def analyze_dataset(data, context=""):
"""Perform AI-powered analysis on structured data."""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """You are a senior data analyst.
Analyze the provided data and return insights in JSON:
{
"summary": "Brief overview",
"key_metrics": [{"name": "", "value": "", "trend": ""}],
"patterns": ["pattern 1", "pattern 2"],
"anomalies": ["anomaly 1"],
"recommendations": ["action 1", "action 2"],
"risk_factors": ["risk 1"]
}"""},
{"role": "user", "content": f"Context: {context}\n\nData:\n{json.dumps(data, indent=2)}"}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Example usage
sales_data = {
"q1": {"revenue": 150000, "customers": 45, "churn": 3},
"q2": {"revenue": 180000, "customers": 52, "churn": 2},
"q3": {"revenue": 165000, "customers": 48, "churn": 7},
"q4": {"revenue": 210000, "customers": 61, "churn": 4}
}
insights = analyze_dataset(sales_data, "Annual B2B SaaS performance")
print(json.dumps(insights, indent=2))Textanalyse
Textanalyse extrahiert strukturierte Informationen aus unstrukturiertem Text – Artikeln, E-Mails, Berichten, Social-Media-Beiträgen:
def analyze_text(text, analysis_type="comprehensive"):
"""Analyze text for various linguistic properties."""
prompts = {
"comprehensive": """Analyze this text comprehensively:
1. Main topics and themes
2. Key entities (people, companies, locations)
3. Sentiment (positive/negative/neutral + confidence)
4. Writing style and tone
5. Summary in 2-3 sentences
Return as structured JSON.""",
"entities": """Extract all named entities from this text:
- People, Organizations, Locations, Dates, Products
Return as JSON with categories.""",
"keywords": """Extract the top 10 most important keywords/phrases.
For each, provide relevance score (0-1) and context.
Return as JSON array."""
}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": prompts.get(analysis_type, prompts["comprehensive"])},
{"role": "user", "content": text}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Usage
article = """Artificial intelligence is transforming healthcare.
New diagnostic tools powered by machine learning can detect
diseases earlier and more accurately than traditional methods.
Hospitals in Germany and the US are adopting these systems rapidly."""
result = analyze_text(article, "comprehensive")
print(json.dumps(result, indent=2))Sentimentanalyse
Sentimentanalyse bestimmt den emotionalen Ton von Text. Sie ist essentiell für die Überwachung von Kundenfeedback, Social Media und Markenwahrnehmung:
def batch_sentiment_analysis(texts):
"""Analyze sentiment for multiple texts at once."""
formatted = "\n".join([f"{i+1}. {t}" for i, t in enumerate(texts)])
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Analyze the sentiment
of each numbered text. Return JSON:
{"results": [
{"id": 1, "text_preview": "first 30 chars...",
"sentiment": "positive/negative/neutral/mixed",
"confidence": 0.0-1.0,
"emotions": ["joy", "trust"],
"key_phrases": ["positive phrase"]}
]}"""},
{"role": "user", "content": formatted}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Analyze customer reviews
reviews = [
"Absolutely love this product! Best purchase ever.",
"Terrible experience. The item arrived broken.",
"It's okay. Nothing special but does the job.",
"Great quality but shipping was very slow.",
"Customer service was outstanding, they fixed everything!"
]
results = batch_sentiment_analysis(reviews)
for r in results["results"]:
print(f"Review {r['id']}: {r['sentiment']} ({r['confidence']})")✅ Über Positiv/Negativ hinaus
Moderne KI kann nuancierte Emotionen (Frustration, Begeisterung, Verwirrung) und gemischte Sentiments innerhalb desselben Textes erkennen. Bitten Sie das Modell, spezifische Emotionen statt nur positiv/negativ zu identifizieren, für eine reichhaltigere Analyse.
Mustererkennung
KI ist ausgezeichnet darin, Muster in Daten zu erkennen, die Menschen übersehen könnten – Trends, Anomalien, zyklische Verhaltensweisen und Korrelationen:
def detect_patterns(data, data_type="time_series"):
"""Detect patterns in data using AI analysis."""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""You are an expert data scientist.
Analyze this {data_type} data for patterns:
1. Trends (upward, downward, cyclical)
2. Seasonality or periodic patterns
3. Outliers or anomalies
4. Correlations between variables
5. Predicted next values (if time series)
Return JSON with detailed findings and confidence levels."""},
{"role": "user", "content": json.dumps(data)}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Example: Detect patterns in website traffic
traffic_data = {
"daily_visitors": [
{"date": "2025-01-06", "visitors": 1200, "day": "Mon"},
{"date": "2025-01-07", "visitors": 1350, "day": "Tue"},
{"date": "2025-01-08", "visitors": 1400, "day": "Wed"},
{"date": "2025-01-09", "visitors": 1280, "day": "Thu"},
{"date": "2025-01-10", "visitors": 980, "day": "Fri"},
{"date": "2025-01-11", "visitors": 650, "day": "Sat"},
{"date": "2025-01-12", "visitors": 580, "day": "Sun"}
]
}
patterns = detect_patterns(traffic_data, "time_series")Berichterstellung
Der letzte Schritt ist die Umwandlung von Analyseergebnissen in menschenlesbare Berichte. KI kann verschiedene Stile je nach Zielgruppe generieren:
def generate_report(analysis_results, report_type="executive"):
"""Generate a human-readable report from analysis results."""
styles = {
"executive": "Write a concise executive summary (max 300 words). Focus on key findings, business impact, and recommended actions.",
"technical": "Write a detailed technical report with methodology, statistical details, and data quality notes.",
"presentation": "Create presentation-ready bullet points with key metrics highlighted. Include talking points for each slide."
}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""Generate a {report_type}
report based on these analysis results.
Style: {styles.get(report_type, styles['executive'])}
Structure the report with clear sections and headers.
Use specific numbers from the data."""},
{"role": "user", "content": json.dumps(analysis_results, indent=2)}
]
)
return response.choices[0].message.content
# Generate different report types
report = generate_report(insights, "executive")
print(report)Eine Analyse-Pipeline erstellen
Kombinieren Sie alle Analyseschritte in einer einzigen wiederverwendbaren Pipeline:
class AIAnalyzer:
"""Complete AI analysis pipeline."""
def __init__(self):
self.client = OpenAI()
self.results = {}
def ingest(self, data, data_type="auto"):
"""Load and prepare data for analysis."""
self.raw_data = data
self.data_type = data_type
return self
def analyze(self, analyses=["summary", "patterns", "sentiment"]):
"""Run multiple analyses on the data."""
for analysis in analyses:
if analysis == "summary":
self.results["summary"] = analyze_dataset(self.raw_data)
elif analysis == "patterns":
self.results["patterns"] = detect_patterns(self.raw_data)
elif analysis == "sentiment":
if isinstance(self.raw_data, list):
self.results["sentiment"] = batch_sentiment_analysis(
self.raw_data
)
return self
def report(self, report_type="executive"):
"""Generate a final report."""
self.results["report"] = generate_report(
self.results, report_type
)
return self.results
# Usage — chain the pipeline
pipeline = AIAnalyzer()
results = (
pipeline
.ingest(sales_data)
.analyze(["summary", "patterns"])
.report("executive")
)
print(results["report"])⚠️ KI-Analyse validieren
KI-Analyse ist leistungsstark, aber nicht unfehlbar. Überprüfen Sie numerische Behauptungen immer stichprobenartig anhand der Rohdaten. Für kritische Geschäftsentscheidungen verwenden Sie KI-Analyse als Ausgangspunkt und verifizieren Sie dann mit traditionellen statistischen Methoden.
Zusammenfassung
Sie haben eine vollständige KI-Analyse-Pipeline erstellt. Wichtigste Erkenntnisse:
- KI kann strukturierte Daten, Text und Sentiment in Sekunden mit einfachen Prompts analysieren.
- Mustererkennung hilft, Trends und Anomalien zu identifizieren, die Menschen übersehen könnten.
- Automatisierte Berichterstellung spart Stunden und kann mehrere Formate erzeugen.
- Validieren Sie KI-generierte Analysen immer anhand der Rohdaten auf ihre Genauigkeit.