pip install pandas numpy scikit-learn scipy plotly seaborn matplotlib openpyxlEnsure CodebookData_SEMPLS_IBB.xlsx is in the same directory as the script.
python enhanced_analysis.pyOpen any of the 15 HTML files in your browser to explore visualizations!
your_project_folder/
β
βββ CodebookData_SEMPLS_IBB.xlsx # Your input data (REQUIRED)
βββ enhanced_analysis.py # Main script
βββ DOCUMENTATION.md # Detailed documentation
βββ COMPARISON_GUIDE.md # Before/after comparison
βββ QUICK_START.md # This file
β
βββ outputs/ (auto-generated)
βββ plot_1_trait_deviations.html
βββ plot_2_risk_score.html
βββ ... (15 HTML files total)
βββ analysis_results_enhanced.csv
βββ profile_characteristics_table.csv
βββ statistical_tests.csv
βββ key_insights_summary.csv
βββ profile_recommendations.csv
βββ summary_statistics_by_profile.csv
- Open
key_insights_summary.csv- High-level findings - Open
plot_3_profiles.html- See profile distribution - Open
plot_8_radar_profiles.html- Compare profiles visually
- Read through all visualizations (plot_1 through plot_15)
- Review
statistical_tests.csvfor significance - Check
profile_recommendations.csvfor action items - Examine
summary_statistics_by_profile.csvfor details
- Use
summary_statistics_by_profile.csvas Table 1 - Include
plot_6_correlation_heatmap.htmlas Figure 1 - Include
plot_8_radar_profiles.htmlas Figure 2 - Report ANOVA results from
statistical_tests.csvin text - Reference
key_insights_summary.csvfor abstract/discussion
- Start with
plot_13_sunburst.html(overview) - Show
plot_3_profiles.html(segment sizes) - Present
plot_7_risk_categories.html(risk distribution) - Share
profile_recommendations.csv(action plan) - Use
plot_10_risk_predictors.html(prioritization)
Solution: Install missing package
pip install xyzSolution: Make sure your Excel file is named exactly CodebookData_SEMPLS_IBB.xlsx and is in the same folder as the script.
Solution: They're saved as HTML files. Open them directly in any web browser (Chrome, Firefox, Safari, Edge).
Solution: Check console for error messages. Make sure all packages are installed.
Solution: Change n_clusters=4 in line 54 of the script to your desired number.
# Line 54: Change from 4 to any number
kmeans = KMeans(n_clusters=5, random_state=42, n_init=10) # Now 5 clusters# Lines 34-39: Modify weights
df['Raw_Risk'] = (
(2.0 * df['IB_Score']) + # Increase impulse buying weight
df['Happy_Score'] +
(0.5 * df['Promo_Score']) + # Decrease promotion weight
df['Social_Score'] +
df['Risk_SC_Penalty']
)# Line 197: Modify cutoffs
df['Risk_Category'] = pd.cut(df['Impulse_Risk_Score'],
bins=[0, 40, 70, 100], # Changed from [0,33,66,100]
labels=['Low Risk', 'Medium Risk', 'High Risk'])# Lines 59-70: Edit labeling logic
labels = {}
for i in range(4):
row = cluster_means.loc[i]
# Add your own rules here
if row['Promo_Score'] > 4.0:
labels[i] = "Super Shopper" # Custom name
elif row['Social_Score'] > 4.0:
labels[i] = "Influencer Buyer" # Custom name
# ... etc| File | What It Contains | When to Use |
|---|---|---|
analysis_results_enhanced.csv |
Full dataset with all calculated features | Raw data export, further analysis |
profile_characteristics_table.csv |
Statistics for each profile | Profile descriptions, comparisons |
statistical_tests.csv |
ANOVA and Kruskal-Wallis results | Significance testing, academic papers |
key_insights_summary.csv |
Top-level findings | Executive summary, abstracts |
profile_recommendations.csv |
Intervention and marketing strategies | Action planning, decision-making |
summary_statistics_by_profile.csv |
Comprehensive descriptive stats | Detailed reporting, appendices |
| Plot # | Name | Best For |
|---|---|---|
| 1 | Trait Deviations | Data exploration, outlier check |
| 2 | Risk Distribution | Overall risk assessment |
| 3 | Profile Counts | Segment sizing |
| 4 | PCA Clusters | Cluster validation |
| 5 | Cluster Centers | Mean comparisons |
| 6 | Correlation Heatmap | Factor relationships |
| 7 | Risk Categories | Risk segmentation |
| 8 | Radar Profiles | Quick visual comparison |
| 9 | Distribution Comparison | Variance analysis |
| 10 | Risk Predictors | Prioritization |
| 11 | Outliers | Data quality |
| 12 | Percentile Rankings | Individual positioning |
| 13 | Sunburst | Hierarchical overview |
| 14 | Parallel Coordinates | Multi-dimensional patterns |
| 15 | Cumulative Distribution | Threshold setting |
import glob
excel_files = glob.glob("*.xlsx")
for file in excel_files:
print(f"Processing {file}...")
df = pd.read_excel(file)
# ... rest of analysis
# Save with unique names
df.to_csv(f'{file[:-5]}_results.csv', index=False)# Add after each fig.write_html() line:
fig.write_image("plot_1.png", width=1200, height=800)
# Requires: pip install kaleidopython enhanced_analysis.py > /dev/null 2>&1# Linux/Mac (crontab)
0 9 * * * cd /path/to/project && python enhanced_analysis.py
# Windows (Task Scheduler)
# Create new task pointing to python.exe with enhanced_analysis.py as argument- Run script as-is
- Open HTML files
- Read key_insights_summary.csv
- Done! β
- Run script
- Review DOCUMENTATION.md
- Compare COMPARISON_GUIDE.md
- Modify risk formula
- Re-run and compare results
- Add new features (demographics, etc.)
- Try different clustering algorithms
- Build predictive models (regression, classification)
- Create interactive dashboard (Dash/Streamlit)
- Integrate with business systems
- DOCUMENTATION.md - Detailed explanations of every feature
- COMPARISON_GUIDE.md - Before/after comparison and use cases
- Script comments - Inline explanations in the code
Q: How many participants do I need?
A: Minimum 100 for meaningful clustering, 200+ recommended.
Q: What if I have missing data?
A: Currently filled with 0. Check data quality in Plot 11 (outliers).
Q: Can I use different survey questions?
A: Yes! Modify the regex patterns in lines 25-30 to match your column names.
Q: How do I cite this analysis?
A: Include methodology description from DOCUMENTATION.md in your paper.
Q: Can I share these files?
A: Yes, but ensure you have permission to share the underlying data.
Before running the analysis, ensure:
- Data file is in correct location
- Data file is named
CodebookData_SEMPLS_IBB.xlsx(or update script) - All packages installed (
pip listto check) - Python version 3.7 or higher (
python --version) - At least 100 MB free disk space for outputs
- No other programs have the Excel file open
Just run:
python enhanced_analysis.pyExpected runtime: 30-60 seconds
Expected output: 21 files (15 HTML + 6 CSV)
Next step: Open HTML files in browser and explore!
- Plotly Documentation: https://plotly.com/python/
- Scikit-learn Clustering: https://scikit-learn.org/stable/modules/clustering.html
- Pandas Guide: https://pandas.pydata.org/docs/user_guide/index.html
- Statistical Tests: https://docs.scipy.org/doc/scipy/reference/stats.html
Happy Analyzing! ππ
For detailed feature explanations, see DOCUMENTATION.md
For before/after comparison, see COMPARISON_GUIDE.md