No module named 'statsmodels.tsa.arima' in Colab but not in Pycharm - python

# ARIMA example
from statsmodels.tsa.arima.model import ARIMA
data = [200,30,30,35,30,20,26,35,30,33,40,29,29,30,30,30,30,20,26,35,30,33,40,29,29,30,30,30]
# fit model
model = ARIMA(data, order=(10, 1, 10))
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data), typ='levels')
print(yhat)
The
from statsmodels.tsa.arima.model import ARIMA is wokring perfectly in pycharm but while running the same code in colab it throws
There are very few supports there on internet for this library, so I would appreciate any sort of help or any workaround please.

Try,
from statsmodels.tsa.arima_model import ARIMA
if you don't have statsmodel installed then also do,
pip install statsmodels

You import statsmodels like this:
import statsmodels.api as sm
And then you can use SARIMA like this:
model=sm.tsa.arima.ARIMA(data,order=(10, 1, 10))
The arima_model import is deprecated. You can read more about using ARIMA here.

You need a newer version. Try to run the following in your Colab:
!pip install statsmodels==0.12.1
It will allow the import that you want.

Related

Library errors with pmdarima and statsmodels

I have a problem with some libraries for time series.
In particular first error rise when i import this library
from pmdarima.arima import auto_arima
As suggested in another post I use the command !pip install pmdarima to solve this problem. But then I have to restart the runtime otherwise I can't compile and I also have to re-use the command every time I open my colab/jupyter notebook.
So my first question is related to this issue. Is there any solution to avoid this process every time?
The second problem is connected to the first one, because I import other libraries that are:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import datetime as datetime
from pmdarima.arima import auto_arima
from sklearn.metrics import mean_squared_error
from statsmodels.tsa.seasonal import seasonal_decompose
from dateutil.parser import parse
from statsmodels.tsa.stattools import adfuller
from pandas.plotting import autocorrelation_plot
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima_model import ARIMA
from pmdarima import auto_arima
from statsmodels.tsa.statespace.sarimax import SARIMAX
According to the fact that I know how to solve the first problem, then I have several lines of code related to the time series prediction and when I have to use a function where I'm using the ARIMA model:
def Predict(train,test,Order1,Order2,Order3,parForecastLenght=31):
# Build Model
model = ARIMA(train.astype("float32"), order=(Order1, Order2, Order3))
fitted = model.fit(disp=-1)
# Forecast
fc, se, conf = fitted.forecast(parForecastLenght, alpha=0.05)
# Make as pandas series
fc_series = pd.Series(fc, index=test.iloc[0:parForecastLenght].index)
lower_series = pd.Series(conf[:, 0], index=test.iloc[0:parForecastLenght].index)
upper_series = pd.Series(conf[:, 1], index=test.iloc[0:parForecastLenght].index)
# Plot
plt.figure(figsize=(12,5), dpi=100)
plt.plot(train, label='training')
plt.plot(test, label='actual')
plt.plot(fc_series, label='forecast')
plt.fill_between(lower_series.index, lower_series, upper_series, color='k', alpha=.15)
plt.title('Forecast vs Actuals')
plt.legend(loc='upper left', fontsize=8)
plt.show()
return fc_series
when I use try to execute this code:
model1 = Predict(train_Att_Assunzioni,test_Att_Assunzioni,0,0,0,30)
appears this kind of error:
NotImplementedError:
statsmodels.tsa.arima_model.ARMA and statsmodels.tsa.arima_model.ARIMA have
been removed in favor of statsmodels.tsa.arima.model.ARIMA (note the .
between arima and model) and statsmodels.tsa.SARIMAX.
statsmodels.tsa.arima.model.ARIMA makes use of the statespace framework and
is both well tested and maintained. It also offers alternative specialized
parameter estimators.
So again I check posts on stackoverflow, I tried to implement the suggested operations, but nothing seems to work except for the substitution of the library from from statsmodels.tsa.arima_model import ARIMA to from statsmodels.tsa.arima.model import ARIMA
but then the first problem rise again.
N.B. I tried to install statsmodels, pmadarima, I tried to change my work enviroment from colab to jupyter lab, but nothing

Alternative for TensorFlow tf.contrib

I keep getting an error when i run this code:
from sklearn import preprocessing
lencoder = preprocessing.LabelEncoder()
voc_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(kw_list)
voc_processor.fit(vocabulary)
X_transform = voc_processor.transform(reviews_df.reviewText)
X_transform = np.array(list(X_transform))
This is the error I get:
AttributeError: module 'tensorflow' has no attribute 'contrib'
Is there another approach to run this without having use an old version of tensorflow. I understand I am getting this error because tf.contrib. has been deprecated.
You can take a look at how new examples are dealing with text preprocessing, as an example
import tensorflow as tf
import tensorflow_transform as tft
[...]
review_tokens = tf.compat.v1.string_split(review, DELIMITERS)
review_indices = tft.compute_and_apply_vocabulary(
review_tokens, top_k=VOCAB_SIZE)
from
https://github.com/tensorflow/transform/blob/master/examples/sentiment_example.py

'NearMiss' object has no attribute '_validate_data'

Detailed Image
This is the code below which shows the error.
from imblearn.under_sampling import NearMiss
nm = NearMiss()
X_res,y_res=nm.fit_sample(X,Y)
You are probably trying to under sample your imbalanced dataset. For this purpose, you can use RandomUnderSampler instead of NearMiss.
Try the following code:
from imblearn.under_sampling import RandomUnderSampler
under_sampler = RandomUnderSampler()
X_res, y_res = under_sampler.fit_resample(X, y)
Now, your dataset is balanced. You can verify it using y_res.value_counts().
Cheers!
Instead of "imblearn" package my conda installed a package named "imbalanced-learn" that's why it does not take the data. But it is strange that the jupyter notebook doesn't tell me that "imblearn" isn't installed.

How to solve error in python program named: AttributeError: 'module' object has no attribute 'TensorFlowLinearClassifier'

This is the code which uses tensorflow library.
import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
print iris
classifier = skflow.TensorFlowLinearClassifier(n_classes=3)
classifier.fit(iris.data, iris.target)
score=metrics.accuracy_score(iris.target,classifier.predict(iris.data))
print ("Accracy: %f" % score)
I have created a python virtual environment and installed tensorflow in it. I tried to use conda as well this results in similar error
They have changed the name to LinearClassifier, therefore this will work
classifier = skflow.LinearClassifier(n_classes=3)
try using from tensorflow.contrib.learn import TensorFlowLinearClassifier

statsmodel: panel regression

I am currently using from pandas.stats.plm import PanelOLS to run Panel regressions. I am needing to switch to statsmodel so that I can ouput heteroskedastic robust results. I have been unable to find notation on calling a panel regression for statsmodel. In general, I find the documentation for statsmodel not very user friendly. Is someone familiar with panel regression syntax in statsmodel?
The linearmodels package is created to extend the statsmodels package to panelOLS (see https://github.com/bashtage/linearmodels). Here is the example from the package doc:
import numpy as np
from statsmodels.datasets import grunfeld
data = grunfeld.load_pandas().data
data.year = data.year.astype(np.int64)
# MultiIndex, entity - time
data = data.set_index(['firm','year'])
from linearmodels import PanelOLS
mod = PanelOLS(data.invest, data[['value','capital']], entity_effect=True)
res = mod.fit(cov_type='clustered', cluster_entity=True)
Best Daniel

Categories