Difference between revisions of "Python-3.4.3/C2/Getting-started-with-arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 21: Line 21:
 
* Perform basic '''array''' operations  
 
* Perform basic '''array''' operations  
 
* Create an '''identity matrix '''and
 
* Create an '''identity matrix '''and
* Use the method '''zeros'''
+
* Use the '''method zeros'''
 
+
 
+
  
 
|-
 
|-
Line 34: Line 32:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 50: Line 46:
  
 
Overview of '''array'''  
 
Overview of '''array'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| '''Arrays''' are '''homogeneous''' '''data structures'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| '''Arrays''' are '''homogeneous data structures'''.  
  
  
All elements in it must be of same '''data type.'''
+
All '''elements''' in it must be of same '''data type.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| In this tutorial, we will be using''' numpy''' library which we used in earlier tutorial.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| In this tutorial, we will be using''' numpy library''' which we used in earlier tutorial.
  
  
Line 69: Line 65:
  
  
We can see the '''ipython''' prompt.
+
We can see the '''ipython prompt'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, import numpy as np
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, import numpy as np
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will '''import''' '''numpy'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will '''import numpy'''.
  
  
Line 96: Line 92:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a1 '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''a1 '''
  
  
Notice that we have created a one dimensional '''array''' here.  
+
Notice that we have created a '''one dimensional array''' here.  
  
  
Line 114: Line 110:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see how to create two dimensional array.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see how to create '''two dimensional array'''.
  
  
Line 129: Line 125:
  
  
This is our 2-dimensional array.
+
This is our '''2-dimensional array'''.
  
  
Line 139: Line 135:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see about '''arange''' method.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see about '''arange method'''.
  
  
To arrange the elements in an array we use '''arange''' method.
+
To arrange the '''elements''' in an '''array''' we use '''arange method.'''
  
  
Line 149: Line 145:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''ar = np.arange(1,9)'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''ar = np.arange(1,9)'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''ar''''' is equal to'' '''np''' ''dot '''''arange''' ''inside parentheses'' 1 ''comma'' 9.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''ar''''' is equal to'' '''np''' ''dot '''''arange''' ''inside parentheses'' 1 ''comma'' 9.
  
 
|-
 
|-
Line 156: Line 152:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''print''' ''inside parentheses'' '''ar'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''print''' ''inside parentheses'' '''ar'''
  
  
Line 162: Line 158:
  
  
As you can see, we obtained a one dimensional array between 1 and 9 with 1 included and 9 excluded.
+
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.
+
It will give the '''elements''' one less than the stop value.
  
  
Line 172: Line 168:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Can we make a two dimensional '''array''' of order 2 by 4?  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Can we make a '''two dimensional array''' of order 2 by 4?  
  
  
Line 191: Line 187:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch to terminal
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch to terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the terminal.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the '''terminal'''.
  
 
|-
 
|-
Line 199: Line 195:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| type''' ar'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| type''' ar'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| type''' ar'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type''' ar'''
  
  
Shape of the original array '''ar '''is not changed.
+
Shape of the original '''array ar '''is not changed.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''ar.shape = (2,4)'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''ar.shape = (2,4)'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| If you want to change the shape of the original array, type
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| If you want to change the shape of the original '''array''', type
  
 
'''ar '''''dot '''''shape '''''is equal to''''' '''''inside parentheses'' '''2''''' comma'' '''4'''.
 
'''ar '''''dot '''''shape '''''is equal to''''' '''''inside parentheses'' '''2''''' comma'' '''4'''.
Line 215: Line 211:
  
  
We can see that the shape of the original array '''ar''' is changed now.
+
We can see that the shape of the original '''array ar''' is changed now.
  
 
|-
 
|-
Line 224: Line 220:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To find the shape of an '''array''' we can use the '''method''' '''shape.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To find the shape of an '''array''' we can use the '''method shape.'''
  
  
Line 230: Line 226:
  
  
A '''tuple''' is nothing but an ordered '''list''' of elements.  
+
A '''tuple''' is nothing but an '''ordered list''' of '''elements'''.  
  
 
|-
 
|-
Line 257: Line 253:
  
  
Find out the shape of the '''arrays''' '''a1 '''and''' ar '''which we have created earlier in this tutorial.
+
Find out the shape of the '''arrays a1 '''and''' ar '''which we have created earlier in this tutorial.
  
 
|-
 
|-
Line 267: Line 263:
  
  
Since '''a1''' is a single dimensional array, the column is empty.
+
Since '''a1''' is a '''single dimensional array''', the '''column''' is empty.
  
 
|-
 
|-
Line 274: Line 270:
  
  
'''ar''' is a two dimensional array.
+
'''ar''' is a '''two dimensional array'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a3 = np.array<nowiki>([1,2,3,'a string'])</nowiki>'''  
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a3 = np.array<nowiki>([1,2,3,'a string'])</nowiki>'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us try to create a new '''array''' with elements of different datatypes.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| 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 '''''is equal to''''' np '''''dot '''''array '''''inside parentheses inside square brackets '''''1 '''''comma '''''2 '''''comma '''''3 '''''comma inside single quotes '''''a string '''
  
 
|-
 
|-
Line 288: Line 284:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| '''Arrays''' handle elements with the same datatype.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| '''Arrays''' handle '''elements''' with the same '''datatype'''.
  
  
Here we are handling with different data types. So it should give us error.  
+
Here we are handling with different '''datatypes'''. So it should give us error.  
  
  
Line 300: Line 296:
  
  
Because all the elements get implicitly converted as '''strings'''.  
+
Because all the '''elements''' get implicitly converted as '''strings'''.  
  
 
|-
 
