<jupyter_start><jupyter_text>Using Built-in Functions to Create ndarrays<jupyter_code># We create a 3 x 4 ndarray full of zeros. 
X = np.zeros((3,4))
# X = np.ones((3,2))
# np.full(shape, constant value)
print('X = \n', X)

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
# We create a 5 x 5 Identity matrix. 
X = np.eye(5)

print('X = \n', X)

# Create a 4 x 4 diagonal matrix that contains the numbers 10,20,30, and 50
# on its main diagonal
X = np.diag([10,20,30,50])
print('X = \n', X)
# We create a rank 1 ndarray that has sequential integers from 0 to 9
x = np.arange(10)
print('x = ', x)

# We create a rank 1 ndarray that has evenly spaced integers from 1 to 13 in steps of 3.
x = np.arange(1,14,3)
print('x = ', x)
# We create a rank 1 ndarray that has 10 integers evenly spaced between 0 and 25,
# with 25 excluded.
x = np.linspace(0,25,10, endpoint = False)
print('x = ', x)

# We create a rank 1 ndarray with 10 integers evenly spaced between 0 and 50,
# with 50 excluded. We then reshape it to a 5 x 2 ndarray
X = np.linspace(0,50,10, endpoint=False).reshape(5,2)
print('X = \n', X)
# We create a 3 x 2 ndarray with random integers in the half-open interval [4, 15).
X = np.random.randint(4,15,size=(3,2))

print('X = \n', X)
# We create a 1000 x 1000 ndarray of random floats drawn from normal (Gaussian) distribution
# with a mean of zero and a standard deviation of 0.1.
X = np.random.normal(0, 0.1, size=(1000,1000))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
print('The elements in X have a mean of:', X.mean())
print('The maximum value in X is:', X.max())
print('The minimum value in X is:', X.min())
print('X has', (X < 0).sum(), 'negative numbers')
print('X has', (X > 0).sum(), 'positive numbers')<jupyter_output>X = 
 [[ 0.03957725 -0.00493177  0.04646322 ...  0.0638329  -0.08492721
  -0.0313585 ]
 [-0.23491727  0.05795087  0.06795608 ... -0.00822195  0.11602839
  -0.04055852]
 [ 0.09524756 -0.0749379   0.05072966 ... -0.01792618  0.06548182
  -0.06196452]
 ...
 [ 0.07074457  0.08120943 -0.07690712 ... -0.15553347 -0.03415656
   0.02712988]
 [-0.05223968 -0.06316713 -0.07568617 ... -0.01829586 -0.04341687
   0.1151966 ]
 [ 0.05293241 -0.01807152 -0.10299914 ... -0.07132904 -0.02032489
  -0.02731891]]

X has dimensions: (1000, 1000)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float64
The elements in X have a mean of: -4.5432394888622705e-05
The maximum value in X is: 0.5103680582722182
The minimum value in X is: -0.4637829659625311
X has 499757 negative numbers
X has 500243 positive numbers<jupyter_text>Accessing, Deleting, and Inserting Elements Into ndarrays<jupyter_code># We create a rank 2 ndarray
Y = np.array([[1,2,3],[4,5,6],[7,8,9]])
print('Original Y = \n', Y)

# We delete the first row of y
w = np.delete(Y, 0, axis=0)
print('w = \n', w)

# We delete the first and last column of y
v = np.delete(Y, [0,2], axis=1)
print('v = \n', v)

# We append a new row containing 7,8,9 to y
v = np.append(Y, [[7,8,9]], axis=0)
print('v = \n', v)

# We append a new column containing 9 and 10 to y
q = np.append(Y,[[9],[10],[11]], axis=1)
print('q = \n', q)
# We create a rank 1 ndarray 
x = np.array([1,2])
print('x = ', x)

# We create a rank 2 ndarray 
Y = np.array([[3,4],[5,6]])
print('Y = \n', Y)

# We stack x on top of Y
z = np.vstack((x,Y))
print('z = \n', z)

# We stack x on the right of Y. We need to reshape x in order to stack it on the right of Y. 
w = np.hstack((Y,x.reshape(2,1)))
print('w = \n', w)<jupyter_output>x =  [1 2]
Y = 
 [[3 4]
 [5 6]]
z = 
 [[1 2]
 [3 4]
 [5 6]]
w = 
 [[3 4 1]
 [5 6 2]]<jupyter_text>Slicing ndarrays1. ndarray[start:end]2. ndarray[start:]3. ndarray[:end]<jupyter_code># We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)
