0% found this document useful (0 votes)
45 views31 pages

How To Index, Slice and Reshape NumPy Arrays For Machine Learning

This document provides an overview of how to index, slice, and reshape NumPy arrays for machine learning applications in Python. It discusses converting lists to NumPy arrays, accessing data via indexing and slicing, and resizing arrays to meet machine learning API expectations. Key topics include one-dimensional and two-dimensional indexing and slicing of NumPy arrays, and converting between list and array data structures. The goal is to demonstrate proper manipulation of machine learning data stored in NumPy arrays.

Uploaded by

Mhd rdb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views31 pages

How To Index, Slice and Reshape NumPy Arrays For Machine Learning

This document provides an overview of how to index, slice, and reshape NumPy arrays for machine learning applications in Python. It discusses converting lists to NumPy arrays, accessing data via indexing and slicing, and resizing arrays to meet machine learning API expectations. Key topics include one-dimensional and two-dimensional indexing and slicing of NumPy arrays, and converting between list and array data structures. The goal is to demonstrate proper manipulation of machine learning data stored in NumPy arrays.

Uploaded by

Mhd rdb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

 Navigation

Click to Take the FREE Linear Algebra Crash-Course

Search... 

How to Index, Slice and Reshape NumPy Arrays for


Machine Learning
by Jason Brownlee on October 25, 2017 in Linear Algebra

Tweet Share Share

Last Updated on June 13, 2020

Machine learning data is represented as arrays.

In Python, data is almost universally represented as NumPy arrays.

If you are new to Python, you may be confused by some of the pythonic ways of accessing data, such
as negative indexing and array slicing.

In this tutorial, you will discover how to manipulate and access your data correctly in NumPy arrays.

After completing this tutorial, you will know:

How to convert your list data to NumPy arrays.


How to access data using Pythonic indexing and slicing.
How to resize your data to meet the expectations of some machine learning APIs.

Kick-start your project with my new book Linear Algebra for Machine Learning, including step-by-step
tutorials and the Python source code files for all examples.

Let’s get started.

Update Jul/2019: Fixed small typo related to reshaping 1D data (thanks Rodrigue).

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 1/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

Start Machine Learning ×


You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.

Email Address

START MY EMAIL COURSE

How to Index, Slice and Reshape NumPy Arrays for Machine Learning in Python
Photo by Björn Söderqvist, some rights reserved.

Tutorial Overview
This tutorial is divided into 4 parts; they are:

1. From List to Arrays


2. Array Indexing
3. Array Slicing
4. Array Reshaping

Start Machine Learning


Need help with Linear Algebra for Machine Learning?
Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Download Your FREE Mini-Course

1. From List to Arrays


https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 2/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

In general, I recommend loading your data from file using Pandas or even NumPy functions.

For examples, see the post:

How To Load Machine Learning Data in Python

This section assumes you have loaded or generated your data by other means and it is now
represented using Python lists.

Let’s look at converting your data in lists to NumPy arrays.

One-Dimensional List to Array


You may load your data or generate your data and have access to it as a list.
Start Machine Learning ×
You can convert a one-dimensional list of data to an array by calling the array() NumPy function.
You can master applied Machine Learning
1 # one dimensional example without math or fancy degrees.
2 from numpy import array Find out how in this free and practical course.
3 # list of data
4 data = [11, 22, 33, 44, 55]
5 # array of data Email Address
6 data = array(data)
7 print(data)
8 print(type(data))
START MY EMAIL COURSE
Running the example converts the one-dimensional list to a NumPy array.

1 [11 22 33 44 55]
2 <class 'numpy.ndarray'>

Two-Dimensional List of Lists to Array


It is more likely in machine learning that you will have two-dimensional data.

That is a table of data where each row represents a new observation and each column a new feature.

Perhaps you generated the data or loaded it using custom code and now you have a list of lists. Each
list represents a new observation.

You can convert your list of lists to a NumPy array the same way as above, by calling the array()
function. Start Machine Learning

1 # two dimensional example


2 from numpy import array
3 # list of data
4 data = [[11, 22],
5 [33, 44],
6 [55, 66]]
7 # array of data
8 data = array(data)
9 print(data)
10 print(type(data))

Running the example shows the data successfully converted.

1 [[11 22]
2 [33 44]
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 3/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

3 [55 66]]
4 <class 'numpy.ndarray'>

2. Array Indexing
Once your data is represented using a NumPy array, you can access it using indexing.

Let’s look at some examples of accessing data via indexing.

One-Dimensional Indexing
Generally, indexing works just like you would expect from your experience with other programming
languages, like Java, C#, and C++.

Start
For example, you can access elements using the bracket Machine
operator Learning
[] specifying the zero-offset index for ×
the value to retrieve.
You can master applied Machine Learning
1 # simple indexing without math or fancy degrees.
2 from numpy import array Find out how in this free and practical course.
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 # index data Email Address
6 print(data[0])
7 print(data[4])

START
Running the example prints the first and last values in the MY EMAIL COURSE
array.

1 11
2 55

Specifying integers too large for the bound of the array will cause an error.

1 # simple indexing
2 from numpy import array
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 # index data
6 print(data[5])

Running the example prints the following error:

1 IndexError: index 5 is out of bounds for axis 0 with size 5

One key difference is that you can use negative indexes to retrieve values offset from the end of the
Start Machine Learning
array.

For example, the index -1 refers to the last item in the array. The index -2 returns the second last item
all the way back to -5 for the first item in the current example.

1 # simple indexing
2 from numpy import array
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 # index data
6 print(data[-1])
7 print(data[-5])

Running the example prints the last and first items in the array.

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 4/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

1 55
2 11

Two-Dimensional Indexing
Indexing two-dimensional data is similar to indexing one-dimensional data, except that a comma is used
to separate the index for each dimension.

1 data[0,0]

This is different from C-based languages where a separate bracket operator is used for each
dimension.

1 data[0][0]

For example, we can access the first row and the firstStart
columnMachine
as follows: Learning ×
1 # 2d indexing
You can master applied Machine Learning
2 from numpy import array
3 # define array without math or fancy degrees.
4 data = array([[11, 22], [33, 44], [55, 66]]) Find out how in this free and practical course.
5 # index data
6 print(data[0,0])
Email Address
Running the example prints the first item in the dataset.

1 11 START MY EMAIL COURSE

If we are interested in all items in the first row, we could leave the second dimension index empty, for
example:

1 # 2d indexing
2 from numpy import array
3 # define array
4 data = array([[11, 22], [33, 44], [55, 66]])
5 # index data
6 print(data[0,])

This prints the first row of data.

1 [11 22]

3. Array Slicing
Start Machine Learning
So far, so good; creating and indexing arrays looks familiar.

Now we come to array slicing, and this is one feature that causes problems for beginners to Python and
NumPy arrays.

Structures like lists and NumPy arrays can be sliced. This means that a subsequence of the structure
can be indexed and retrieved.

This is most useful in machine learning when specifying input variables and output variables, or splitting
training rows from testing rows.

Slicing is specified using the colon operator ‘:’ with a ‘from‘ and ‘to‘ index before and after the column
respectively. The slice extends from the ‘from’ index and ends one item before the ‘to’ index.
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 5/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

1 data[from:to]

Let’s work through some examples.

One-Dimensional Slicing
You can access all data in an array dimension by specifying the slice ‘:’ with no indexes.

1 # simple slicing
2 from numpy import array
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 print(data[:])

Running the example prints all elements in the array.

1 [11 22 33 44 55] Start Machine Learning ×


The first item of the array can be sliced by specifying You
a slice that starts at index 0 and ends at index 1
can master applied Machine Learning
(one item before the ‘to’ index). without math or fancy degrees.
Find out how in this free and practical course.
1 # simple slicing
2 from numpy import array
3 # define array
Email Address
4 data = array([11, 22, 33, 44, 55])
5 print(data[0:1])

START MY EMAIL COURSE


Running the example returns a subarray with the first element.

1 [11]

We can also use negative indexes in slices. For example, we can slice the last two items in the list by
starting the slice at -2 (the second last item) and not specifying a ‘to’ index; that takes the slice to the
end of the dimension.

1 # simple slicing
2 from numpy import array
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 print(data[-2:])

Running the example returns a subarray with the last two items only.

1 [44 55]

Start Machine Learning


Two-Dimensional Slicing
Let’s look at the two examples of two-dimensional slicing you are most likely to use in machine learning.

Split Input and Output Features


It is common to split your loaded data into input variables (X) and the output variable (y).

We can do this by slicing all rows and all columns up to, but before the last column, then separately
indexing the last column.

For the input features, we can select all rows and all columns except the last one by specifying ‘:’ for in
the rows index, and :-1 in the columns index.

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 6/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

1 X = [:, :-1]

For the output column, we can select all rows again using ‘:’ and index just the last column by specifying
the -1 index.

1 y = [:, -1]

Putting all of this together, we can separate a 3-column 2D dataset into input and output data as
follows:

1 # split input and output


2 from numpy import array
3 # define array
4 data = array([[11, 22, 33],
5 [44, 55, 66],
6 [77, 88, 99]])
7
8
# separate data
X, y = data[:, :-1], data[:, -1] Start Machine Learning ×
9 print(X)
10 print(y) You can master applied Machine Learning
without math or fancy degrees.
Running the example prints the separated X and y elements. Note that X is a 2D array and y is a 1D
Find out how in this free and practical course.
array.

1 [[11 22] Email Address


2 [44 55]
3 [77 88]]
4 [33 66 99] START MY EMAIL COURSE

Split Train and Test Rows


It is common to split a loaded dataset into separate train and test sets.

This is a splitting of rows where some portion will be used to train the model and the remaining portion
will be used to estimate the skill of the trained model.

This would involve slicing all columns by specifying ‘:’ in the second dimension index. The training
dataset would be all rows from the beginning to the split point.

1 dataset
2 train = data[:split, :]

The test dataset would be all rows starting from the split point to the end of the dimension.

1 test = data[split:, :] Start Machine Learning

Putting all of this together, we can split the dataset at the contrived split point of 2.

1 # split train and test


2 from numpy import array
3 # define array
4 data = array([[11, 22, 33],
5 [44, 55, 66],
6 [77, 88, 99]])
7 # separate data
8 split = 2
9 train,test = data[:split,:],data[split:,:]
10 print(train)
11 print(test)

Running the example selects the first two rows for training and the last row for the test set.
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 7/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

1 [[11 22 33]
2 [44 55 66]]
3 [[77 88 99]]

4. Array Reshaping
After slicing your data, you may need to reshape it.

For example, some libraries, such as scikit-learn, may require that a one-dimensional array of output
variables (y) be shaped as a two-dimensional array with one column and outcomes for each row.

Some algorithms, like the Long Short-Term Memory recurrent neural network in Keras, require input to
be specified as a three-dimensional array comprised of samples, timesteps, and features.

Start
It is important to know how to reshape your NumPy arrays so Machine
that your dataLearning
meets the expectation of ×
specific Python libraries. We will look at these two examples.
You can master applied Machine Learning
without math or fancy degrees.
Data Shape Find out how in this free and practical course.
NumPy arrays have a shape attribute that returns a tuple of the length of each dimension of the array.
Email Address
For example:

1 # array shape START MY EMAIL COURSE


2 from numpy import array
3 # define array
4 data = array([11, 22, 33, 44, 55])
5 print(data.shape)

Running the example prints a tuple for the one dimension.

1 (5,)

A tuple with two lengths is returned for a two-dimensional array.

1 # array shape
2 from numpy import array
3 # list of data
4 data = [[11, 22],
5 [33, 44],
6 [55, 66]]
7 # array of data
8 data = array(data)
9 print(data.shape) Start Machine Learning

Running the example returns a tuple with the number of rows and columns.

1 (3, 2)

You can use the size of your array dimensions in the shape dimension, such as specifying parameters.

The elements of the tuple can be accessed just like an array, with the 0th index for the number of rows
and the 1st index for the number of columns. For example:

