ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 17.04.2021

Просмотров: 3755

Скачиваний: 3

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
background image

Introduction to Python for Science, Release 0.9.23

3.2.4 Multidimensional lists and tuples

We can also make multidimensional lists, or lists of lists. Consider, for example, a list of
three elements, where each element in the list is itself a list:

In [40]:

a

=

[[

3

,

9

], [

8

,

5

], [

11

,

1

]]

Here we have a three-element list where each element consists of a two-element list. Such
constructs can be useful in making tables and other structures. They also become relevant
later on in our discussion of NumPy arrays and matrices, which we introduce below.

We can access the various elements of a list with a straightforward extension of the index-
ing scheme we have been using. The first element of the list

a

above is

a[0]

, which is

[3, 9]

; the second is

a[1]

, which is

[8, 5]

. The first element of

a[1]

is accessed

as

a[1][0]

, which is 8, as illustrated below:

In [41]:

a[

0

]

Out[41]:

[

3

,

9

]

In [42]:

a[

1

]

Out[42]:

[

8

,

5

]

In [43]:

a[

1

][

0

]

Out[43]:

8

In [44]:

a[

2

][

1

]

Out[44]:

1

Multidimensional tuples work exactly like multidimensional lists, except they are im-
mutable.

3.3 NumPy arrays

The NumPy array is the real workhorse of data structures for scientific and engineering
applications. The NumPy array, formally called

ndarray

in NumPy documentation, is

similar to a list but where all the elements of the list are of the same type. The elements of
a NumPy array, or simply an

array

, are usually numbers, but can also be boolians, strings,

or other objects. When the elements are numbers, they must all be of the same type. For
example, they might be all integers or all floating point numbers.

3.3. NumPy arrays

35


background image

Introduction to Python for Science, Release 0.9.23

3.3.1 Creating arrays (1-d)

NumPy has a number of functions for creating arrays. We focus on four (or five or six,
depending on how you count!). The

first

of these, the

array

function, converts a list to

an array:

In [1]:

a

=

[

0

,

0

,

1

,

4

,

7

,

16

,

31

,

64

,

127

]

In [2]:

b

=

array(a)

In [3]:

b

Out[3]:

array([

0

,

0

,

1

,

4

,

7

,

16

,

31

,

64

,

127

])

In [4]:

c

=

array([

1

,

4.

,

-

2

,

7

])

In [5]:

c

Out[5]:

array([

1.

,

4.

,

-

2.

,

7.

])

Notice that

b

is an integer array, as it was created from a list of integers. On the other hand,

c

is a floating point array even though only one of the elements of the list from which it

was made was a floating point number. The

array

function automatically promotes all

of the numbers to the type of the most general entry in the list, which in this case is a
floating point number. In the case that elements of the list is made up of numbers and
strings, all the elements become strings when an array is formed from a list.

The

second

way arrays can be created is using the NumPy

linspace

or

logspace

functions. The

linspace

function creates an array of

N

evenly spaced points between

a starting point and an ending point. The form of the function is

linspace(start,

stop, N)

. If the third argument

N

is omitted, then

N=50

.

In [6]:

linspace(

0

,

10

,

5

)

Out[6]:

array([

0.

,

2.5

,

5.

,

7.5

,

10.

])

The

linspace

function produced 5 evenly spaced points between 0 and 10 inclusive.

NumPy also has a closely related function

logspace

that produces evenly spaced points

on a logarithmically spaced scale. The arguments are the same as those for

linspace

except that

start

and

stop

refer to a power of 10. That is, the array starts at

10

start

and ends at

10

stop

.

In [7]:

%

precision

1

# display only 1 digit after decimal

Out[7]:

u’

%.1f

In [8]:

logspace(

1

,

3

,

5

)

Out[8]:

array([

10.

,

31.6

,

100.

,

316.2

,

1000.

])

36

Chapter 3. Strings, Lists, Arrays, and Dictionaries


background image

Introduction to Python for Science, Release 0.9.23

The

logspace

function created an array with 5 points evenly spaced on a logarithmic

axis starting at

10

1

and ending at

10

3

. The

logspace

function is particularly useful

when you want to create a log-log plot.

The

third

way arrays can be created is using the NumPy

arange

function, which is

similar to the Python

range

function for creating lists. The form of the function is

arange(start, stop, step)

. If the third argument is omitted

step=1

. If the

first and third arguments are omitted, then

start=0

and

step=1

.

In [9]:

arange(

0

,

10

,

2

)

Out[9]:

array([

0

,

2

,

4

,

6

,

8

])

In [10]:

arange(

0.

,

10

,

2

)

Out[10]:

array([

0.

,

2.

,

4.

,

6.

,

8.

])

In [11]:

arange(

0

,

10

,

1.5

)

Out[11]:

array([

0.

,

1.5

,

3.

,

4.5

,

6.

,

7.5

,

9.

])

The

arange

function produces points evenly spaced between 0 and 10 exclusive of the

final point. Notice that

arange

produces an integer array in the first case but a floating

point array in the other two cases. In general

arange

produces an integer array if the

arguments are all integers; making any one of the arguments a float causes the array that
is created to be a float.

A

fourth

way to create an array is with the

zeros

and

ones

functions. As their names

imply, they create arrays where all the elements are either zeros or ones. They each take
on mandatory argument, the number of elements in the array, and one optional argument
that specifies the data type of the array. Left unspecified, the data type is a float. Here are
three examples

In [12]:

zeros(

6

)

Out[12]:

array([

0.

,

0.

,

0.

,

0.

,

0.

,

0.

])

In [13]ones(8)

Out[13]:

array([

1.

,

1.

,

1.

,

1.

,

1.

,

1.

,

1.

,

1.

])

In [14]ones(8, dtype=int)

Out[14]:

array([

1

,

1

,

1

,

1

,

1

,

1

,

1

,

1

])

Recap of ways to create a 1-d array

array(a)

:

Creates an array from the list

a

.

linspace(start, stop, num)

:

Returns

num

evenly spaced num-

bers over an interval from

start

to

stop

inclusive. [

num=50

if

omitted.]

3.3. NumPy arrays

37


background image

Introduction to Python for Science, Release 0.9.23

logspace(start, stop, num)

:

Returns

num

logarithmically

spaced numbers over an interval from

10

start

to

10

stop

inclusive.

[

num=50

if omitted.]

arange([start,] stop[, step,], dtype=None)

:

Returns

data points from

start

to

end

, exclusive, evenly spaced by

step

.

[

step=1

if omitted.

start=0

and

step=1

if both are omitted.]

zeros(num, dtype=float)

:

Returns an an array of 0s with

num

el-

ements. Optional

dtype

argument can be used to set data type; left

unspecified, a float array is made.

ones(num, dtype=float)

:

Returns an an array of 1s with

num

ele-

ments. Optional

dtype

argument can be used to set data type; left

unspecified, a float array is made.

3.3.2 Mathematical operations with arrays

The utility and power of arrays in Python comes from the fact that you can process and
transform all the elements of an array in one fell swoop. The best way to see how this
works is look at an example.

In [15]:

a

=

linspace(

-

1.

,

5

,

7

)

In [16]:

a

Out[16]:

array([

-

1.

,

0.

,

1.

,

2.

,

3.

,

4.

,

5.

])

In [17]:

a

*

6

Out[17]:

array([

-

6.

,

0.

,

6.

,

12.

,

18.

,

24.

,

30.

])

Here we can see that each element of the array has been multiplied by 6. This works
not only for multiplication, but for any other mathematical operation you can imagine:
division, exponentiation,

etc

.

In [18]:

a

/

5

Out[18]:

array([

-

0.2

,

0.

,

0.2

,

0.4

,

0.6

,

0.8

,

1.

])

In [19]:

a

**

3

Out[19]:

array([

-

1.

,

0.

,

1.

,

8.

,

27.

,

64.

,

125.

])

In [20]:

a

+

4

Out[20]:

array([

3.

,

4.

,

5.

,

6.

,

7.

,

8.

,

9.

])

In [21]:

a

-

10

38

Chapter 3. Strings, Lists, Arrays, and Dictionaries


background image

Introduction to Python for Science, Release 0.9.23

Out[21]:

array([

-

11.

,

-

10.

,

-

9.

,

-

8.

,

-

7.

,

-

6.

,

-

5.

])

In [22]:

(a

+

3

)

*

2

Out[22]:

array([

4.

,

6.

,

8.

,

10.

,

12.

,

14.

,

16.

])

In [23]:

sin(a)

Out[23]:

array([

-

0.84147098

,

0.

,

0.84147098

,

0.90929743

,

0.14112001, -0.7568025 , -0.95892427])

In [24]:

exp(

-

a)

Out[24]:

array([

2.71828183

,

1.

,

0.36787944

,

0.13533528

,

0.04978707, 0.01831564, 0.00673795])

In [25]:

1.

+

exp(

-

a)

Out[25]:

array([

3.71828183

,

2.

,

1.36787944

,

1.13533528

,

1.04978707, 1.01831564, 1.00673795])

In [26]:

b

=

5

*

ones(

8

)

In [27]:

b

Out[27]:

array([

5.

,

5.

,

5.

,

5.

,

5.

,

5.

,

5.

,

5.

])

In [28] b += 4

In [29] b

Out[29]:

array([

9.

,

9.

,

9.

,

9.

,

9.

,

9.

,

9.

,

9.

])

In each case, you can see that the same mathematical operations are performed individ-
ually on each element of each array. Even fairly complex algebraic computations can be
carried out this way.

Let’s say you want to create an

x

-

y

data set of

y

= cos

x

vs

.

x

over the interval from -3.14

to 3.14. Here is how you might do it.

In [30]:

x

=

linspace(

-

3.14

,

3.14

,

21

)

In [31]:

y

=

cos(x)

In [32]:

x

Out[32]:

array([

-

3.14

,

-

2.826

,

-

2.512

,

-

2.198

,

-

1.884

,

-

1.57

,

-1.256, -0.942, -0.628, -0.314, 0.

, 0.314,

0.628, 0.942, 1.256, 1.57 , 1.884, 2.198,

2.512, 2.826, 3.14 ])

In [33]:

y

3.3. NumPy arrays

39