如何修复Jupyter Notebook中无法导入‘logsumexp’的错误
Let’s break down your problem and fix it step by step:
Root Cause
The error boils down to a version mismatch between statsmodels 0.9.0 and scipy 1.3.0:
- Your older statsmodels version (0.9.0) expects
logsumexpto live inscipy.misc, but starting from scipy 1.3.0, this function was moved to thescipy.specialmodule. The 0.9.0 statsmodels release wasn’t updated to handle this change. - Upgrading/downgrading scikit-learn didn’t resolve the issue because the conflict has nothing to do with scikit-learn—it’s purely between statsmodels and scipy. Also, that
sklearn 0.0version is just a dummy meta-package; your actual working scikit-learn is 0.21.2, so that’s not a problem.
Solutions (Ordered by Recommendation)
1. Upgrade statsmodels to a compatible version
This is the cleanest, most sustainable fix. You need a statsmodels version that supports scipy 1.3.0+—aim for 0.10.0 or newer, since that’s when they adjusted the logsumexp import path. Run this command in your environment:
pip install --upgrade statsmodels
2. Downgrade scipy to match your old statsmodels
If you can’t upgrade statsmodels (e.g., due to other project dependency constraints), roll back scipy to a version where logsumexp still resides in scipy.misc. Scipy 0.19.1 works perfectly with statsmodels 0.9.0:
pip install scipy==0.19.1
3. Temporary hack (Emergency Only)
If you need a quick fix right now and can’t adjust versions, manually patch the scipy.misc module before importing statsmodels. Add these lines at the very top of your notebook:
# Patch scipy.misc to include logsumexp from scipy.special import scipy.misc from scipy.special import logsumexp scipy.misc.logsumexp = logsumexp # Now import your original libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt import statsmodels.api as sm
⚠️ Note: This is a temporary workaround and might cause unexpected issues with other parts of scipy or statsmodels. Use it only if the first two solutions aren’t feasible.
内容的提问来源于stack exchange,提问作者Runjhun