1 # array shape
2 from numpy import array
3 # list of data
4 data = [[11, 22],

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 8/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

5 [33, 44],
6 [55, 66]]
7 # array of data
8 data = array(data)
9 print('Rows: %d' % data.shape[0])
10 print('Cols: %d' % data.shape[1])

Running the example accesses the specific size of each dimension.

1 Rows: 3
2 Cols: 2

Reshape 1D to 2D Array
It is common to need to reshape a one-dimensional array into a two-dimensional array with one column
and multiple rows.
Start Machine Learning ×
NumPy provides the reshape() function on the NumPy array object that can be used to reshape the
data. You can master applied Machine Learning
without math or fancy degrees.
Find out how
The reshape() function takes a single argument that specifies theinnew
this shape
free andofpractical course.
the array. In the case of
reshaping a one-dimensional array into a two-dimensional array with one column, the tuple would be
the shape of the array as the first dimension (data.shape[0]) and 1 for the second dimension.
Email Address

1 data = data.reshape((data.shape[0], 1))


START MY EMAIL COURSE
Putting this all together, we get the following worked example.

1 # reshape 1D array
2 from numpy import array
3 from numpy import reshape
4 # define array
5 data = array([11, 22, 33, 44, 55])
6 print(data.shape)
7 # reshape
8 data = data.reshape((data.shape[0], 1))
9 print(data.shape)

Running the example prints the shape of the one-dimensional array, reshapes the array to have 5 rows
with 1 column, then prints this new shape.

1 (5,)
2 (5, 1)

Start Machine Learning


Reshape 2D to 3D Array
It is common to need to reshape two-dimensional data where each row represents a sequence into a
three-dimensional array for algorithms that expect multiple samples of one or more time steps and one
or more features.

A good example is the LSTM recurrent neural network model in the Keras deep learning library.

The reshape function can be used directly, specifying the new dimensionality. This is clear with an
example where each sequence has multiple time steps with one observation (feature) at each time
step.

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 9/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

We can use the sizes in the shape attribute on the array to specify the number of samples (rows) and
columns (time steps) and fix the number of features at 1.

1 data.reshape((data.shape[0], data.shape[1], 1))

Putting this all together, we get the following worked example.

1 # reshape 2D array
2 from numpy import array
3 # list of data
4 data = [[11, 22],
5 [33, 44],
6 [55, 66]]
7 # array of data
8 data = array(data)
9 print(data.shape)
10
11
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1)) Start Machine Learning ×
12 print(data.shape)
You can master applied Machine Learning
Running the example first prints the size of each dimension in the 2D array, reshapes the array, then
without math or fancy degrees.
summarizes the shape of the new 3D array. Find out how in this free and practical course.

1 (3, 2)
2 (3, 2, 1) Email Address

Further Reading START MY EMAIL COURSE

This section provides more resources on the topic if you are looking go deeper.

An Informal Introduction to Python


Array Creation in NumPy API
Indexing and Slicing in NumPy API
Basic Indexing in NumPy API
NumPy shape attribute
NumPy reshape() function

Summary
In this tutorial, you discovered how to access and reshape data in NumPy arrays with Python.

Specifically, you learned:


Start Machine Learning

How to convert your list data to NumPy arrays.


How to access data using Pythonic indexing and slicing.
How to resize your data to meet the expectations of some machine learning APIs.

Do you have any questions?


Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Linear Algebra for Machine Learning!

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 10/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

Develop a working understand of linear algebra


...by writing lines of code in python

Discover how in my new Ebook:


Linear Algebra for Machine Learning

It provides self-study tutorials on topics like:


Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA
and much more...

Finally Understand the Mathematics of Data


Skip the Academics. Just Results.

SEE WHAT'S INSIDE


Start Machine Learning ×
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
Tweet Share Share

Email Address
About Jason Brownlee
Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results
START MYtutorials.
with modern machine learning methods via hands-on EMAIL COURSE

View all posts by Jason Brownlee →

 Difference Between Return Sequences and Return States for LSTMs in Keras
How to Develop a Seq2Seq Model for Neural Machine Translation in Keras 

90 Responses to How to Index, Slice and Reshape NumPy Arrays for


Machine Learning

Start Machine Learning REPLY 


ltshan October 27, 2017 at 12:58 pm #

Great articles. Still get much base knowledge though used python for a while.

REPLY 
Jason Brownlee October 27, 2017 at 2:57 pm #

I’m glad it helped!

REPLY 
Ben January 8, 2018 at 7:53 pm #

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 11/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

I have a clarification regarding converting single dimensional array to matrix form(2D). Why we
have to convert 1D to 2D in machine learning?

REPLY 
Jason Brownlee January 9, 2018 at 5:27 am #

Most machine learning algorithms expect a matrix as input, each row is one observation.

REPLY 
Anthony The Koala March 24, 2018 at 8:53 am #

Dear Dr Jason, :,
Starthelp
Thank you for providing this tutorial on slicing. It will certainly Machine Learning
me understand material on LSTMs on
×
your page, and your e-books.
You can master applied Machine Learning
My question is on 2-D slicing and the method of slicing. I would
without like or
math a “generalised”
fancy degrees.concept of slicing.
In 1-D slicing an array can be split as: Find out how in this free and practical course.
myarray[from:to] – that is understood

Email
In 2-D slicing, “Split Input and Output Features”, you gave twoAddress
examples of splitting
x = [ : , :-1]
y = [ : , -1]
START MY EMAIL COURSE
I would like clarification please in the context of myarray[from:to] especially for 2D splicing especially
where there are two colons ‘:’

myarray[from: to] and x [ : , : -1] For x, to = : , and from = : – 1,

Thank you,
Anthony of NSW

REPLY 
Jason Brownlee March 25, 2018 at 6:24 am #

Good question Anthony!

When you just provide an index instead of a slice, like -1, it will select only that column/row, in this
case the last column.
Start Machine Learning
Doers that help?

REPLY 
Ashish K S Arya April 27, 2018 at 5:53 pm #

Hello;
thanks for such nice tutorial, i am new to numpy. While I am working on deep learning algorithms at a
certain juncture in my program I need to create an array of (1000,256,256,3) dims where 1000 images
data (of size 256*256*3) can be loaded.

