diff options
author | Puneeth Chaganti | 2010-12-01 16:51:35 +0530 |
---|---|---|
committer | Puneeth Chaganti | 2010-12-01 16:51:35 +0530 |
commit | f3a34dfb4e879f3eb7274704f44546aac4add88f (patch) | |
tree | 1cb0a8cc5dbd5ee2b374350915ed2addfa0fb447 /getting_started_with_arrays | |
parent | 347866ed0d29db61ee062563b1e1616cfb85588c (diff) | |
download | st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.gz st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.bz2 st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.zip |
Renamed all LOs to match with their names in progress.org.
Diffstat (limited to 'getting_started_with_arrays')
-rw-r--r-- | getting_started_with_arrays/quickref.tex | 14 | ||||
-rw-r--r-- | getting_started_with_arrays/script.rst | 343 | ||||
-rw-r--r-- | getting_started_with_arrays/slides.org | 135 | ||||
-rw-r--r-- | getting_started_with_arrays/slides.tex | 284 |
4 files changed, 776 insertions, 0 deletions
diff --git a/getting_started_with_arrays/quickref.tex b/getting_started_with_arrays/quickref.tex new file mode 100644 index 0000000..10714c4 --- /dev/null +++ b/getting_started_with_arrays/quickref.tex @@ -0,0 +1,14 @@ +Creating an array:\\ +{\ex \lstinline| a = array([[1,2,3,4],[5,6,7,8]])|} + +Finding shape of array:\\ +{\ex \lstinline| a.shape|} + +Reshape an array:\\ +{\ex \lstinline| a.reshape(4,2)|} + +Creating identity matrix:\\ +{\ex \lstinline| identity(3)|} + +Creating matrix with all zeros:\\ +{\ex \lstinline| z = zeros((4,2))|} diff --git a/getting_started_with_arrays/script.rst b/getting_started_with_arrays/script.rst new file mode 100644 index 0000000..65a9f93 --- /dev/null +++ b/getting_started_with_arrays/script.rst @@ -0,0 +1,343 @@ +.. Objectives +.. ---------- + +.. At the end of this tutorial, you will be able to + +.. 1. Create arrays using data +.. #. Create arrays from lists +.. #. Basic array operations +.. #. Creating identity matrix using ``identity()`` function. +.. #. Learn about ``zeros()``, ``zeros_like()``, ``ones()``, + ``ones_like()`` functions. + +.. Prerequisites +.. ------------- + +.. 1. should have ``ipython`` and ``pylab`` installed. +.. #. getting started with ``ipython``. +.. #. getting started with lists. + +.. Author: Anoop Jacob Thomas <anoop@fossee.in> + Internal Reviewer : Puneeth + External Reviewer : + Language Reviewer : Bhanukiran + Checklist OK? : <11-11-2010,Anand, OK > [2010-10-05] + +=========================== +Getting started with Arrays +=========================== + +.. #[Puneeth: Prerequisites and Objectives are missing. Fill them in] + +{{{ show the welcome slide }}} + +Welcome to the spoken tutorial on getting started with arrays. + +{{{ switch to next slide, outline slide }}} + +In this tutorial, we will learn about the data structure called an array, how to convert +a list into an array, operations on arrays and also why an array is preferred +to lists. + +.. #[Puneeth: Fix the grammar above.] + +{{{ switch to next slide on overview of array }}} + +Arrays are homogeneous data structures. Unlike lists, arrays cannot have +heterogeneous data elements, that is, they can have only one type of data +as their entries, be them all integers, strings, or maybe floats, but not a mix. + +.. #[Puneeth: Use multiple short sentences, rather than one long sentence + I would've written something like this. + + Unlike lists, arrays are homogeneous data structures. They can have only + type of data, ....] + +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. + +.. #[Puneeth: For what size of an array is that the comparison? + +{{{ switch to the next slide, creating arrays }}} + +Now let us see how to create arrays. + +Run your IPython interpreter with ``-pylab`` option, to load the required +modules to work with arrays. +{{{ take terminal and run the following command }}} +:: + + ipython -pylab + +.. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it +.. is not open?] + +To create an array we will use the function ``array()`` as, + +:: + + a1 = array([1,2,3,4]) + +Notice that we created a one dimensional array here. Also notice the object +we passed to create an array. We passed a list to create an array. + +Now let us see how to create a two dimensional array. Pause here and try to +do it yourself before looking at the solution. + +{{{ switch to next slide, creating two dimensional arrays }}} + +.. #[Puneeth: I don't think this question can be solved by an average +.. viewer. Questions during the tutorial, should generally be to re-iterate +.. concepts learnt? ] + +.. #[Puneeth: Also, you didn't even point out that we are converting a +.. list, using the ``array`` function. Bring the later section about +.. converting a list, here. A separate section is not necessary, IMHO.] + +We create two dimensional array by converting a list of lists to an array +as, + +:: + + a2 = array([[1,2,3,4],[5,6,7,8]]) + +.. #[Puneeth: Again, you could explain a bit about the fact that we are +.. converting a list of lists.] + +Now let us use ``arange()`` function to create the same array as before. + +:: + + ar = arange(1,9) + +.. #[Puneeth: say, creating the same array as before. for some time I got +.. confused .] + +And we obtained a one dimensional array with elements from 1 to 8. + +:: + + print ar + +.. #[Puneeth: be consistent with voice. say, we obtained... or something.] + +And how can we make it a two dimensional array of order 2 by 4? Pause here +and try to do it yourself, try ``ar.tab`` and find a suitable method for +that. + +{{{ switch to next slide, reshape() method }}} + +We can use the function ``reshape()`` for that purpose and it can be done +as, + +:: + + ar.reshape(2,4) + ar.reshape(4,2) + ar = ar.reshape(2,4) + +{{{ switch to next slide, creating array from list}}} + +Now, let us see how to convert a list object to an array. As you have +already seen, in both of the previous statements we have passed a list, so +creating an array can be done so, first let us create a list ``l1`` + +:: + + l1 = [1,2,3,4] + +Now we can convert the list to an array as, + +:: + + a3 = array(l1) + + +{{{ switch to the next slide, problem statement of unsolved exercise 1 }}} + +Create a three dimensional array of the shape (2,2,4). + +.. #[Puneeth: s/order/shape or size ?] + +{{{ switch to the next slide, 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, + +.. #[Puneeth: s/object/method ?] + +:: + + a2.shape + +``a2.shape`` object is a tuple, and it returned a tuple (2, 4). + +.. #[Puneeth: first show a 2D array, so that it becomes easier to explain. +.. Also, the word ``tuple`` need not be mentioned. ] + +{{{ switch to the next slide, unsolved exercise 2 }}} + +Find out the shape of the other arrays that we have created. + +.. #[Puneeth: solution missing.] + +It can be done as, +:: + + a1.shape + a3.shape + ar.shape + +{{{ Array can have only a single type of data }}} + +.. #[Puneeth: I guess, this whole section can be skipped. If you want to +.. keep this, just briefly mention that arrays are homogeneous in the +.. intro, don't explain it there.] + +Now let us try to create a new array with a mix of elements and see what +will happen, + +:: + + a4 = array([1,2,3,'a string']) + +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. In your IPython terminal type, +:: + + a4 + +Did you notice it, + +{{{ switch to next slide, implicit type casting }}} + +.. #[Puneeth: typecasting may be unnecessary. (Also too advanced?) an +.. average guy wouldn't use arrays with strings.] + +.. #[Puneeth: You may want to mention that float is the default dtype.] + +{{{ highlight all the array elements one by one using mouse movements }}} + +all the elements have been implicitly type casted as strings, though our +first three elements were meant to be integers. + +.. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand +.. what it is, can you explain? ;)] + +{{{ switch to the next slide, identity & zeros methods }}} + +.. #[Puneeth: something needs to motivate this. why are we suddenly talking +.. of an identity matrix?] + +Now 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, + +:: + + identity(3) + +As you can see the identity function returned a three by three square matrix +with all the diagonal elements as ones and the rest of the elements as zeros. + +.. #[Puneeth: You say array here, matrix there -- it's a bit messed up. +.. Clarify, explicitly.] + +``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. + +{{{ switch to the next slide, problem statement of solved exercise 1 }}} + +Let us creates 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. + +{{{ switch to next slide, learning exercise }}} + +We learned two functions ``identity()`` and ``zeros()``, find out more +about the functions ``zeros_like()``, ``ones()``, ``ones_like()``. + +{{{ switch to next slide, array operations }}} + +Try the following, first check the value of a1, +:: + + a1 + +``a1`` is a single dimensional array, and now try, +:: + + a1 * 2 + +It returned a new array with all the elements multiplied by 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. +:: + + a1 + +You may change the value of a1 by simply assigning the newly returned +array as, +:: + + a1 += 2 + +Notice the change in elements of a, +:: + + a + +We can use all the mathematical operations with arrays, Now let us try this +:: + + a1 = array([1,2,3,4]) + a2 = array([1,2,3,4]) + a1 + a2 + +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. + +{{{ switch to next slide, summary slide }}} + +So this brings us to the end of this tutorial, in this tutorial we covered +basics of arrays, learned how to create an array, saw how to convert a list +to an array, and basic array operations etc. + +.. #[Puneeth: s/how to create an array/creating an array] + +{{{ switch to next slide, thank you }}} + +Thank you! + +.. + Local Variables: + mode: rst + indent-tabs-mode: nil + sentence-end-double-space: nil + fill-column: 75 + End: diff --git a/getting_started_with_arrays/slides.org b/getting_started_with_arrays/slides.org new file mode 100644 index 0000000..a5b315f --- /dev/null +++ b/getting_started_with_arrays/slides.org @@ -0,0 +1,135 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC + +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] + +#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} +#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} + +#+LaTeX_HEADER: \usepackage{listings} + +#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, +#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +#+TITLE: Getting started with arrays +#+AUTHOR: FOSSEE +#+EMAIL: info@fossee.in +#+DATE: + +#+DESCRIPTION: +#+KEYWORDS: +#+LANGUAGE: en +#+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t +#+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc + +* Outline + - Arrays + - why arrays over lists + - Creating arrays + - Array operations + +* Overview of Arrays + - Arrays are homogeneous data structures. + - elements have to the same data type + - Arrays are faster compared to lists + - at least /80-100 times/ faster than lists + +* Creating Arrays + - Creating a 1-dimensional array + : In []: a1 = array([1, 2, 3, 4]) + ~[1, 2, 3, 4]~ is a list. +* Creating two-dimensional array + - Creating a 2-dimensional array + : In []: a2 = array([[1,2,3,4],[5,6,7,8]]) + here we convert a list of lists to an array making a 2-d array. + - Using ~arange()~ function + : In []: ar = arange(1,9) +* ~reshape()~ method + - To reshape an array + : In []: ar.reshape(2, 4) + : In []: ar.reshape(4, 2) + : In []: ar = ar.reshape(2, 4) + +* Creating ~array~ from ~list~. + - ~array()~ method accepts list as argument + - Creating a list + : In []: l1 = [1, 2, 3, 4] + - Creating an array + : In []: a3 = array(l1) + +* Exercise 1 + Create a 3-dimensional array of the order (2, 2, 4). + +* ~.shape~ of array + - ~.shape~ + To find the shape of the array + : In []: a2.shape + - ~.shape~ + returns a tuple of shape +* Exercise 2 + Find out the shape of the other arrays(a1, a3, ar) that we have created. +* Homogeneous data + - All elements in array should be of same type + : In []: a4 = array([1,2,3,'a string']) +* Implicit type casting + : In []: a4 + All elements are type casted to string type +* ~identity()~, ~zeros()~ methods + - ~identity(n)~ + Creates an identity matrix, a square matrix of order (n, n) with diagonal elements 1 and others 0. + - ~zeros((m, n))~ + Creates an ~m X n~ matrix with all elements 0. + +* Learning exercise + - Find out about + - ~zeros_like()~ + - ~ones()~ + - ~ones_like()~ + +* Array operations + - ~a1 * 2~ + returns a new array with all elements of ~a1~ multiplied by ~2~. + - Similarly ~+~, ~-~ \& ~/~. + - ~a1 + 2~ + returns a new array with all elements of ~a1~ summed with ~2~. + - ~a1 += 2~ + adds ~2~ to all elements of array ~a1~. + - Similarly ~-=~, ~*=~ \& ~/=~. + - ~a1 + a2~ + does elements-wise addition. + - Similarly ~-~, ~*~ \& ~/~. + - ~a1 * a2~ + does element-wise multiplication + + *Note* - array(A) * array(B) does element wise multiplication and not matrix multiplication + +* Summary + In this tutorial we covered, + - Basics of arrays + - Creating arrays + - Arrays from lists + - Basic array operations + +* Thank you! +#+begin_latex + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +#+end_latex + + diff --git a/getting_started_with_arrays/slides.tex b/getting_started_with_arrays/slides.tex new file mode 100644 index 0000000..7273c59 --- /dev/null +++ b/getting_started_with_arrays/slides.tex @@ -0,0 +1,284 @@ +% Created 2010-11-07 Sun 15:18 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{t1enc} +\usepackage{textcomp} +\usepackage{marvosym} +\usepackage{wasysym} +\usepackage{latexsym} +\usepackage{amssymb} +\usepackage{hyperref} +\tolerance=1000 +\usepackage[english]{babel} \usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} +\usepackage{listings} +\lstset{language=Python, basicstyle=\ttfamily\bfseries, +commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\providecommand{\alert}[1]{\textbf{#1}} + +\title{Getting started with arrays} +\author{FOSSEE} +\date{} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + +\maketitle + + + + + + + + + +\begin{frame} +\frametitle{Outline} +\label{sec-1} + +\begin{itemize} +\item Arrays + +\begin{itemize} +\item why arrays over lists +\end{itemize} + +\item Creating arrays +\item Array operations +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Overview of Arrays} +\label{sec-2} + +\begin{itemize} +\item Arrays are homogeneous data structures. + +\begin{itemize} +\item elements have to the same data type +\end{itemize} + +\item Arrays are faster compared to lists + +\begin{itemize} +\item at least \emph{80-100 times} faster than lists +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Creating Arrays} +\label{sec-3} + +\begin{itemize} +\item Creating a 1-dimensional array +\end{itemize} + +\begin{verbatim} + In []: a1 = array([1, 2, 3, 4]) +\end{verbatim} + + \texttt{[1, 2, 3, 4]} is a list. +\end{frame} +\begin{frame}[fragile] +\frametitle{Creating two-dimensional array} +\label{sec-4} + +\begin{itemize} +\item Creating a 2-dimensional array +\end{itemize} + +\begin{verbatim} + In []: a2 = array([[1,2,3,4],[5,6,7,8]]) +\end{verbatim} + + here we convert a list of lists to an array making a 2-d array. +\begin{itemize} +\item Easier method of creating array with consecutive elements. +\end{itemize} + +\begin{verbatim} + In []: ar = arange(1,9) +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{\texttt{reshape()} method} +\label{sec-5} + +\begin{itemize} +\item To reshape an array +\end{itemize} + +\begin{verbatim} + In []: ar.reshape(2, 4) + In []: ar.reshape(4, 2) + In []: ar = ar.reshape(2, 4) +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Creating \texttt{array} from \texttt{list}.} +\label{sec-6} + +\begin{itemize} +\item \texttt{array()} method accepts list as argument +\item Creating a list +\begin{verbatim} + In []: l1 = [1, 2, 3, 4] +\end{verbatim} + +\item Creating an array +\begin{verbatim} + In []: a3 = array(l1) +\end{verbatim} + +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Exercise 1} +\label{sec-7} + + Create a 3-dimensional array of the order (2, 2, 4). +\end{frame} +\begin{frame}[fragile] +\frametitle{\texttt{.shape} of array} +\label{sec-8} + +\begin{itemize} +\item \texttt{.shape} + To find the shape of the array +\begin{verbatim} + In []: a1.shape +\end{verbatim} + +\item \texttt{.shape} + returns a tuple of shape +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Exercise 2} +\label{sec-9} + + Find out the shape of the other arrays(a2, a3, ar) that we have created. +\end{frame} +\begin{frame}[fragile] +\frametitle{Homogeneous data} +\label{sec-10} + +\begin{itemize} +\item All elements in array should be of same type +\begin{verbatim} + In []: a4 = array([1,2,3,'a string']) +\end{verbatim} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Implicit type casting} +\label{sec-11} + +\begin{verbatim} + In []: a4 +\end{verbatim} + + All elements are type casted to string type +\end{frame} +\begin{frame} +\frametitle{\texttt{identity()}, \texttt{zeros()} methods} +\label{sec-12} + +\begin{itemize} +\item \texttt{identity(n)} + Creates an identity matrix, a square matrix of order (n, n) with diagonal elements 1 and others 0. +\item \texttt{zeros((m, n))} + Creates an \texttt{m X n} matrix with all elements 0. +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Learning exercise} +\label{sec-13} + +\begin{itemize} +\item Find out about + +\begin{itemize} +\item \texttt{zeros\_like()} +\item \texttt{ones()} +\item \texttt{ones\_like()} +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Array operations} +\label{sec-14} + +\begin{itemize} +\item \texttt{a1 * 2} + returns a new array with all elements of \texttt{a1} multiplied by \texttt{2}. + +\begin{itemize} +\item Similarly \texttt{+}, \texttt{-} \& \texttt{/}. +\end{itemize} + +\item \texttt{a1 + 2} + returns a new array with all elements of \texttt{a1} summed with \texttt{2}. +\item \texttt{a1 += 2} + adds \texttt{2} to all elements of array \texttt{a1}. + +\begin{itemize} +\item Similarly \texttt{-=}, \texttt{*=} \& \texttt{/=}. +\end{itemize} + +\item \texttt{a1 + a2} + does elements-wise addition. + +\begin{itemize} +\item Similarly \texttt{-}, \texttt{*} \& \texttt{/}. +\end{itemize} + +\item \texttt{a1 * a2} + does element-wise multiplication +\end{itemize} + + + \textbf{Note} - array(A) * array(B) does element wise multiplication and not matrix multiplication +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-15} + + In this tutorial we covered, +\begin{itemize} +\item Basics of arrays +\item Creating arrays +\item Arrays from lists +\item Basic array operations +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-16} + + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} |