View on GitHub

babbling.github.io

Code Babble

Defining Numpy ndarray with shape and type specifications

Definining ndarray specifying shape and type

import numpy

X_train_new = np.ndarray(shape=(0,32,32), dtype='uint8')
y_train_new = np.ndarray(shape=(0,1), dtype='uint8')

<!DOCTYPE html>

Numpy 4D concatenation with 3D matrices | babbling.github.io
View on GitHub

babbling.github.io

Code Babble

Numpy 4D concatenation with 3D matrices

What happens when you have a series of RGB images where image is represented as a 3D Numpy array such as (32,32,3).

Now let’s say you want to insert these into a single 4D Numpy array where the the first axis is the index for each image.

We can initialize this new fourth dimension by referencing it with the None term as follows using numpy.concatenate:

import numpy
mygray

print(mygray.shape)
newarray = mygray[None, :]  # initialize the array
print(newarray.shape)
newarray = numpy.concatenate((newarray, mygray[None, :]), axis=0)
newarray = numpy.concatenate((newarray, mygray[None, :]), axis=0)
newarray = numpy.concatenate((newarray, mygray[None, :]), axis=0)
print(newarray.shape)
xtract = newarray[0,:,:]
plt.imshow(xtract, cmap='gray')

Here is an equivalent method using numpy.vstack:

import numpy
mygray
print(mygray.shape)
newarray = mygray[None, :]  # initialize the array
print(newarray.shape)
newarray = numpy.vstack((newarray, mygray[None, :]))
newarray = numpy.vstack((newarray, mygray[None, :]))
newarray = numpy.vstack((newarray, mygray[None, :]))
print(newarray.shape)
xtract = newarray[0,:,:]
plt.imshow(xtract, cmap='gray')

Here is an equivalent method using numpy.append:

import numpy
mygray
print(mygray.shape)
newarray = mygray[None, :]  # initialize the array
print(newarray.shape)
newarray = numpy.append(newarray, mygray[None, :], axis=0)
newarray = numpy.append(newarray, mygray[None, :], axis=0)
newarray = numpy.append(newarray, mygray[None, :], axis=0)
print(newarray.shape)
xtract = newarray[0,:,:]
plt.imshow(xtract, cmap='gray')

{{ page.title }}

Definining ndarray specifying shape and type

import numpy

X_train_new = np.ndarray(shape=(0,32,32), dtype='uint8')
y_train_new = np.ndarray(shape=(0,1), dtype='uint8')

{{ page.previous }} {{ page.next }}