Can you kindly help me in doing this in Python.

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 12/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

code looks like the following:

x_train=np.empty([256,256,3])

for i in range(0,1000):

pic= Image.open(f'{path}%s.jpg’ %i)

pic=pic.resize([256,256])

x_train2= np.asarray(pic)

x_train=np.stack((x_train,x_train2))

print(x_train.shape)

Thanks

Start Machine Learning ×


You can master applied Machine Learning
REPLY 
Jason Brownlee April 28, 2018 at 5:25 am # without math or fancy degrees.
Find out how in this free and practical course.
A good trick is to load the data into a list, convert the list to an array, and then reshape the
array to the required dimensionality.
Email Address

START MY EMAIL COURSE


REPLY 
Fatemeh June 8, 2018 at 6:01 am #

Great. the best description I had seen.

REPLY 
Jason Brownlee June 8, 2018 at 6:18 am #

Thanks, I’m glad it helped.

REPLY 
Visalini August 25, 2018 at 4:37 pm #

Hi Jason,
How to convert a 3D array into 1D array.. Start Machine Learning

Help me please..

REPLY 
Jason Brownlee August 26, 2018 at 6:23 am #

X = X.reshape((X.shape[0], X.shappe[1], 1))

REPLY 
hashim July 30, 2020 at 12:40 am #

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 13/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

X=X.reshape((X.shape[0]*X.shape[1]*X.shape[2],1))

REPLY 
SkrewEverything August 25, 2018 at 5:34 pm #

We can access 2D array just like C: data[0][0].

Using data[0, 0] is not the only way like you said.

Which version of python are you using?

×
REPLY 
Jason Brownlee August 26, 2018 at 6:24 am #
Start Machine Learning
Using [i, j] is valid for 2d numpy array access in Python 2 and 3.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY 
jerico April 23, 2019 at 4:53 pm #
Email Address
can you help me with this sir..your help will be much apreciated. thanks in advance!

https://stackoverflow.com/questions/55645616/how-to-output-masks-using-vis-util-visualize-
START MY EMAIL COURSE
boxes-and-labels-on-image-array

REPLY 
Jason Brownlee April 24, 2019 at 7:52 am #

Perhaps you can summarize the issue for me in a sentence or two?

REPLY 
ai October 1, 2018 at 3:58 am #

I’ve been searching for this information for so long. Thank you so so so much sir

Start Machine Learning


REPLY 
Jason Brownlee October 1, 2018 at 6:30 am #

You’re welcome!

REPLY 
Haotian Wang October 8, 2018 at 5:52 am #

Jason, you help me a lot

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 14/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Jason Brownlee October 8, 2018 at 9:29 am #

I’m happy to hear that it helped.

REPLY 
Maijama'a October 27, 2018 at 9:37 pm #

Thank you so much for this clear tutorial.

REPLY 
Jason Brownlee October 28, 2018 at 6:10 am #
Start Machine Learning ×
I’m glad it helped.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY 
RasmusT January 29, 2019 at 6:42 am #
Email Address
Hi, I am dealing with normalization of 3d timeseries data.

I have 3d time series data (samples, features, timestemps).


START MY EMAIL COURSE

How am I supposed to reshape my 3d data to do normalization?


I am quessing it goes like this:
From [samples, features, timesteps] to ([timesteps,features] or [features,timesteps]) and then use like
MinMaxScaler to fit_transform on the training data and then transform on test data?

REPLY 
Jason Brownlee January 29, 2019 at 11:39 am #

It is a good idea to normalize per time series across all samples.

I give examples here:


https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/

Start Machine Learning


REPLY 
Mansoor February 4, 2019 at 7:13 pm #

Thank you very much for this very useful, very easy to follow and understand introduction to
NumPy arrays. It was very helpful.

REPLY 
Jason Brownlee February 5, 2019 at 8:14 am #

Thanks, I’m glad it helped.

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 15/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Kuruvilla Abraham February 8, 2019 at 2:24 pm #

Hi jason,

my Dataframe has non – image data of dimensions (1446736, 11).

How can i reshape this array to be fed into CNN model

REPLY 
Jason Brownlee February 9, 2019 at 5:52 am #

This may help:


https://machinelearningmastery.com/faq/single-faq/what-is-the-difference-between-samples-
timesteps-and-features-for-lstm-input Start Machine Learning ×
You can master applied Machine Learning
without math or fancy degrees.
Kuruvilla Abraham February 9, 2019 atFind
4:41out
pm how
# in this free and practical course. REPLY 

thanks Jason will this apply to CNN model as well as this for LSTM ryt ?
Email Address

START MY EMAIL COURSE


REPLY 
Jason Brownlee February 10, 2019 at 9:40 am #

Yes.

REPLY 
Eva April 9, 2019 at 8:00 pm #

Hi Jason, thanks for the tutorial, really helps strengthening the basics.
I have a somewhat related question – the numpy reshape function has a default reshape order C. When
working in Keras, is there any difference in using the numpy reshape or the keras native reshape? Uses
that one the same order? And if not, is it possible to use numpy native functions directly in Keras code?

Thanks!

Start Machine Learning

REPLY 
Jason Brownlee April 10, 2019 at 6:11 am #

They solve different things.

You can use numpy to reshape your arrays.

Keras offers a reshape layer, to reshape tensors as part of a neural net.

REPLY 
Rahul April 13, 2019 at 5:37 pm #

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 16/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

You’re awesome dude! So well explained. Thank you!

REPLY 
Jason Brownlee April 14, 2019 at 5:44 am #

Thanks, I’m glad it helped.

REPLY 
Nastaran April 26, 2019 at 11:26 am #

Hi,

Just wondering how I can import a dataset of 2D arrays ?


Start Machine Learning ×
Thanks
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY 
Jason Brownlee April 26, 2019 at 1:57 pm #
Email Address
Do you mean load from file?

This might help: START MY EMAIL COURSE


http://machinelearningmastery.com/load-machine-learning-data-python/

REPLY 
dong zhan May 27, 2019 at 12:32 pm #

very thoughtful of you to give this tutorial, otherwise, it would be much harder for me to follow
your machine learning tutorial, thank you so much

REPLY 
Jason Brownlee May 27, 2019 at 2:38 pm #

Thanks, I’m glad it helped.

Start Machine Learning

REPLY 
Sandeep Dawre June 5, 2019 at 7:51 pm #

One of the best article!!

REPLY 
Jason Brownlee June 6, 2019 at 6:22 am #

Thanks!

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 17/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Hemanth June 18, 2019 at 4:24 am #

sir thanks a lot for this article.

REPLY 
Jason Brownlee June 18, 2019 at 6:42 am #

You’re welcome.

REPLY 
Rodrigue KANKEU July 2, 2019 at 5:21 pm #
Start Machine Learning
Hi Jason thanks a lot for this nice tutorial again. I have a problem with the following sentence of
×
the section 4 about reshaping.
You can master applied Machine Learning
For example, some libraries, such as scikit-learn, maywithout
require math
that aorone-dimensional
fancy degrees. array of output
variables (y) be shaped as a two-dimensional array with one
Find outcolumn andfree
how in this outcomes for each
and practical column.
course.
I’m not getting well Isn’t?
For example, some libraries, such as scikit-learn, may require that a one-dimensional array of output
Email Address
variables (y) be shaped as a two-dimensional array with one column and outcomes for each “row”.
In fact I’m confused.
START MY EMAIL COURSE
Thanks a lot for the work done.

REPLY 
Jason Brownlee July 3, 2019 at 8:24 am #

Yes, I mean row. Updated.

Yes, so [1, 2, 3] is a 1D with the shape (3,) becomes [[1, 2, 3]] or one column with 3 rows shape
(1,3).

REPLY 
Rakesh July 24, 2019 at 2:57 pm #

Dear sir, I want to input my tfidf vector of shape 156060×15103 into LSTM layer with 150
Start Machine Learning
timeseries step and 15103 features .. my lstm input should look something like
(None, 150, 15103). How can I achieve this ? Please help.

If array reshaping does not help here, please suggest any alternative way of how to create lstm layer
with (None, 150, 15103) using tfidf input.
My aim is to use tfidf output as input to LSTM layer with 150 timesteps.

Please help.

REPLY 
Jason Brownlee July 25, 2019 at 7:39 am #

Set the input_shape argument on the first LSTM layer to the desired shape.
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 18/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
tina August 15, 2019 at 7:08 pm #

how to ask the user to input a 3d and to solve that input as an inverse

REPLY 
Jason Brownlee August 16, 2019 at 7:50 am #

Sorry, I don’t follow. Can you elaborate?

Start Machine Learning ×


REPLY 
Nick August 28, 2019 at 9:46 pm #
You can master applied Machine Learning
Hi Jason, without math or fancy degrees.
Find out how in this free and practical course.
I am experiencing some weird indexing problem where I seemingly have the correct coordinates to call
both cv2.rectangle() and plt.Rectangle() but then using the same coordintes for slicing does not
Email
work, ie. y1 and y2 need to be reversed. I have an issue Address
up on OpenCV Github but can you take a look if
you have a moment? https://github.com/opencv/opencv/issues/15406
START MY EMAIL COURSE

REPLY 
Jason Brownlee August 29, 2019 at 6:07 am #

Sorry to hear that, this sounds like an opencv issue, not an python array issue.

Good Luck.

REPLY 
Ipsita October 10, 2019 at 6:18 pm #

I have got a .dat file that contains the matrix images is of size 784-by-1990, i.e., there are totally
1990 images, and each column of the
matrix corresponds to one image of size 28-by-28 pixels.how to visualize it.Pls help.
Start Machine Learning

REPLY 
Jason Brownlee October 11, 2019 at 6:15 am #

Perhaps this will help:


https://machinelearningmastery.com/how-to-load-and-manipulate-images-for-deep-learning-in-
python-with-pil-pillow/

REPLY 
Abdoo October 13, 2019 at 6:55 pm #

Great Article!
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 19/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

I have a data to be fed into stacked LSTM. The data of the shape (81,25,12). When I do predictions
using model.predict, the output will of course be 3D again (81,25,12). But I want to plot each feature by
itself. I want to convert the output back into 2D and slice each column/feature for error calculations and
plotting.

How can I convert 3D back into to 2D array?


thank you so much Jason!

REPLY 
Jason Brownlee October 14, 2019 at 8:06 am #

The output does not have to be 3d, the output can be any shape you design into your
model.
Start Machine Learning
Nevertheless, you can reshape an array using the reshape() function:
×
data = data.reshape((??)) You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.

REPLY 
Ryan October 16, 2019 at 3:58 am # Email Address

Hi Jason,
START MY EMAIL COURSE
Can you explain an array slicing like [:,:,1] for a 2D array please?

REPLY 
Jason Brownlee October 16, 2019 at 8:11 am #

Yes, it selects all rows and column 0.

REPLY 
shadrack kodondi October 17, 2019 at 1:37 am #

the best training in ML i have ever come across…THANK YOU

Start Machine Learning


REPLY 
Jason Brownlee October 17, 2019 at 6:39 am #

Thanks.

REPLY 
Anthony The Koala November 15, 2019 at 9:44 am #

Dear Dr Jason,
Under the heading Two-Dimensional Slicing”

Where it says:
“For the input features, we can select all rows and columns except the last one by…”
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 20/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

To clarify:
“For the input features, we can select all rows and all columns except the last column by….”

1 X = [:,:-1]

Where it says:
“For the output column, we can select all rows again using “:
and index just the last column by….”

To clarify:
“For the output column, we can select all rows again using “:
and index just the last row by….”

1 y = [:,-1]

Reason, in the last example, all columns are displayed of the last row.
Start Machine Learning ×
Alternatively, only the last row is displayed.
You can master applied Machine Learning
Thank you,
without math or fancy degrees.
Anthony of Sydney
Find out how in this free and practical course.

Email Address
REPLY 
Anthony The Koala November 15, 2019 at 10:04 am #

START MY EMAIL COURSE


Dear Dr Jason,
My further elaboration on section 2

1 import numpy as np
2
3 doo = np.array([[1,2,3],[4,5,6],[7,8,9]])
4 doo
5 array([[1, 2, 3],
6 [4, 5, 6],
7 [7, 8, 9]])
8
9 #Select only the all rows and all columns except the last column
10 doo[:,:-1]
11 array([[1, 2],
12 [4, 5],
13 [7, 8]])
14
15 #Select only all the rows and all columns except the last two columns
16 doo[:,:-2]
17 array([[1],
18 [4], Start Machine Learning
19 [7]])
20
21 #Select all rows and last column
22 doo[:,-1]
23 array([3, 6, 9])
24
25 #Select all rows and second column
26 doo[:,-2]
27 array([2, 5, 8])
28
29 #Select all rows and first column
30 doo[:,0]
31 array([1, 4, 7])

#Select all rows and first two columns


doo[:,0:2]

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 21/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

array([[1, 2],
[4, 5],
[7, 8]])

The list is not exhaustible, but you get a better feel by experimenting.

Thank you,
Anthony of Sydney

REPLY 
Anthony The Koala November 15, 2019 at 11:18 am #

Dear Dr Jason,
While the above methods look at selecting either ‘successive’ rows or columns, the finishing
Start Machine Learning
touch is to select particular columns or particular rows.
×
1 #The whole matrix You can master applied Machine Learning
2 doo without math or fancy degrees.
3 array([[1, 2, 3],
Find out how in this free and practical course.
4 [4, 5, 6],
5 [7, 8, 9]])
6
7 #Select the first and last (3rd row)Email Address
8 doo[[0,2],:]
9 array([[1, 2, 3],
10 [7, 8, 9]]) START MY EMAIL COURSE
11
12 #Select the first and last columns
13 doo[:,[0,2]]
14 array([[1, 3],
15 [4, 6],
16 [7, 9]])
17
18 #If you want the diagonal of the matrix, use numpy's diag = no clever indexing
19 np.diag(doo)
20 array([1, 5, 9])

Still exploring the fundamentals of matrix selection,

One question please:


How if you have a 3D matrix, how to slice a matrix.

Thank you
Anthony of Sydney

Start Machine Learning

REPLY 
Jason Brownlee November 16, 2019 at 7:17 am #

It is important to get good at slicing in Python.

REPLY 
Jason Brownlee November 16, 2019 at 7:16 am #

Well done!

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 22/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Jason Brownlee November 16, 2019 at 7:16 am #

Thanks.

REPLY 
Anthony The Koala November 20, 2019 at 1:33 am #

Dear Dr Jason,
I had a go at slicing a 3D array. I was able to select particular columns and particular rows. I
don’t know if 3D arrays are sliced or if there is an application for slicing 3D arrays. So here it is.

I note that the slicing techniques are not exhaustible.

1 doo = [1 + i for i in range(36)] Start Machine Learning ×


2 doo = np.reshape(doo,[3,3,4])
3 # We relate all examples from the 3x3x4 array
4 doo; # We could also say doo[:]
You can master applied Machine Learning
5 array([[[ 1, 2, 3, 4], without math or fancy degrees.
6 [ 5, 6, 7, 8], Find out how in this free and practical course.
7 [ 9, 10, 11, 12]],
8
9 [[13, 14, 15, 16],
Email Address
10 [17, 18, 19, 20],
11 [21, 22, 23, 24]],
12
13 [[25, 26, 27, 28], START MY EMAIL COURSE
14 [29, 30, 31, 32],
15 [33, 34, 35, 36]]])
16
17 doo[0,] # Oth submatrix (the first)
18 array([[ 1, 2, 3, 4],
19 [ 5, 6, 7, 8],
20 [ 9, 10, 11, 12]])
21
22 doo[1,] # 1st submatrix (the second)
23 array([[13, 14, 15, 16],
24 [17, 18, 19, 20],
25 [21, 22, 23, 24]])
26
27 doo[2,] # 2nd submatrix (the third)
28 array([[25, 26, 27, 28],
29 [29, 30, 31, 32],
30 [33, 34, 35, 36]])
31
32 doo[0:,0] ;# The first row (0th) of each submatrix
33 array([[ 1, 2, 3, 4],
34 [13, 14, 15, 16], Start Machine Learning
35 [25, 26, 27, 28]])
36
37 doo[0:,1] ;# The second row (1st) of each submatrix
38 array([[ 5, 6, 7, 8],
39 [17, 18, 19, 20],
40 [29, 30, 31, 32]])
41
42 doo[0:,2] ;# The third row (2nd) of each submatrix
43 array([[ 9, 10, 11, 12],
44 [21, 22, 23, 24],
45 [33, 34, 35, 36]])
46
47 #Getting specific columns for a particular submatrix
48 #doo[submatrix,[array of rows], column]\
49
50 doo[0,[0,1,2],0] #1st submatrix, rows 0-2, 0th column, that is first column of
51 array([1, 5, 9])
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 23/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

52
53 doo[0,[0,1,2],1] #1st submatrix, rows 0-2, 0th column, that is the 2nd column
54 array([ 2, 6, 10])
55
56 doo[1,[0,1,2],0] #2nd submatrix, rows 0-2, 0th column, that is the 1st column
57 array([13, 17, 21])
58
59 doo[1,[0,1,2],1] #2nd submatrix rows 0-2, 1st column, that is the 2nd column
60 array([14, 18, 22])
61
62 #Specific columns of all submatrices 3x3 -please relate this to the 3x4x3 matr
63 doo[:,:,0] #First column of all submatrices
64 array([[ 1, 5, 9],
65 [13, 17, 21],
66 [25, 29, 33]])
67
68 doo[:,:,1] #Second column of all submatrices
69 array([[ 2, 6, 10],
70
71
[14, 18, 22],
[26, 30, 34]]) Start Machine Learning ×
72
73 #doo[:,:,2] #Third column of all submatrices
You can master applied Machine Learning
74 #doo[:,:,3] #Fourth column of all submatrices
without math or fancy degrees.
75
76 #Selecting multiple columns Find out how in this free and practical course.
77 #Example 1st and 2nd columns of each submatrix
78 doo[:,:,[0,1]]
79 array([[[ 1, 2], Email Address
80 [ 5, 6],
81 [ 9, 10]],
82
83 [[13, 14], START MY EMAIL COURSE
84 [17, 18],
85 [21, 22]],
86
87 [[25, 26],
88 [29, 30],
89 [33, 34]]])
90
91 #Specific columns of all submatrices 3x3x3
92 doo[:,:,[0,1,3]]; #First, second and fourth cols of all submatrices
93 array([[[ 1, 2, 4],
94 [ 5, 6, 8],
95 [ 9, 10, 12]],
96
97 [[13, 14, 16],
98 [17, 18, 20],
99 [21, 22, 24]],
100
101 [[25, 26, 28],
102 [29, 30, 32],
103 [33, 34, 36]]])
Start Machine Learning
104
105 #Selecting particular rows - the list is not exhaustible
106 #How to select particular rows of all submatrix
107 doo[[0,1,2],0] #Select 1st (0th) row from each submatrix
108 array([[ 1, 2, 3, 4],
109 [13, 14, 15, 16],
110 [25, 26, 27, 28]])
111
112 doo[[0,1,2],1] #Select 2nd (1st) row from each submatrix
113 array([[ 5, 6, 7, 8],
114 [17, 18, 19, 20],
115 [29, 30, 31, 32]])
116
117 doo[[0,1,2],2] #Select 3rd (2nd) row from each submatrix
118 array([[ 9, 10, 11, 12],
119 [21, 22, 23, 24],
120 [33, 34, 35, 36]])

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 24/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

121
122 doo[[0,2],0] #Select 1st (0th) row from first and last submatrix
123 array([[ 1, 2, 3, 4],
124 [25, 26, 27, 28]])
125
126 doo[[0,2],1] #Select 2nd (1st) row from first and last submatrix
127 array([[ 5, 6, 7, 8],
128 [29, 30, 31, 32]])

Whether you call this selection or slicing depends on whether you use indices or the slicing
operator “:”.

Whatever procedure, the end result is that you want to get a subset of the original data structure.

What is the application of 3D slicing and/or selection?

Thank you
Start Machine Learning ×
Anthony of Sydney
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY 
Jason Brownlee November 20, 2019 at 6:19 am #

Nice work! Email Address

START MY EMAIL COURSE

Anthony The Koala June 17, 2020 at 3:25 pm #

Dear Dr Adrian,
I came across an array splitting with four parameters:
I understand that it substitutes a 0 at particular location

1 #Don't worry about this, GOTO simplified


2 fftShift[cY - size:cY + size, cX - size:cX + size] = 0 ;

To simplify: by subsituting for a,b,c,d:

1 fftShift[a: b, c: d] = 0 ;#substitute a 0, but was is a,b,c,d

What is a, b, c, d? What is the effect of a,b,c,d for rows and columns?

Thank you,
Anthony of Sydneyhank you,
Start Machine Learning
Anthony of Sydney

Jason Brownlee June 18, 2020 at 6:20 am #

Yes, that is “to” and “from” for rows then columns.

Anthony The Koala June 18, 2020 at 3:33 am #

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 25/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

Dear Dr Jason,
From experimentation, a and b means to select ath row to bth-1 row and at the same
time select the remaining from cth column to cth-1 column.
To illustrate;

1 doo = np.reshape([i for i in range(100)],(10,10))


2 doo
3 array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
4 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
5 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
6 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
7 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
8 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
9 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
10 [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
11 [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
12 [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Start Machine Learning ×
Suppose we did an operation slicing on doo

1 doo[0:3,1:2] You can master applied Machine Learning


2 array([[ 1], without math or fancy degrees.
3 [11], Find out how in this free and practical course.
4 [21]])
5
6 doo[0:3,1:4]
Email Address
7 array([[ 1, 2, 3],
8 [11, 12, 13],
9 [21, 22, 23]])
10 START MY EMAIL COURSE
11 doo[1:3,1:4]
12 array([[11, 12, 13],
13 [21, 22, 23]])

So if we assigned to those elements

1 doo[1:3,1:4] = 0
2 doo[1:3,1:4] = 0
3 >>> doo
4 array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
5 [10, 0, 0, 0, 14, 15, 16, 17, 18, 19],
6 [20, 0, 0, 0, 24, 25, 26, 27, 28, 29],
7 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
8 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
9 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
10 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
11 [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
12 [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
13 [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

Going back: Start Machine Learning

1 #Are two successive operations, one for columns and one for rows on the
2 doo = np.reshape([i for i in range(100)],(10,10))
3 doo[1:3] = 0 ;#It does not matter the order of doing doo[1:3] = 0 or d
4 doo[:,1:4] = 0
5 doo
6 array([[ 0, 0, 0, 0, 4, 5, 6, 7, 8, 9],
7 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
8 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
9 [30, 0, 0, 0, 34, 35, 36, 37, 38, 39],
10 [40, 0, 0, 0, 44, 45, 46, 47, 48, 49],
11 [50, 0, 0, 0, 54, 55, 56, 57, 58, 59],
12 [60, 0, 0, 0, 64, 65, 66, 67, 68, 69],
13 [70, 0, 0, 0, 74, 75, 76, 77, 78, 79],
14 [80, 0, 0, 0, 84, 85, 86, 87, 88, 89],
15 [90, 0, 0, 0, 94, 95, 96, 97, 98, 99]])

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 26/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

In sum doing slicing operations

1 #We are doing the substitution at the intersection of the rows and the co
2 doo[1:3,1:4] = 0
3 #We are substituting on the particular rows and the particular columns
4 doo[1:3] = 0 ;# Select the rows
5 doo[:,1:4] = 0 ;# Select the columns

I hope to exhaust all the possible methods of index slicing

Thank you,
Anthony of Sydney

REPLY 
spankwire November 30, 2019 at 4:37 am #
Start Machine Learning ×
In more advanced use case, you may find yourself needing to switch the dimensions of a
certain matrix. This is often the case in machine learning
Youapplications where Machine
can master applied a certainLearning
model expects a
certain shape for the inputs that is different from your dataset. NumPy’s
without math or fancy degrees.
Find out how in this free and practical course.

Email Address
REPLY 
Jason Brownlee November 30, 2019 at 6:32 am #

Yes, I think I give examples using moveaxis() withMY


START images
EMAILhere:
COURSE
https://machinelearningmastery.com/a-gentle-introduction-to-channels-first-and-channels-last-image-
formats-for-deep-learning/

REPLY 
Iyanda Taofeek December 13, 2019 at 10:06 am #

Please how can I convert a 1D array to 7D array?

REPLY 
Jason Brownlee December 13, 2019 at 1:42 pm #

You can use the reshape() function to specify the dimensions of existing numpy array.

Start Machine Learning

REPLY 
Pratik Chavhan January 10, 2020 at 3:24 am #

Nice workflow for Numpy slicing and reshaping………got much knowledge from ur article…and
it helped me a lot in my workspace..Thanks

REPLY 
Jason Brownlee January 10, 2020 at 7:29 am #

I’m happy to hear that!

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 27/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Alexiy February 28, 2020 at 8:22 am #

Explained better than on Stack Overflow. Thank you, man)

REPLY 
Jason Brownlee February 28, 2020 at 1:24 pm #

Thanks!

Seham March 13, 2020 at 1:46 am # Start Machine Learning ×


REPLY 

Such a great tutorial, thanks very much for your


Yougreat work. keep
can master the
applied good work
Machine up
Learning
without math or fancy degrees.
Find out how in this free and practical course.

REPLY 
Jason Brownlee March 13, 2020 at 8:19 am #
Email Address
Thanks, I’m happy it helps!
START MY EMAIL COURSE

REPLY 
Rajendran May 21, 2020 at 9:37 pm #

This tutorial really helps a lot. Thank you so much. How can I give my Input to RNN as my
dataset is 3d array. for example, it is (no. of samples, no. of rows, no.of columns)? because the RNN
accepts input as (samples, time steps and features) rite?

REPLY 
Jason Brownlee May 22, 2020 at 6:07 am #

You’re welcome.

Good question, see this:


https://machinelearningmastery.com/faq/single-faq/what-is-the-difference-between-samples-
Start Machine Learning
timesteps-and-features-for-lstm-input

REPLY 
Winner June 11, 2020 at 4:44 am #

‘How to Index, Slice and Reshape NumPy Arrays for Machine Learning’

Ok, I have been at this for weeks…., going through your post and then trying to figure out how you did it,
especially when the code does readily run on my system.
But this post most eloquently explained the root of the problem I have been facing over the last few
weeks.
And that has to do with my lack of understanding/knowledge that different python ‘libraries’ and network
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 28/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

models require different data formatting for input/output.


Today I have found this post which explains the fundamentals of data preparedness for beginners like
myself.
Thank you very much.
I have three questions for you:
1.
Is this all in your book?
2.
Is there some way you can redesign this website so that a ‘table of content’ for this site could show on
the left sidebar similar to (https://pandas.pydata.org/docs/getting_started/intro_tutorials/index.html )
starting at the beginner level? This will surely help with ease of navigation and troubleshooting. It takes
me HOURS to find some of the answers that I am looking for and it’s all right under my nose.
3.
Do you have a hard copy version of your book? Start Machine Learning ×
Winner
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY 
Jason Brownlee June 11, 2020 at 6:04 am #
Email Address
I do cover the basics of array indexing/manipulation in this book:
https://machinelearningmastery.com/linear_algebra_for_machine_learning/
START MY EMAIL COURSE
Great suggestion, this may help:
https://machinelearningmastery.com/start-here/

On hard copies:
https://machinelearningmastery.com/faq/single-faq/can-i-get-a-hard-copy-of-your-book

REPLY 
Han June 12, 2020 at 7:19 pm #

Thanks a lot Jason! This is really clear and helpful for me as a beginner. Just wanted to check if
there’s a typo in this sentence: “It is common to need to reshape a one-dimensional array into a two-
dimensional array with one column and multiple arrays (sic).” Do you mean *rows?

Start Machine Learning


REPLY 
Jason Brownlee June 13, 2020 at 5:56 am #

Thanks, fixed!

REPLY 
mm October 16, 2020 at 10:35 am #

How to enter matrix values from the text box tkinter

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 29/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

REPLY 
Jason Brownlee October 16, 2020 at 1:49 pm #

What is tkinter?

REPLY 
mm October 29, 2020 at 10:47 am #

What is meant is how to create numpy array and array values user intervention via
graphical interface

Jason Brownlee October 29, 2020Start


at 1:42 pmMachine
# Learning ×
REPLY 

You
Sorry, I don’t have any tutorials on can a
using master applied
graphical Machine
interface forLearning
creating numpy
arrays. without math or fancy degrees.
Find out how in this free and practical course.

Email Address

Leave a Reply
START MY EMAIL COURSE

Name (required)

Email (will not be published) (required)

Start Machine Learning


Website

SUBMIT COMMENT

Welcome!
I'm Jason Brownlee PhD
and I help developers get results with machine learning.
Read more

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 30/31
1/3/2021 How to Index, Slice and Reshape NumPy Arrays for Machine Learning

Never miss a tutorial:

Picked for you:

How to Index, Slice and Reshape NumPy Arrays for Machine Learning

How to Calculate Principal Component Analysis (PCA) from Scratch in Python

Start Machine Learning ×


Linear Algebra for Machine Learning (7-Day Mini-Course)
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
A Gentle Introduction to Sparse Matrices for Machine Learning

Email Address

How to Calculate the SVD from Scratch with Python START MY EMAIL COURSE

Loving the Tutorials?

The Linear Algebra for Machine Learning EBook is where you'll find the Really Good stuff.

>> SEE WHAT'S INSIDE

© 2020 Machine Learning Mastery Pty. Ltd. All Rights Reserved.


Start
Address: PO Box 206, Vermont Victoria 3133, Australia. | ACN: 626 Machine
223 336. Learning
LinkedIn | Twitter | Facebook | Newsletter | RSS

Privacy | Disclaimer | Terms | Contact | Sitemap | Search

https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 31/31

You might also like