Skip to main content

Lists, Arrays and Matrices and their Visualization


What You Will Learn

  • You will create lists, arrays and matrices using scipy
  • You will learn to slice, reverse, append and plot arrays

Lists in Python

Type in the code at the Ipython console
  • from scipy import *
  • x=[1,2,3,4,5]
  • The first line imports all modules in scipy. The second line creates a list in the variable x.
  • Type x at the console and observe the list
  • Type x[0:2] at the console and observe the first two values in the list
  • Type x[3:5] at the console and observe a slice of the list
  • Type x[::-1] at the console and observe the reversed list
  • Type len(x) at the console and observe the length of the list x
  • Type y=[4,5,6] and then type x+y observe the two lists getting concatenated.
  • Type append(x,y) observe a similar result
  • Type z=['apple','orange','pear'] to create a list of strings

Arrays using Scipy

scipy and numpy provide arrays that support mathematical operations.

  • Type x=array([1,2,3,4,5]) at the console and observe the array
  • Type x[0:2] at the console and observe the first two values in the array
  • Type x[3:5] at the console and observe a slice of the array
  • Type x[::-1] at the console and observe the reversed array
  • Type len(x) at the console and observe the length of the array x
  • Type y=[4,5,6,5,7] and then type x+y observe the point wise sum of the two arrays.
  • Type append(x,y) observe the concatenated array
  • Type concatenate(x,y) and observe a similar result.
  • Type x*y and observe the point wise multiplication
  • Type sum(x*y) and observe the dot product between x and y

Plots of Arrays

Matplotlib is an excellent tool for visualizing two and three dimensional data. It imported as
from pylab import *
Stem plot of x can be made as
from scipy import *
from pylab import *
x=array([1,2,3,4,5])
y=array([2.4,4.6,3.765,8.7,5.4])
stem(range(0,len(x)),x,'b')
stem(range(0,len(y)),y,'r')
grid('on')
savefig('figure.pdf')
show()
The first two lines import all modules in scipy and pylab. Lines 3 and 4 creates two arrays of real numbers. Lines 5 and 6 create the stem plots of x and y in blue and red colors respectively. The x axis if formed by integers from 0 to length of x and y respectively. The grid command places a grid on the GUI. savefig commands saves the pot as pdf (or .eps) file in the present working directory. Replace stem by plot to observe continuous plots of x and y. See the stem plots in Fig. 1.
Fig.1 - Discrete plots of one dimensional arrays using matplotlib

Two Dimensional Arrays and Matrices

Two dimensional array (also called image) is created as
from scipy import *
from pylab import *
x=array([[1,2,3],[4,5,6],[7,8,9]])
Type x and observe the two dimensional array. The array can be visualized as an image by
imshow(x)
show()
The two dimensional representation od the array is shown as Fig. 2.
Fig.2 - Image of the 2-D array

The above two dimensional array can be made into a matrix as

x=mat([[1,2,3],[4,5,6],[7,8,9]])
It can be visualized by
matshow(x)
show()
The plot looks like what we had in Fig. 2.

What You Learned

  • You created lists, arrays and matrices using scipy
  • You learned to slice, reverse, append and plot arrays and matrices

Comments

Popular posts from this blog

Functions in Python

Functions in Python Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You will Learn You will learn the syntax of Python functions You will learn to import the functions to other codes The Syntax The syntax used the keyword $def$ followed the name of the function again followed by a colon (:) that is then followed by an indented code block of instructions. The function def function_name(arg1.arg2,....argN): statements return results A Simple Python Function Needless to say that the statements and the return command should form an indented block. Consider a simple function that returns the sum of the input values. It is realized as the function below. def summ(x,y): return x+y Unlike MATLAB or IDL, it is possible to execute a function in the Python shell. You may type the above function in a Python shell or an enhanced Python shell such as IPython. Once the ...

Ex:2 Familiarization of Scientific Computing

Iterations and Conditions in Python

Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You Will Learn You will learn the syntax of for, while loops and if statement in Python You will apply these to solve a mathematical problem. For loop Python identifies code blocks by indentation. The for loop has the syntax shown below. Observe the colon at the end of the first line and the indentation at the start of the second line. Whatever is to happen under the for loop should be given as an indented block. If the indentation is wrong, it raises an exception. for i in range(0,10): print i The above code prints the integers from 0 to 9. One can modify the above code for appending the values to a blank array as shown in the following code. from scipy import * arr=[] for i in range(0,10): arr.append(i) print arr The loops being slow, the above method is not encouraged. Instead, vectorized computation is used as shown below. from scipy impor...