How To Index, Slice and Reshape NumPy Arrays For Machine Learning
How To Index, Slice and Reshape NumPy Arrays For Machine Learning
Navigation
Search...
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.
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.
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
Email Address
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:
Click to sign-up and also get a free PDF Ebook version of the course.
In general, I recommend loading your data from file using Pandas or even NumPy functions.
This section assumes you have loaded or generated your data by other means and it is now
represented using Python lists.
1 [11 22 33 44 55]
2 <class 'numpy.ndarray'>
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 [[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.
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])
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.
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,])
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]
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[:])
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]
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:
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.
Putting all of this together, we can split the dataset at the contrived split point of 2.
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 (5,)
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])
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 # 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)
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 # 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
This section provides more resources on the topic if you are looking go deeper.
Summary
In this tutorial, you discovered how to access and reshape data in NumPy arrays with Python.
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
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
Difference Between Return Sequences and Return States for LSTMs in Keras
How to Develop a Seq2Seq Model for Neural Machine Translation in Keras
Great articles. Still get much base knowledge though used python for a while.
REPLY
Jason Brownlee October 27, 2017 at 2:57 pm #
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 ‘:’
Thank you,
Anthony of NSW
REPLY
Jason Brownlee March 25, 2018 at 6:24 am #
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.
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
x_train=np.empty([256,256,3])
for i in range(0,1000):
pic=pic.resize([256,256])
x_train2= np.asarray(pic)
x_train=np.stack((x_train,x_train2))
print(x_train.shape)
Thanks
REPLY
Jason Brownlee June 8, 2018 at 6:18 am #
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 #
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 #
×
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 #
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
You’re welcome!
REPLY
Haotian Wang October 8, 2018 at 5:52 am #
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 #
REPLY
Maijama'a October 27, 2018 at 9:37 pm #
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.
REPLY
Jason Brownlee January 29, 2019 at 11:39 am #
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 #
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,
REPLY
Jason Brownlee February 9, 2019 at 5:52 am #
thanks Jason will this apply to CNN model as well as this for LSTM ryt ?
Email Address
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!
REPLY
Jason Brownlee April 10, 2019 at 6:11 am #
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
REPLY
Jason Brownlee April 14, 2019 at 5:44 am #
REPLY
Nastaran April 26, 2019 at 11:26 am #
Hi,
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 #
REPLY
Sandeep Dawre June 5, 2019 at 7:51 pm #
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 #
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, 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 #
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 #
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.
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 #
REPLY
shadrack kodondi October 17, 2019 at 1:37 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 #
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])
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])
Thank you
Anthony of Sydney
REPLY
Jason Brownlee November 16, 2019 at 7:17 am #
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.
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.
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 #
Dear Dr Adrian,
I came across an array splitting with four parameters:
I understand that it substitutes a 0 at particular location
Thank you,
Anthony of Sydneyhank you,
Start Machine Learning
Anthony of Sydney
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[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]])
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
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
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 #
REPLY
Iyanda Taofeek December 13, 2019 at 10:06 am #
REPLY
Jason Brownlee December 13, 2019 at 1:42 pm #
You can use the reshape() function to specify the dimensions of existing numpy array.
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 #
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 #
REPLY
Jason Brownlee February 28, 2020 at 1:24 pm #
Thanks!
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.
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
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?
Thanks, fixed!
REPLY
mm October 16, 2020 at 10:35 am #
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
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)
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
How to Index, Slice and Reshape NumPy Arrays for Machine Learning
Email Address
How to Calculate the SVD from Scratch with Python START MY EMAIL COURSE
The Linear Algebra for Machine Learning EBook is where you'll find the Really Good stuff.
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ 31/31