Difference between revisions of "Python-3.4.3/C2/Getting-started-with-arrays/English"
Nancyvarkey (Talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 21: | Line 21: | ||
* Perform basic '''array''' operations | * Perform basic '''array''' operations | ||
* Create an '''identity matrix '''and | * Create an '''identity matrix '''and | ||
− | * Use the | + | * 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 | + | | 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''' | + | | 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''' | + | 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 | + | | 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 | + | | 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 | + | Notice that we have created a '''one dimensional array''' here. |
− | Also notice that the object we passed to create an '''array '''is a '''list''' | + | Also notice that the object we passed to create an '''array '''is a '''list,''' i.e '''a1.''' |
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'''. |
+ | |||
'''<nowiki><<</nowiki>'''PAUSE>> | '''<nowiki><<</nowiki>'''PAUSE>> | ||
Line 138: | 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''' | + | | 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.''' | ||
− | |||
− | + | The syntax is shown here. | |
|- | |- | ||
| 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 | + | | 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 157: | 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 | + | | 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 163: | 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. |
+ | |||
'''<nowiki><<</nowiki>'''PAUSE>> | '''<nowiki><<</nowiki>'''PAUSE>> | ||
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 | + | | 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;"| | + | | 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 | + | 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 | + | 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 | + | | 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 | + | A '''tuple''' is nothing but an '''ordered list''' of '''elements'''. |
|- | |- | ||
Line 257: | Line 253: | ||
− | Find out the shape of the '''arrays | + | 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 | + | 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 | + | 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''' | + | | 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. |
− | The syntax is '''identity | + | The syntax is '''identity '''''inside parentheses''' ''n.''' |
|- | |- | ||
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''' | + | | 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. |
− | The Syntax is: '''zeros | + | The Syntax is: '''zeros '''''inside parentheses inside parentheses''' ''m, n''' |
|- | |- | ||
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''' | + | | 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''' | ||
|- | |- | ||
Line 398: | Line 391: | ||
− | We see that '''a1''' is a | + | 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 | + | | 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 | + | | 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 | + | 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 | + | | 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 | + | # 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, reshapeidentity '''and'''zeros''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 505: | 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? |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | *True | ||
+ | *False | ||
|- | |- | ||
Line 523: | 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.''' |
− | That is ''' | + | 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''' |
|- | |- | ||
Line 534: | Line 521: | ||
|- | |- | ||
| 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;"| Show Slide Fossee Forum | | 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;"| Show Slide Fossee Forum | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Please post your general queries on Python in this forum. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Please post your general queries on '''Python''' in this forum. |
|- | |- | ||
Line 545: | Line 532: | ||
Acknowledgment | Acknowledgment | ||
− | + | ||
+ | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. | ||
Line 553: | Line 541: | ||
|- | |- | ||
| 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;"| Previous slide | | 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;"| Previous slide | ||
− | | 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 Priya from IIT Bombay signing off. Thanks for | + | | 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 Priya from IIT Bombay signing off. Thanks for watching. |
|} | |} |
Latest revision as of 21:46, 6 May 2018
|
|
Show Slide | Welcome to the spoken tutorial on Getting started with arrays. |
Show Slide
Learning objectives
|
In this tutorial, you will learn to,
|
Show Slide
System Requirements |
To record this tutorial, I am using
|
Show Slide
Pre-requisite slide |
To practice this tutorial, you should know how to use Lists.
|
Show Slide
Overview of array |
Arrays are homogeneous data structures.
|
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.
|
type python3
Highlight ipython prompt |
Let us start ipython by typing ipython3 and press Enter.
|
Type, import numpy as np | Now we will import numpy.
|
Type, a1 = np.array([1,2,3,4])
|
Now let us see how to create arrays.
|
Type a1
|
Type a1
|
Type
a2 = np.array([[1,2,3,4],[5,6,7,8]])
|
Next we will see how to create two dimensional 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
|
slide : arange method
|
Next we will see about arange method.
|
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
|
Can we make a two dimensional array of order 2 by 4?
| |
slide reshape method
|
We will use reshape method to change the shape of an array.
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
|
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
|
Show Slide
shape of an array
|
To find the shape of an array we can use the method shape.
|
Type
a2.shape
|
Let us check the shape of the arrays we have created so far.
|
Show Slide
Assignment 1
|
Pause the video here, try out the following exercise and resume the video.
|
Type a1.shape | Let us see the solution.
|
Type, ar.shape | Type, ar dot shape
|
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
|
Arrays handle elements with the same datatype.
|
Highlight dtype='<U21')
|
This is how array works.
|
Show Slide
identity(n) method
|
Next we will see about identity matrix.
|
type np.identity(2) | Let us see how to create a 2 by 2 identity matrix.
|
Show Slide
zeros(shape) method
|
Next is Zeros method.
|
type
np.zeros((4,5)) |
Let us create an array of the shape (4, 5) with all the elements zero.
|
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:
|
type, a1 | Try the following.
|
type
a1 * 2 |
Let us now try a1 multiplied by 2
|
Type a1 | Now let us again check the contents of a1.
|
Type a1 + 2 | Similarly we will try with addition.
|
Type a1 | Type a1
|
Type a1 += 2 | Let us try with a1 plus equal to 2 |
Type a1 | Type, a1
|
Type a1 = np.array([1, 2, 3, 4]) | We can use all the mathematical operations with arrays.
|
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
|
Type, a1 * a2
|
Type a1 multiplied by a2
|
Show Slide
Summary slide
|
This brings us to the end of the end of this tutorial.
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?
|
Show Slide
Solution of self assessment questions on slide
|
And the answer is False.
|
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.
|
Previous slide | This is Priya from IIT Bombay signing off. Thanks for watching. |