print('X = \n', X)

# We select all the elements that are in the 2nd through 4th rows and in the 3rd to 5th columns
Z = X[1:,2:5]
print('Z = \n', Z)
# create a copy of the slice using the np.copy() function
Z = np.copy(X[1:4,2:5])

#  create a copy of the slice using the copy as a method
W = X[1:4,2:5].copy()

# We change the last element in Z to 555
Z[2,2] = 555

# We change the last element in W to 444
W[2,2] = 444

print('X = \n', X)
print('Z = \n', Z)
print('W = \n', W)
print('X = \n', X)
# We print the elements above the main diagonal of X
print('y =', np.diag(X, k=1))
print()

# Create 3 x 3 ndarray with repeated values
X = np.array([[1,2,3],[5,2,8],[1,2,3]])
print('X = \n', X)

# We print the unique elements of X 
print('The unique elements in X are:',np.unique(X))<jupyter_output>X = 
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
y = [ 1  7 13 19]

X = 
 [[1 2 3]
 [5 2 8]
 [1 2 3]]
The unique elements in X are: [1 2 3 5 8]<jupyter_text>Boolean Indexing, Set Operations, and Sorting<jupyter_code># We create a 5 x 5 ndarray that contains integers from 0 to 24
X = np.arange(25).reshape(5, 5)
print('Original X = \n', X)
# We use Boolean indexing to select elements in X:
print('The elements in X that are greater than 10:', X[X > 10])

# We use Boolean indexing to assign the elements that are between 10 and 17 the value of -1
X[(X > 10) & (X < 17)] = -1
print('X = \n', X)
# We create a rank 1 ndarray
x = np.array([1,2,3,4,5])

# We create a rank 1 ndarray
y = np.array([6,7,2,8,4])

# We use set operations to compare x and y:
print()
print('The elements that are both in x and y:', np.intersect1d(x,y))
print('The elements that are in x that are not in y:', np.setdiff1d(x,y))
print('All the elements of x and y:',np.union1d(x,y))
# We create an unsorted rank 1 ndarray
x = np.random.randint(1,11,size=(10,))
print('Original x = ', x)
print('Sorted x (out of place):', np.sort(x))
# When we sort out of place the original array remains intact. To see this we print x again
print('x after function sorting:', x)

# We sort x and print the sorted array using sort as a method.
x.sort()
print('x after method sorting:', x)
# We create an unsorted rank 2 ndarray
X = np.random.randint(1,11,size=(5,5))
print('Original X = \n', X)

# We sort the columns of X and print the sorted array
print('X with sorted columns :\n', np.sort(X, axis = 0))<jupyter_output>Original X = 
 [[ 5  4  1  2  5]
 [10  3  5  1 10]
 [ 7  7  4  9 10]
 [ 1  5  3  7  1]
 [ 5  1 10  2  2]]
X with sorted columns :
 [[ 1  1  1  1  1]
 [ 5  3  3  2  2]
 [ 5  4  4  2  5]
 [ 7  5  5  7 10]
 [10  7 10  9 10]]<jupyter_text>Arithmetic operations and Broadcasting<jupyter_code># We create two rank 1 ndarrays
x = np.array([1,2,3,4])
y = np.array([5.5,6.5,7.5,8.5])
print('x = ', x)
print('y = ', y)

print('multiply(x,y) = ', np.multiply(x,y))
print('POW(x,2) =',np.power(x,2)) # We raise all elements to the power of 2

# We create a 2 x 2 ndarray
X = np.array([[1,2,5], [3,4,6]])

print('X = \n', X)
print('Standard Deviation of all elements in the columns of X:', X.std(axis=0))
print('Minimum value of all elements in the rows of X:', X.min(axis=1))

# We create a rank 1 ndarray
x = np.array([1,2,3])
# We create a 3 x 3 ndarray
Y = np.array([[1,2,3],[4,5,6],[7,8,9]])
print('x + Y = \n', x + Y)<jupyter_output>X = 
 [[1 2 5]
 [3 4 6]]
Standard Deviation of all elements in the columns of X: [1.  1.  0.5]
Minimum value of all elements in the rows of X: [1 3]
x + Y = 
 [[ 2  4  6]
 [ 5  7  9]
 [ 8 10 12]]
StarcoderdataJupyterStructuredCleanDedup