X=sm.add_constant(X, prepend=True) is not working - python

I am trying to get the beta and the error term from a linear regression(OLS) in python. I am stuck at the statement X=sm.add_constant(X, prepend=True), which is returning an
error:"AttributeError: 'module' object has no attribute 'add_constant'"
I already installed the statsmodels module.

Try importing statsmodel.api
import statsmodels.api as sm

Try importing add_constant, for example:
from statsmodels.api import add_constant

If sm is a defined object in statsmodels, you need to invoke it by statsmodels.sm, or using from statsmodel import sm, then you can invoke sm directly.

Related

Error when using function of imported package

I am running the follwing code:
!pip install scikit-learn==0.24
import sklearn.metrics
mape = mean_absolute_percentage_error(target_test.values, p)
but then get an error. What is the problem with this code?
If you only need this function from sklearn.metrics it is recommended to also
import only this function by using:
from sklearn.metrics import mean_absolute_percentage_error
...
If you want to keep the syntax of your import as is, you have to call the function the following way:
mape = sklearn.metrics.mean_absolute_percentage_error(target_test.values, p)
If this is too long for you, you can import it and give it an own name:
import sklearn.metrics as skmetr
mape = skmetr.mean_absolute_percentage_error(target_test.values, p)

Failed to Importing Adida from statsforecast.models in Python

I was trying to replicate this code for stat forecasting in python, I came across the issue of not being able to load this model 'adida' form statsforecast library,
Here is the link for reference : https://towardsdatascience.com/time-series-forecasting-with-statistical-models-f08dcd1d24d1
import random
from itertools import product
from IPython.display import display, Markdown
from multiprocessing import cpu_count
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from nixtlats.data.datasets.m4 import M4, M4Info
from statsforecast import StatsForecast
from statsforecast.models import (
adida,
croston_classic,
croston_sba,
croston_optimized,
historic_average,
imapa,
naive,
random_walk_with_drift,
seasonal_exponential_smoothing,
seasonal_naive,
seasonal_window_average,
ses,
tsb,
window_average
)
Attached is the error message, Can you please have a look at this and let me know why is there an issue in importing this?
Given below is the error image:
I did some research and figured out the issue is probably with the version, try installing this specific version of statsforecast
pip install statsforecasts==0.6.0
Trying loading these models after that, hopefully this should work.
As of v1.0.0 of StatsForecast, the API changed to be more like sklearn, using classes instead of functions. You can find an example of the new syntax here: https://nixtla.github.io/statsforecast/examples/IntermittentData.html.
The new code would be
from statsforecast import StatsForecast
from statsforecast.models import ADIDA, IMAPA
model = StatsForecast(df=Y_train_df, # your data
models=[ADIDA(), IMAPA()],
freq=freq, # frequency of your data
n_jobs=-1)
If you want to use the old syntax, setting the version as suggested should work.
If you have updated the package ..use ADIDA it will work
see the model list name with new packages
ADIDA(),
IMAPA(),
(SimpleExponentialSmoothing(0.1)),
(TSB(0.3,0.2)),
(WindowAverage( 6))

Cannot import name 'threshold' from scipy.stats

I am trying to use function threshold from scipy package.
from scipy.stats import threshold
I_t=threshold(I, threshmin=2, threshmax=400, newval=-1) # I is an array containing image data
However, i am getting an error message saying
cannot import name 'threshold' from 'scipy.stats'
The function threshold was deprecated in SciPy 0.17.0 and removed from SciPy 1.0.0. Use numpy.clip (with arguments adjusted appropriately) instead.

what is the difference between import statsmodels.api as sm and import statsmodels as sm?

when I import statsmodels.api as sm, and run sm.nonparametric.smoothers_lowess.lowess
Error occurs:
module 'statsmodels.nonparametric.api' has no attribute 'smoothers_lowess'
But import statsmodels as sm doesn't have this error.
import statsmodels.api as sm
sm.nonparametric.smoothers_lowess.lowess()
import statsmodels as sm
sm.nonparametric.smoothers_lowess.lowess()
AttributeError: module 'statsmodels.nonparametric.api' has no attribute 'smoothers_lowess'
import statsmodels as sm makes your sm refer to statsmodels -- which is to say, statsmodels/__init__.py.
import statsmodels.api as sm makes your sm refer to statsmodels.api -- which is to say, statsmodels/api.py.
Because these are different files, it is normal and expected for them to have different contents. Note that all content accessed through the api modules is public functionality, supported and stable across releases; if you use nonpublic functionality, your code may break in future releases.
The difference in rationale between the APIs is documented at https://www.statsmodels.org/dev/importpaths.html

Python 3 Attribute Error: Statsmodels has no attribute 'tools'

Im trying to use the following code (example):
import pandas as pd
(import statsmodels.api as sm) - Tried adding, no luck
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
x = pd.DataFrame(imports a vector)
plot_acf(x)
There is some code in between, but the problem arises when Python tries to plot the autocorrelation using statsmodels, and returns the following error:
File "/Users/user/anaconda/lib/python3.6/site-packages/statsmodels/iolib/foreign.py",
line 20, in <module>
import statsmodels.tools.data as data_util
AttributeError: module 'statsmodels' has no attribute 'tools'
I tried reinstalling multiple libraries, but nothing seems to get me past this error. Could this be a statsmodels-side bug?

Categories

Resources