|
| 1 | +#!/usr/bin/env python |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +a = np.array([1, 2, 3]) |
| 5 | +print type(a) |
| 6 | +print a.shape |
| 7 | +print a[0], a[1], a[2] |
| 8 | +a[0] = 5 |
| 9 | +print a |
| 10 | + |
| 11 | +b = np.array([[1,2,3], [4,5,6]]) |
| 12 | +print b.shape # (2, 3), 2 lines, 3 columns, means two vectors, each have three dimensions |
| 13 | +print b[0,0], b[0,1], b[1,0] |
| 14 | + |
| 15 | +a = np.zeros((2,2)) # Create an array of all zeros |
| 16 | +print a # Prints "[[ 0. 0.] |
| 17 | + # [ 0. 0.]]" |
| 18 | + |
| 19 | +b = np.ones((1,2)) # Create an array of all ones |
| 20 | +print b # Prints "[[ 1. 1.]]" |
| 21 | + |
| 22 | +c = np.full((2,2), 7) # Create a constant array |
| 23 | +print c # Prints "[[ 7. 7.] |
| 24 | + # [ 7. 7.]]" |
| 25 | + |
| 26 | +d = np.eye(2) # Create a 2x2 identity matrix |
| 27 | +print d # Prints "[[ 1. 0.] |
| 28 | + # [ 0. 1.]]" |
| 29 | + |
| 30 | +e = np.random.random((2,2)) # Create an array filled with random values |
| 31 | +print e # Might print "[[ 0.91940167 0.08143941] |
| 32 | + # [ 0.68744134 0.87236687]]" |
| 33 | + |
| 34 | + |
| 35 | +# Create the following rank 2 array with shape (3, 4) |
| 36 | +# [[ 1 2 3 4] |
| 37 | +# [ 5 6 7 8] |
| 38 | +# [ 9 10 11 12]] |
| 39 | +a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) |
| 40 | + |
| 41 | +# Use slicing to pull out the subarray consisting of the first 2 rows |
| 42 | +# and columns 1 and 2; b is the following array of shape (2, 2): |
| 43 | +# [[2 3] |
| 44 | +# [6 7]] |
| 45 | +b = a[:2, 1:3] |
| 46 | + |
| 47 | +# A slice of an array is a view into the same data, so modifying it |
| 48 | +# will modify the original array. |
| 49 | +print a[0, 1] # Prints "2" |
| 50 | +b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1] |
| 51 | +print a[0, 1] # Prints "77" |
| 52 | + |
| 53 | +# Create the following rank 2 array with shape (3, 4) |
| 54 | +# [[ 1 2 3 4] |
| 55 | +# [ 5 6 7 8] |
| 56 | +# [ 9 10 11 12]] |
| 57 | +a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) |
| 58 | + |
| 59 | +# Two ways of accessing the data in the middle row of the array. |
| 60 | +# Mixing integer indexing with slices yields an array of lower rank, |
| 61 | +# while using only slices yields an array of the same rank as the |
| 62 | +# original array: |
| 63 | +row_r1 = a[1, :] # Rank 1 view of the second row of a |
| 64 | +row_r2 = a[1:2, :] # Rank 2 view of the second row of a |
| 65 | +print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)" |
| 66 | +print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)" |
| 67 | + |
| 68 | +# We can make the same distinction when accessing columns of an array: |
| 69 | +col_r1 = a[:, 1] |
| 70 | +col_r2 = a[:, 1:2] |
| 71 | +print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)" |
| 72 | +print col_r2, col_r2.shape # Prints "[[ 2] |
| 73 | + # [ 6] |
| 74 | + # [10]] (3, 1)" |
| 75 | + |
| 76 | + |
| 77 | +a = np.array([[1,2], [3, 4], [5, 6]]) |
| 78 | + |
| 79 | +# An example of integer array indexing. |
| 80 | +# The returned array will have shape (3,) and |
| 81 | +print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]" |
| 82 | + |
| 83 | +# The above example of integer array indexing is equivalent to this: |
| 84 | +print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]" |
| 85 | + |
| 86 | + |
| 87 | +# When using integer array indexing, you can reuse the same |
| 88 | +# element from the source array: |
| 89 | +print a[[0, 0], [1, 1]] # Prints "[2 2]" |
| 90 | + |
| 91 | +# Equivalent to the previous integer array indexing example |
| 92 | +print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]" |
| 93 | + |
| 94 | + |
| 95 | +a = np.array([[1,2], [3, 4], [5, 6]]) |
| 96 | + |
| 97 | +bool_idx = (a > 2) # Find the elements of a that are bigger than 2; |
| 98 | + # this returns a numpy array of Booleans of the same |
| 99 | + # shape as a, where each slot of bool_idx tells |
| 100 | + # whether that element of a is > 2. |
| 101 | + |
| 102 | +print bool_idx # Prints "[[False False] |
| 103 | + # [ True True] |
| 104 | + # [ True True]]" |
| 105 | + |
| 106 | +# We use boolean array indexing to construct a rank 1 array |
| 107 | +# consisting of the elements of a corresponding to the True values |
| 108 | +# of bool_idx |
| 109 | +print a[bool_idx] # Prints "[3 4 5 6]" |
| 110 | + |
| 111 | +# We can do all of the above in a single concise statement: |
| 112 | +print a[a > 2] # Prints "[3 4 5 6]" |
| 113 | + |
| 114 | + |
| 115 | +x = np.array([1, 2]) # Let numpy choose the datatype |
| 116 | +print x.dtype # Prints "int64" |
| 117 | + |
| 118 | +x = np.array([1.0, 2.0]) # Let numpy choose the datatype |
| 119 | +print x.dtype # Prints "float64" |
| 120 | + |
| 121 | +x = np.array([1, 2], dtype=np.int64) # Force a particular datatype |
| 122 | +print x.dtype # Prints "int64"xample of integer array indexing. |
| 123 | +# The returned array will have shape (3,) and |
| 124 | +print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]" |
| 125 | + |
| 126 | +# The above example of integer array indexing is equivalent to this: |
| 127 | +print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]" |
| 128 | + |
| 129 | + |
| 130 | +# When using integer array indexing, you can reuse the same |
| 131 | +# element from the source array: |
| 132 | +print a[[0, 0], [1, 1]] # Prints "[2 2]" |
| 133 | + |
| 134 | +# Equivalent to the previous integer array indexing example |
| 135 | +print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]" |
| 136 | + |
| 137 | +a = np.array([[1,2], [3, 4], [5, 6]]) |
| 138 | + |
| 139 | +bool_idx = (a > 2) # Find the elements of a that are bigger than 2; |
| 140 | + # this returns a numpy array of Booleans of the same |
| 141 | + # shape as a, where each slot of bool_idx tells |
| 142 | + # whether that element of a is > 2. |
| 143 | + |
| 144 | +print bool_idx # Prints "[[False False] |
| 145 | + # [ True True] |
| 146 | + # [ True True]]" |
| 147 | + |
| 148 | +# We use boolean array indexing to construct a rank 1 array |
| 149 | +# consisting of the elements of a corresponding to the True values |
| 150 | +# of bool_idx |
| 151 | +print a[bool_idx] # Prints "[3 4 5 6]" |
| 152 | + |
| 153 | +# We can do all of the above in a single concise statement: |
| 154 | +print a[a > 2] # Prints "[3 4 5 6]" |
| 155 | + |
| 156 | + |
0 commit comments