|-
Line 307: Line 303:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| This is how array works.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| This is how '''array''' works.
  
  
Line 316: Line 312:
  
  
The characters of '''dtype''' i.e. '''<nowiki>’<U21' </nowiki>'''might differ with python version.
+
The characters of '''dtype''' i.e. '''<nowiki>’<U21' </nowiki>'''might differ with '''python''' version.
  
 
|-
 
|-
Line 325: Line 321:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see about '''identity''' matrix.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| 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.
+
It is a '''square matrix''' of order (n,n) with ones on the '''main diagonal''' and all other '''elements''' as zeros.
  
  
Line 341: Line 337:
  
  
We can see all ones in the main diagonal as expected.
+
We can see all ones in the '''main diagonal''' as expected.
  
 
|-
 
|-
Line 350: Line 346:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next is '''Zeros''' method.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next is '''Zeros method'''.
  
  
It creates an '''m by n '''matrix with all elements as 0.
+
It creates an '''m by n '''matrix with all '''elements''' as 0.
  
  
Line 362: Line 358:
  
 
'''np.zeros((4,5))'''  
 
'''np.zeros((4,5))'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us create an array of the shape (4, 5) with all the elements zero.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us create an '''array''' of the shape (4, 5) with all the '''elements zero'''.
  
  
Line 369: Line 365:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| But box on the arrays iden:  
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| But box on the arrays iden:  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The default output of '''identity''' and '''zeros method '''are in '''float''' datatype.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The default output of '''identity''' and '''zeros method '''are in '''float datatype'''.
  
 
|-
 
|-
Line 378: Line 374:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Explore the below functions on your own:
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Explore the below '''functions''' on your own:
 
+
 
+
* zeros_like
+
* ones
+
* ones_like
+
 
+
  
 +
* '''zeros_like'''
 +
* '''ones'''
 +
* '''ones_like'''
  
 
|-
 
|-
Line 398: Line 391:
  
  
We see that '''a1''' is a single dimensional '''array.'''
+
We see that '''a1''' is a '''single dimensional array.'''
  
 
|-
 
|-
Line 407: Line 400:
  
  
It returned a new '''array''' with all the elements multiplied by 2.
+
It returned a new '''array''' with all the '''elements''' multiplied by 2.
  
 
|-
 
|-
Line 424: Line 417:
  
  
It returns a new '''array''', with all the elements summed with two.  
+
It returns a new '''array''', with all the '''elements''' summed with two.  
  
 
|-
 
|-
Line 449: Line 442:
  
  
Next, we will see how to add two arrays.
+
Next, we will see how to add two '''arrays'''.
  
  
Line 456: Line 449:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''<nowiki>a2 = np.array([5, 6, 7, 8])</nowiki>'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''<nowiki>a2 = np.array([5, 6, 7, 8])</nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a2 '''''is equal to''''' np '''''dot '''''array '''''inside parentheses inside square brackets '''''5, 6, 7, 8'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''a2 '''''is equal to''''' np '''''dot '''''array '''''inside parentheses inside square brackets '''''5, 6, 7, 8'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a1 + a2'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a1 + a2'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a1 '''''plus''''' a2'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''a1 '''''plus''''' a2'''
  
  
This returns an '''array''' by adding element by element'''.'''
+
This returns an '''array''' by adding '''element''' by '''element'''.
  
 
|-
 
|-
Line 470: Line 463:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''a1 '''''multiplied by''''' a2'''  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''a1 '''''multiplied by''''' a2'''  
  
  
It returns an '''array''' with element by element multiplication.
+
It returns an '''array''' with '''element''' by '''element''' multiplication.
  
 
|-
 
|-
Line 487: Line 480:
 
In this tutorial, we have learnt to,  
 
In this tutorial, we have learnt to,  
  
# Create an '''array''' using the '''array'''() function.  
+
# Create an '''array''' using the '''array() function'''.  
 
# Perform some basic operations on '''arrays''' like addition and multiplication.  
 
# Perform some basic operations on '''arrays''' like addition and multiplication.  
 
# Use methods like -  
 
# Use methods like -  
  
'''shapearange'''
+
'''shapearange, reshapeidentity '''and'''zeros'''
 
+
'''reshapeidentity '''and'''zeros'''
+
  
 
|-
 
|-
Line 504: Line 495:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
  
'''x '''''is equal to''''' np.array '''''inside parentheses inside square brackets''' ''1, 2, 3, '''''inside square brackets''' ''5, 6, 7''' is a valid statement ?
+
'''x '''''is equal to''''' np.array '''''inside parentheses inside square brackets''' ''1, 2, 3, '''''inside square brackets''' ''5, 6, 7''' is a valid statement?
  
 
*True  
 
*True  
 
*False  
 
*False  
 
 
  
 
|-
 
|-
Line 521: Line 510:
  
  
The correct way is to assign the elements as a '''list''' of '''lists''' and then convert it to an '''array.'''  
+
The correct way is to assign the '''elements''' as a '''list''' of '''lists''' and then convert it to an '''array.'''  
  
  

Latest revision as of 21:46, 6 May 2018

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.

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 datatypes. 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 inside parentheses 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 inside parentheses inside parentheses 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 -

shapearange, reshapeidentity andzeros

Show Slide

Self assessment questions slide


Here is a self assessment question for you to solve

x is equal to np.array inside parentheses inside square brackets 1, 2, 3, inside square brackets 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 is equal to np.array inside parentheses inside square brackets again inside square brackets 1, 2, 3, inside square brackets 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


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 watching.

Contributors and Content Editors

Nancyvarkey, Priyacst