ARIMA models : plot_diagnostics share error - statsmodels

I am using plot_diagnostics to plot the ARIMA model.
My code is
mod = sm.tsa.statespace.SARIMAX(y,
order=(1, 1, 1),
seasonal_order=(1, 1, 0, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results = mod.fit()
print(results.summary().tables[1])
So far everything is good.
But when I try to use plot_diagonistic, I got error.
results.plot_diagnostics(figsize=(30,15))
plt.show()
The error is
ValueError: operands could not be broadcast together with shapes (9,) (3,) (9,)
Not sure why this happened. Thanks

try to change the lags value in plot_diagnostic, see example below :
results.plot_diagnostics(**lags=4**,figsize=(30,15))
While changing the lags value keep an eye on your error message parameters :
ValueError: operands could not be broadcast together with shapes
(9,) (3,) (9,)
message could change, So accordingly you can adjust your lags value and resolve the error.

Related

why are my negative contours not generating with vtk.vtkContourFilter?

I am using vtk.vtkContourFilter to create isolines from a dataset.
Everything seems to work fine, but any contour values I specify <= 0 don't seem to generate, even though there is definitely data with those values.
I am trying to contour this scalar 'S' in my unstructured grid file. Here you can see the range:
This is my code to create the contours:
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName('file.vtk')
reader.Update()
out = reader.GetOutput()
out.GetPointData().SetActiveScalars('S')
cnt = vtk.vtkContourFilter()
cnt.SetInputData(out)
cnt.GenerateValues(9,-4,4)
cnt.SetOutputPointsPrecision(1) #my lines are very janky if I use 0 or 2
cnt.Update()
lines=cnt.GetOutput()
writer = vtk.vtkPolyDataWriter()
writer.SetInputData(lines)
writer.SetFileName('wow.vtk')
writer.Write()
When I visualize the output, there's nothing above 0 for whatever reason. I've tried using SetNumberOfContours and setting values manually too, same result.
How can I make the filter contour the negative scalar values properly?
I feel like I am missing something basic.

cellfun in Matlab and Classification with Wavelet Scattering

I want to apply the following example to my data:
https://www.mathworks.com/help/wavelet/ug/digit-classification-with-wavelet-scattering.html
I have more than 4000 images. Images are 224x224x3. In other words 244*244 with 3 channels. After I load images in Matlab I want to apply "Wavelet Image Scattering Feature Extraction". In the beginning, I got the following error:
Error using tall/cellfun (line 21)
Argument 2 to CELLFUN must be one of the following data types: cell.
My codes are:
sf = waveletScattering2('ImageSize',[224 224],'InvarianceScale',112, ...
'NumRotations',[8 8]);
Ttrain = tall(x_train.X);
Ttest = tall(x_test.X);
trainfeatures = cellfun(#(x)helperScatImages(sf,x),Ttrain,'UniformOutput',false);
testfeatures = cellfun(#(x)helperScatImages(sf,x),Ttest,'UniformOutput',false);
As an example Ttrain is in the above code is:
4093x224x224x3 tall single (unevaluated)
How should I change the entire code in https://www.mathworks.com/help/wavelet/ug/digit-classification-with-wavelet-scattering.html to work properly?
Thank you in advance for any help.

Even an image in data set used to train is giving opposite values when making prediction

I am new to ML and TensorFlow. I am trying to build a CNN to categorize a good image against corrupted images, similar to rock paper scissor tutorials in tensor flow, except for only two categories.
The Colab Notebook
Model Architecture
train_generator = training_datagen.flow_from_directory(
TRAINING_DIR,
target_size=(150,150),
class_mode='categorical'
)
validation_generator = validation_datagen.flow_from_directory(
VALIDATION_DIR,
target_size=(150,150),
class_mode='categorical'
)
model = tf.keras.models.Sequential([
# Note the input shape is the desired size of the image 150x150 with 3 bytes color
# This is the first convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# The second convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The third convolution
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The fourth convolution
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5),
# 512 neuron hidden layer
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(2, activation='softmax')
])
model.summary()
model.compile(loss = 'categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
history = model.fit_generator(train_generator, epochs=25, validation_data = validation_generator, verbose = 1)
model.save("rps.h5")
Only Change I made was turning input shape to (150,150,1) to (150,150,3) and changed last layers output to 2 neurons from 3. The training gave me consistently accuracy of 90 above for data set of 600 images in each class. But when I am making a prediction using code in the tutorial, it gives me highly wrong values even for data in the data set.
PREDICTION
Original code in TensorFlow tutorial
for file in onlyfiles:
path = fn
img = image.load_img(path, target_size=(150, 150,3)) # changed target_size to (150, 150,3)) from (150,150 )
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print(classes)
I changed target_size to (150, 150,3)) from (150,150) in my belief that since my input is a 3 channel image,
Result
It gives very wrong values [0,1][0,1] for even images in which are in dataset
But when I changed the code to this
for file in onlyfiles:
path = fn
img = image.load_img(path, target_size=(150, 150,3))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x /= 255.
classes = model.predict(images, batch_size=10)
print(fn)
print(classes)
In this case values come like
[[9.9999774e-01 2.2242968e-06]]
[[9.9999785e-01 2.1864464e-06]]
[[9.9999785e-01 2.1641024e-06]]
one or two errors are there but it is very much correct
So my question even though the last activation is softmax, why it is now coming in decimal values, is there any logical mistake in the way I am making predictions.? I tried binary also, but couldn't find much difference.
Please note -
When you are changing output classes from 2 to 3, you are asking the model to categorise into 3 classes. This would contradict your problem statement which separates good and corrupted ones i.e 2 output classes (a binary problem). I think it can be reversed from 3 to 2 if I have understood the question correctly.
Second the output you are getting is perfectly correct, the neural network models outputs probabilities instead of absolute class values like 0 or 1. By probability, it tells how likely it belongs to say class 0 or class 1.
Also , as mentioned above by #BBloggsbott - you just have to use np.argmax on the output array which will tell you the probability of belonging to class 1 (Positive class) by default.
Hope this helps.
Thanks.
Softmax returns probability distributions for the vector it gets as input. So, the fact that you are getting decimal values is not a problem. If you want to find the exact class each image belongs to, try using the argmax function on the predictions.

How to get my X_rotated with Keras ImageDataGenerator

Hi I just want my databasis of MNIST randomly rotated:
I have my X which is a numpy array (5000, 1, 28, 28)
I want the X_rotated in the same order same dimension.
I have made this:
datagen = ImageDataGenerator(rotation_range=360)
datagen.fit(X)
Now how I get my X_rotated ?
They only explain how to make tricky stuff with epoch and batch I just want to get back my array where each image has randomly rotated nothing more, nothing tricky.
I don't understand why on tutorials they only explain how to make the tricky stuff but not the basics...
https://keras.io/preprocessing/image/
The NumpyArrayIterador and DirectoryIterator objects are pretty much like any python iterator:
g = ImageDataGenerator(...)
d = g.flow(..., batch_size=256, shuffle=False)
# flow all batches through the iterator,
# then zip all inputs and outputs, respectively.
batches = zip(*(next(d) for _ in range(len(d))))
# concatenate all inputs and outputs, respectively.
x, y = (np.concatenate(b) for b in batches)
print(x.shape, y.shape)
This should output something similar to this:
(5000, 1, 28, 28) (5000, ?)

pymc3 how to code multi-state discrete Bayes net CPT?

I'm trying to build a simple Bayesian network, where rain and sprinkler are the parents of wetgrass, but rain and sprinkler each have three (fuzzy-logic type rather rather than the usual two boolean) states, and wetgrass has two states (true/false). I can't find anywhere in the pymc3 docs what syntax to use to describe the CPTs for this -- I'm trying the following based on 2-state examples but it's not generalizing to three states the way I thought it would. Can anyone show the correct way to do this? (And also for the more general case where wetgrass has three states too.)
rain = mc.Categorical('rain', p = np.array([0.5, 0. ,0.5]))
sprinker = mc.Categorical('sprinkler', p=np.array([0.33,0.33,0.34]))
wetgrass = mc.Categorical('wetgrass',
mc.math.switch(rain,
mc.math.switch(sprinker, 10, 1, -4),
mc.math.switch(sprinker, -20, 1, 3),
mc.math.switch(sprinker, -5, 1, -0.5)))
[gives error at wetgrass definition:
Wrong number of inputs for Switch.make_node (got 4((, , , )), expected 3)
]
As I understand it - switch is a theano function similar to (b?a:b) in a C program; which is only doing a two way comparison. It's maybe possible to set up the CPT using a whole load of binary switches like this, but I really want to just give a 3D matrix CPT as the input as in BNT and other bayes net libraries. Is this currently possible ?
You can code a three-way switch using two individual switches:
tt.switch(sprinker == 0,
10
tt.switch(sprinker == 1, 1, -4))
But in general it is probably better to index into a table:
table = tt.constant(np.array([[...], [...]]))
value = table[rain, sprinker]

Resources