Python 3 ImportError: cannot import name 'maxentropy'

Question:

I am using python 3.5.3 and for

from scipy import maxentropy

I am getting the error: ImportError: cannot import name ‘maxentropy’
Please suggest solution for the error. Thank you

Asked By: Dattaprasad

||

Answers:

scipy.maxentropy was removed from scipy in version 0.11 – https://docs.scipy.org/doc/scipy-0.10.1/reference/maxentropy.html

Answered By: Sergii Dymchenko

As per SciPy docs, maxentropy is removed since version 0.11.

Answered By: Oleksii Filonenko

This is what I found here:

The scipy.maxentropy module, which was deprecated in the 0.10.0 release, has been removed. Logistic regression in scikits.learn is a good and modern alternative for this functionality.

Hope it helps

Answered By: Georgy

The lates version of scipy is 0.19 and maxentropy module will not work with 0.11 or higher versions. If you have 0.11 or higher than try to download and install scipy 0.10 from the link below:

DOWNLOAD LINK TO SCIPY 0.10:
https://pypi.python.org/pypi/scipy/0.10.1

SOURCE:
https://docs.scipy.org/doc/scipy-0.10.1/reference/maxentropy.html

Answered By: user8297551

Like @Gregory pointed out maxentropy was removed from scipy and they suggested to use logistic regression instead, so here is an example of max entropy spectrum estimation on a signal made of 2 sines and a random noise:

import numpy as np
from scipy import signal
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt

# Generate a random time series data
np.random.seed(1234)
n = 1000
t = np.arange(n)
x = np.sin(2*np.pi*t/100) + 0.5*np.sin(2*np.pi*t/10) + 0.2*np.random.randn(n)

# Compute the autocorrelation function of the data
acf = signal.correlate(x, x, mode='full', method='auto')
acf = acf[len(acf)//2:]

# Define the constraints for maximum entropy estimation
order = 50   # order of the maximum entropy model
lags = np.arange(order)   # lags for autocorrelation function
acf_constraints = np.where(acf[:order] > 0, 1, 0)   # autocorrelation constraints
power_constraints = np.zeros(order)   # power spectrum constraints

# Fit a logistic regression model to the autocorrelation constraints
lr = LogisticRegression(penalty='none', solver='sag', max_iter=10000)
lr.fit(np.reshape(lags, (-1,1)), acf_constraints)

# Estimate the power spectrum using the logistic regression coefficients
spectrum = np.abs(np.fft.fft(lr.coef_[0]*np.hamming(order), n))**2
freq = np.fft.fftfreq(n, d=1)

# Plot the original signal and the estimated power spectrum
plt.figure(figsize=(8,6))
#plt.plot(t, x, label='Signal')
plt.plot(freq[:n//2], spectrum[:n//2], label='Power Spectrum')
plt.xlabel('Frequency')
plt.ylabel('Power')
plt.legend()
plt.show()
Answered By: mac13k
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.