PYZpcautoreload endVerbatim section2 NumPylabelnumpy subsubsection21 Type

Pyzpcautoreload endverbatim section2 numpylabelnumpy

This preview shows page 9 - 12 out of 34 pages.

{\color{incolor}In [{\color{incolor}145}]:} \PY{o}{?} \PYZpc{}autoreload \end{Verbatim} \section{2. NumPy}\label{numpy} \subsubsection{2.1 Type, creating and accessing}\label{type-creating-and-accessing} NumPy, short for Numerical Python, is a library that provides efficient storage and data operations on arrays. They are essential tools for a data scientist. List: Flexibility comes at a cost - each item in the list must contain its own type info, reference count, and other information---that is, each item is a complete Python object. In the special case that all variables are of the same type, much of this information is redundant: it can be much more efficient to store data in a fixed-type array. \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}146}]:} \PY{n}{L} \PY{o}{=} \PY{p} {[}\PY{k+kc}{True}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{2}\PY{l+s+s1} {\PYZsq{}}\PY{p}{,} \PY{l+m+mi}{12}\PY{p}{,} \PY{l+m+mf}{3.14}\PY{p}{]} \PY{c+c1}{\PYZsh{} convenience at a cost} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}147}]:} \PY{p}{[}\PY{n+nb}{type}\PY{p} {(}\PY{n}{l}\PY{p}{)} \PY{k}{for} \PY{n}{l} \PY{o+ow}{in} \PY{n}{L}\PY{p}{]} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}147}]:} [bool, str, int, float] \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}]
Image of page 9
{\color{incolor}In [{\color{incolor}148}]:} \PY{c+c1}{\PYZsh{} figure 2.2: under the hood mappings} \end{Verbatim} \begin{figure} \centering \includegraphics{attachment:array_vs_list.png} \caption{array\_vs\_list.png} \end{figure} \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}149}]:} \PY{n}{np}\PY{o}{.}\PY{n} {array}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p} {,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{]}\PY{p}{)} \PY{c+c1} {\PYZsh{}fixed type} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}149}]:} array([1, 3, 4, 6]) \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}151}]:} \PY{n}{np}\PY{o}{.}\PY{n} {array}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p} {,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mf}{6.3}\PY{p}{]}\PY{p}{)} \PY{c+c1} {\PYZsh{}fixed type} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}151}]:} array([1. , 3. , 4. , 6.3]) \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}152}]:} \PY{n}{np}\PY{o}{.}\PY{n} {array}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p} {,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,} \PY{n}{dtype}\PY{o} {=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{float32}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{}explicit type} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}152}]:} array([1., 3., 4., 6.], dtype=float32) \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}153}]:} \PY{n}{np}\PY{o}{.}\PY{n} {array}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p} {,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi} {5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{7}\PY{p} {,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{]}\PY{p}{]}\PY{p}{)} \PY{c+c1} {\PYZsh{}multi\PYZhy{}d, \PYZgt{}lists} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}153}]:} array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) \end{Verbatim}
Image of page 10
Creating arrays \begin{Verbatim}[commandchars=\\\{\}] {\color{incolor}In [{\color{incolor}154}]:} \PY{n}{np}\PY{o}{.}\PY{n} {ones}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)}\PY{p}{)} \end{Verbatim} \begin{Verbatim}[commandchars=\\\{\}] {\color{outcolor}Out[{\color{outcolor}154}]:} array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]) \end{Verbatim}
Image of page 11
Image of page 12

You've reached the end of your free preview.

Want to read all 34 pages?

  • Fall '09

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture