Python-3.4.3/C2/Getting-started-with-arrays/English

From Script | Spoken-Tutorial
Revision as of 15:53, 3 May 2018 by Priyacst (Talk | contribs)

Jump to: navigation, search
Visual Cue
Narration
Show Slide Welcome to the spoken tutorial on Getting started with arrays.
Show Slide

Learning objectives


In this tutorial, you will learn to,
  • Create arrays using data
  • Create arrays from lists
  • Perform basic array operations
  • Create an identity matrix and
  • Use the method zeros


Show Slide

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0


Show Slide

Pre-requisite slide

To practice this tutorial, you should know how to use Lists.


If not, see the relevant Python tutorials on this website.

Show Slide

Overview of array

Arrays are homogeneous data structures.


All elements in it must be of same data type.

open the Terminal by pressing Ctrl+Alt+T keys simultaneously In this tutorial, we will be using numpy library which we used in earlier tutorial.


Let us first open the terminal by pressing Ctrl+Alt+T keys simultaneously.

type python3

Highlight ipython prompt

Let us start ipython by typing ipython3 and press Enter.


We can see the ipython prompt.

Type, import numpy as np Now we will import numpy.


Type, import numpy as np and press Enter.

Type, a1 = np.array([1,2,3,4])


Now let us see how to create arrays.


From here onwards, please remember to press the Enter key after typing every command on the terminal.


Type, a1 is equal to np dot array inside parentheses inside square brackets 1 comma 2 comma 3 comma 4

Type a1


Type, a1


Notice that we have created a one dimensional array here.


Also notice that the object we passed to create an array is a list. i.e a1.


<<PAUSE>>

Type

a2 = np.array([[1,2,3,4],[5,6,7,8]])


Next we will see how to create two dimensional array.


Two dimensional array is created by converting a list of lists to an array.

Type, a2 is equal to np dot array inside parentheses inside square brackets again inside square brackets 1 comma 2 comma 3 comma 4 comma inside square brackets 5 comma 6 comma 7 comma 8

Type a2


Type a2


This is our 2-dimensional array.

<<PAUSE>>

slide : arange method


Next we will see about arange method.


To arrange the elements in an array we use arange method.


The syntax is shown here:

arange([start,] stop, [step,] dtype=None)

Type, ar = np.arange(1,9) Type, ar is equal to np dot arange inside parentheses 1 comma 9.
type print (ar)


Type, print inside parentheses ar


Here, 1 is the start value and 9 is the stop value.


As you can see, we obtained a one dimensional array between 1 and 9 with 1 included and 9 excluded.


It will give the elements one less than the stop value.

<<PAUSE>>

Can we make a two dimensional array of order 2 by 4?


Yes, we can do it.

slide reshape method


object.reshape(rows, columns)

We will use reshape method to change the shape of an array.


The syntax is:

object.reshape inside parentheses rows comma columns

Switch to terminal Switch back to the terminal.
Type, br = ar.reshape(2,4) Type, ar dot reshape inside parentheses 2 comma 4.
type ar type ar


Shape of the original array ar is not changed.

Type, ar.shape = (2,4) If you want to change the shape of the original array, type

ar dot shape is equal to inside parentheses 2 comma 4.

Type ar Type ar


We can see that the shape of the original array ar is changed now.

Show Slide

shape of an array


To find the shape of an array we can use the method shape.


It returns a tuple of the shape of an array.


A tuple is nothing but an ordered list of elements.

Type

a2.shape


Let us check the shape of the arrays we have created so far.


Type a2 dot shape


a2.shape object is a tuple, and it returned a tuple (2, 4).

Show Slide

Assignment 1


Pause the video here, try out the following exercise and resume the video.


Find out the shape of the arrays a1 and ar which we have created earlier in this tutorial.

Type a1.shape Let us see the solution.


Type, a1 dot shape


Since a1 is a single dimensional array, the column is empty.

Type, ar.shape Type, ar dot shape


ar is a two dimensional array.

Type, a3 = np.array([1,2,3,'a string']) Now let us try to create a new array with elements of different datatypes.


Type, a3 is equal to np dot array inside parentheses inside square brackets 1 comma 2 comma 3 comma inside single quotes a string

Type a3


Arrays handle elements with the same datatype.


Here we are handling with different data types. So it should give us error.


Type a3


But we did not get any error.


Because all the elements get implicitly converted as strings.

Highlight dtype='<U21')


This is how array works.


Note that the output mentions dtype=’<U21'


A dtype is the datatype required to hold the objects in sequence.


The characters of dtype i.e. ’<U21' might differ with python version.

Show Slide

identity(n) method


Next we will see about identity matrix.


It is a square matrix of order (n,n) with ones on the main diagonal and all other elements as zeros.


The syntax is identity(n).

type np.identity(2) Let us see how to create a 2 by 2 identity matrix.


Type, np dot identity inside parentheses 2.


We can see all ones in the main diagonal as expected.

Show Slide

zeros(shape) method


Next is Zeros method.


It creates an m by n matrix with all elements as 0.


The Syntax is: zeros((m, n))

type

np.zeros((4,5))

Let us create an array of the shape (4, 5) with all the elements zero.


Type, np dot zeros inside parentheses inside parentheses 4 comma 5.

But box on the arrays iden: The default output of identity and zeros method are in float datatype.
Show Slide

Learning exercise


Explore the below functions on your own:


  • zeros_like
  • ones
  • ones_like


type, a1 Try the following.


First check the value of a1 which we assigned earlier.


Type a1


We see that a1 is a single dimensional array.

type

a1 * 2

Let us now try a1 multiplied by 2


It returned a new array with all the elements multiplied by 2.

Type a1 Now let us again check the contents of a1.


Note that the value of a1 still remains the same.

Type a1 + 2 Similarly we will try with addition.


Type, a1 plus 2


It returns a new array, with all the elements summed with two.

Type a1 Type a1


But again notice that the value of a1 has not been changed.

Type a1 += 2 Let us try with a1 plus equal to 2
Type a1 Type, a1


This will change the array a1 itself as we are assigning the new output to a1.

Type a1 = np.array([1, 2, 3, 4]) We can use all the mathematical operations with arrays.


Next, we will see how to add two arrays.


Type, a1 is equal to np dot array inside parentheses inside square brackets 1, 2, 3, 4

Type, a2 = np.array([5, 6, 7, 8]) Type, a2 is equal to np dot array inside parentheses inside square brackets 5, 6, 7, 8
Type, a1 + a2 Type, a1 plus a2


This returns an array by adding element by element.

Type, a1 * a2


Type, a1 multiplied by a2


It returns an array with element by element multiplication.

Show Slide

Summary slide


This brings us to the end of the end of this tutorial.


In this tutorial, we have learnt to,

  1. Create an array using the array() function.
  2. Perform some basic operations on arrays like addition and multiplication.
  3. Use methods like -
  • shape
  • arange
  • reshape
  • identity
  • zeros
Show Slide

Self assessment questions slide


Here is a self assessment question for you to solve

x = np.array([1, 2, 3], [5, 6, 7]) is a valid statement ?


  • True
  • False


Show Slide

Solution of self assessment questions on slide


And the answer is False.


The correct way is to assign the elements as a list of lists and then convert it to an array.


That is x = np.array([[1, 2, 3], [5, 6, 7]])

Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.


For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst