PythonDataScience_01 - Jake VanderPlas Flashcards

1
Q

In welcher Sprache ist Python geschrieben?

A

The standard Python implementation is written in C. This means that every Python object is simply a cleverly-disguised C structure, which contains not only its value, but other information as well.

This means that there is some overhead in storing an integer in Python as compared to an integer in a compiled language like C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Was kann eine Liste aus der Python-Datenstruktur speichern?

A

Durch dynamische Typisierung kann ich heterogene Listen erstellen mit Integern, Strings oder Boolean.

Das erhöht aber auch den Speicherbedarf, da jedes als einzelne Python-Objekt gespeichert wird.

Bei C wäre es nur ein Pointer auf einen Speicherplatz.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Wenn ich eine homogene Liste (Integer Array) erstellen möchte, was benutze ich am besten?

A

Das ndarray aus dem NumPy-Paket.

import numpy as np

In [8]:

integer array:
np.array([1, 4, 2, 5, 3])

Out[8]:

array([1, 4, 2, 5, 3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wie kann ich explizit den Datentyp in einem Numpy-Array festlegen?

Ein Array 1,2,3,4 mit Datentyp float32

A

In [10]:

np.array([1, 2, 3, 4], dtype=’float32’)

Out[10]:

array([1., 2., 3., 4.], dtype=float32)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Wie kann ich mit Numpy ein multidimensionales Array erstellen?

array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])

A

In [11]:

nested lists result in multi-dimensional arrays
np.array([range(i, i + 3) for i in [2, 4, 6]])

Out[11]:

array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Numpy: Create a length-10 integer array filled with zeros

A

Create a length-10 integer array filled with zeros
np.zeros(10, dtype=int)

Out[12]:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Numpy: Create a 3x5 floating-point array filled with ones

A

Create a 3x5 floating-point array filled with ones
np.ones((3, 5), dtype=float)

Out[13]:

array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Numpy: Create a 3x5 array filled with 3.14

A

Create a 3x5 array filled with 3.14
np.full((3, 5), 3.14)

Out[14]:

array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Numpy: # Create an array filled with a linear sequence
# Starting at 0, ending at 20, stepping by 2
# (this is similar to the built-in range() function)
A

np.arange(0, 20, 2)

Out[15]:

array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Numpy: Create an array of five values evenly spaced between 0 and 1

A

np.linspace(0, 1, 5)

Out[16]:

array([0. , 0.25, 0.5 , 0.75, 1.])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Numpy:

Create a 3x3 array of uniformly distributed
# random values between 0 and 1
A

np.random.random((3, 3))

Out[17]:

array([[0.99844933, 0.52183819, 0.22421193],
[0.08007488, 0.45429293, 0.20941444],
[0.14360941, 0.96910973, 0.946117]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Numpy

Create a 3x3 array of normally distributed random values
# with mean 0 and standard deviation 1
A

np.random.normal(0, 1, (3, 3))

Out[18]:

array([[1.51772646, 0.39614948, -0.10634696],
[0.25671348, 0.00732722, 0.37783601],
[0.68446945, 0.15926039, -0.70744073]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Numpy

Create a 3x3 array of random integers in the interval [0, 10)

A

np.random.randint(0, 10, (3, 3))

Out[19]:

array([[2, 3, 4],
[5, 7, 8],
[0, 5, 0]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Numpy

Create a 3x3 identity matrix

A

np.eye(3)

Out[20]:

array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Numpy

Create an uninitialized array of three integers
# The values will be whatever happens to already exist at that memory location
A

np.empty(3)

Out[21]:

array([1., 1., 1.])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Wie kann ich ein 1-,2- und 3-Dimensionales Array x1, x2 und x3 mit Zufallszahlen bis 10 erstellen?

A

np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array

17
Q

Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array):

Wie kann ich das von x3 anzeigen lassen?

A

print(“x3 ndim: “, x3.ndim)
print(“x3 shape:”, x3.shape)
print(“x3 size: “, x3.size)

x3 ndim: 3
x3 shape: (3, 4, 5)
x3 size: 60

18
Q

To index from the end of the array, you can use…

In [5]:

x1

Out[5]:

array([5, 0, 3, 3, 7, 9])

A

To index from the end of the array, you can use negative indices:

In [8]:

x1[-1]

Out[8]:

9

19
Q

In a multi-dimensional array, items can be accessed using a …

In [10]:

x2

Out[10]:

array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])

A

In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices:

x2[0, 0]

Out[11]:

3

20
Q

Wie kann ich die ersten 5 Elemente von array x anzeigen lassen?

A

x[:5] # first five elements

Out[17]:

array([0, 1, 2, 3, 4])

21
Q

Wie kann ich die Elemente ab index 5 anzeigen lassen?

A

x[5:] # elements after index 5

Out[18]:

array([5, 6, 7, 8, 9])

22
Q

Wie kann ich jedes zweite Element anzeigen lassen?

x = np.arange(10)
x

Out[16]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

A

x[::2] # every other element

Out[20]:

array([0, 2, 4, 6, 8])

23
Q

Wie kann ich mir von array x den Index 4 bis 7 anzeigen lassen?

A

x[4:7] # middle sub-array

Out[19]:

array([4, 5, 6])

24
Q

Wie kann ich mir jedes zweite Element anzeigen lassen? Startpunkt ist Index 1

x = np.arange(10)
x

Out[16]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

A

x[1::2] # every other element, starting at index 1

Out[21]:

array([1, 3, 5, 7, 9])

25
Q

Wie kann ich mir die Elemente von hinten bis vorne anzeigen lassen?

A

x[::-1] # all elements, reversed

Out[22]:

array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

26
Q

Wie kann ich mir die erste Reihe von x2 anzeigen lassen?

A

print(x2[0, :]) # first row of x2

[12 5 2 4]

print(x2[0]) # equivalent to x2[0, :]

27
Q

Wie kann ich mir die erste Spalte von x2 anzeigen lassen?

A

print(x2[:, 0]) # first column of x2

[12 7 1]

28
Q

Wie unterscheiden sich Python Listen und NumPy Listen?

A

One important–and extremely useful–thing to know about array slices is that they return views rather than copies of the array data. This is one area in which NumPy array slicing differs from Python list slicing: in lists, slices will be copies.

29
Q

Another useful type of operation is reshaping of arrays. The most flexible way of doing this is with the reshape method. For example, if you want to put the numbers 1 through 9 in a 3×3 grid, you can do the following:

A

In [38]:

grid = np.arange(1, 10).reshape((3, 3))
print(grid)

[[1 2 3]
[4 5 6]
[7 8 9]]

30
Q

Another common reshaping pattern is the conversion of a one-dimensional array into a two-dimensional row or column matrix. This can be done with the reshape method, or more easily done by making use of the newaxis keyword within a slice operation:

A

In [39]: x = np.array([1, 2, 3])

row vector via reshape
x.reshape((1, 3))

Out[39]: array([[1, 2, 3]])

row vector via newaxis

In [40]: x[np.newaxis, :]

Out[40]: array([[1, 2, 3]])

column vector via reshape

In [41]: x.reshape((3, 1))

Out[41]:

array([[1],
[2],
[3]])

column vector via newaxis

In [42]: x[:, np.newaxis]

Out[42]:

array([[1],
[2],
[3]])

31
Q

Concatenation of two arrays in NumPy

x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
A

np.concatenate([x, y])

Out[43]: array([1, 2, 3, 3, 2, 1])

32
Q

Concatenation of two-dimensional arrays:

grid = np.array([[1, 2, 3],
[4, 5, 6]])

A

concatenate along the first axis
np.concatenate([grid, grid])

array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])

33
Q

Concatenation along the second axis?

grid = np.array([[1, 2, 3],
[4, 5, 6]])

A

concatenate along the second axis (zero-indexed)
np.concatenate([grid, grid], axis=1)

Out[47]:

array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])

34
Q

Eine 4x4 Matrix mit Zahlen von 0 bis 15

A

grid = np.arange(16).reshape((4, 4))
grid

Out[51]:

array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]])

35
Q

Das Array x = [1, 2, 3, 99, 99, 3, 2, 1] folgendermaßen aufteilen:

[1 2 3] [99 99] [3 2 1]

A

x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)

[1 2 3] [99 99] [3 2 1]

36
Q

Kopiere die ersten 2 Reihen und 2 Spalten eines Array als x2_sub_copy

A

x2_sub_copy = x2[:2, :2].copy()

37
Q

Was sind ufuncs?

A

NumPy’s universal functions (ufuncs)

Vektorisierte Operationen in NumPy werden durch ufuncs implementiert.

38
Q
A