author | Puneeth Chaganti <punchagan@gmail.com> |
Tue, 13 Apr 2010 14:26:52 +0530 | |
changeset 52 | 53700ad0e71e |
parent 40 | 6e3526275a00 |
child 54 | 46a3575919f5 |
permissions | -rw-r--r-- |
40
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
1 |
Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
2 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
3 |
In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
4 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
5 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
6 |
Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
7 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
8 |
On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 ,close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
9 |
Now we will try to create a multi-dimensional array type in your ipython terminal |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
10 |
c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we will do c.shape . We can see that c is a 3 by 3 matrix . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
11 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
12 |
There are other special methods of creating arrays as well we will now look at them . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
13 |
The first one is the command arange which is similar to range except that it returns an array. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
14 |
We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
15 |
Ones can be use to get all entries as ones . We can pass it the shape of the array as required . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
16 |
type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
17 |
To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
18 |
command. type b= ones_like in parenthesis a . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
19 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
20 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
21 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
22 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
23 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
24 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
25 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
26 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
27 |