Python/C3/Getting-started-with-arrays/English

From Script | Spoken-Tutorial
Revision as of 12:38, 2 December 2012 by Pravin1389 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello friends and welcome to the spoken tutorial on 'Getting started with arrays'.
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Create arrays using data.
  2. Create arrays from lists.
  3. Perform basic array operations.
  4. Create identity matrix.
  5. Use functions zeros(), zeros_like(), ones(), ones_like().


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
Show Slide 4

Overview of array

Arrays are homogeneous data structures. Unlike lists, arrays cannot have heterogeneous data elements. They can have only one type of data as their entries, be them all integers, strings, or maybe floats, but not a mix.

Arrays of a given length are comparatively much faster in mathematical operations than lists of the same length, because of the fact that they are homogeneous data structures.

Now let us see how to create arrays.

Open the terminal and run the following command
ipython -pylab
Run your IPython interpreter with -pylab option, to load the required modules to work with arrays.
a1 = array([1,2,3,4]) To create an array we will use the function array() as,
a2 = array([[1,2,3,4],[5,6,7,8]]) Notice that we created a one dimensional array here. Also notice that the object we passed to create an array is a list.

Now let us see how to create a two dimensional array.

We create two dimensional array by converting a list of lists to an array as,

ar = arange(1,9)
print ar
Now let us use arange() function to create the same array as before.
ar.reshape(2,4)
ar.reshape(4,2)
ar = ar.reshape(2,4)
As you can see, we obtained a one dimensional array with elements from 1 to 8.

Now can we make it a two dimensional array of order 2 by 4? Yes,we can.For this we will have to use the function reshape(),

l1 = [1,2,3,4] Hence,we got our two-dimensional array.

Now, let us see how to convert a list object to an array. We define a list,say l1

a3 = array(l1) Now to convert this list to an array,we use the array function as,
Show Slide 5

Shape of an array

To find the shape of an array we can use the method .shape, let us check the shape of the arrays we have created so far,
Switch to the terminal
a2.shape
a2.shape object is a tuple, and it returned a tuple (2, 4).A tuple is nothing but an ordered list of elements.

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

Show Slide 6

Assignment 1

Find out the shape of the other arrays i.e. a1, a3, ar that we have created.
Continue from paused state Switch to the terminal
a1.shape
a3.shape
ar.shape
Switch to the terminal for solution
a4 = array([1,2,3,'a string']) Now let us try to create a new array with a mix of elements and see what will happen,
a4

Highlight all the array elements one by one using mouse movements accordingly

Well, we would expect an error as it has been previously mentioned that arrays handle elements with the same datatype, but it didn't raise an error. Let us check the values in the new array created. Type a4 in the terminal,
Did you notice it, all the elements have been implicitly type casted as strings, though our first three elements were meant to be integers. Also,if you have noticed,we got something like 'dtype S8' in the output. dtype is nothing but the datatype which is the minimum type required to hold the objects in the sequence.
Show Slide 7

Identity & zeros methods

Let us now move on to study functions like zeros() and ones(). For this ,we will have to create a matrix. let us see how to create an identity matrix of a given size, that is a two-dimensional array in which all the diagonal elements are ones and rest of the elements are zeros. We can create an identity matrix using the function identity().

The function identity() takes an integer argument which specifies the size of the desired matrix,

Switch to the terminal
identity(3)
As you can see the identity function returned a three by three square matrix with all the diagonal elements as one and the rest of the elements as zeros.

zeros() function accepts a tuple, which is the order of the array that we want to create, and it generates an array with all elements as zeros.

Let us create an array of the order four by five with all the elements zero. We can do it using the method zeros(),

zeros((4,5)) Notice that we passed a tuple to the function zeros. Pause the video here, try out the following exercise and resume the video.
Show Slide 8

Learning Assignment

Find out about the functions
    • zeros_like()
    • ones()
    • ones_like()

< pause for some time and then continue >

a1 Try the following, first check the value of a1,
a1 * 2 We see that a1 is a single dimensional array, Let us now try a1*2
a1 It returned a new array with all the elements multiplied by 2. Now let us again check the contents of a1
a1 + 2
a1
note that the value of a1 still remains the same.

Similarly with addition,

a1 += 2 it returns a new array, with all the elements summed with two. But again notice that the value of a1 has not been changed.

You may change the value of a1 by simply assigning the newly returned array as,

a Notice the change in elements of a by typing 'a'
a1 = array([1,2,3,4])
a2 = array([1,2,3,4])
a1 + a2
We can use all the mathematical operations with arrays, Now let us try this
a1 * a2 This returns an array with element by element addition
a1*a2 returns an array with element by element multiplication, notice that it does not perform matrix multiplication.
Show Slide 9

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. Convert a list to an array.
  3. Perform some basic operations on arrays like addition,multiplication.
  4. Use functions like - .shape - arrange() - .reshape - zeros() & zeros_like() - ones() & ones_like()


Show Slide 10

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. x = array([1, 2, 3], [5, 6, 7]) is a valid statement
    • True
    • False
  1. What does the ones_like() function do?
    1. Returns an array of ones with the same shape and type as a given array.
    2. Return a new array of given shape and type, filled with ones.Read the statements and answer,
    • Only statement A is correct.
    • Only statement B is correct.
    • Both statement A and B are correct.
    • Both statement A and B are incorrect.


Show Slide 11

Solution of self assessment questions on slide

And the answers,
  1. False. The correct way would be to assign the elements as a list of lists and then convert it to an array

Enumerated list ends without a blank line; unexpected unindent.

x = array([[1, 2, 3], [5, 6, 7]])
  1. The function ones_like() returns an array of ones with the same shape and type as a given array.


Show Slide 12

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika, Pravin1389