Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: CSCI 195
Views: 5930
Image: ubuntu2004
Kernel: Python 3 (system-wide)
import numpy as np
print(np.arange(0, 30)) arr = np.arange(0, 30).reshape(5,6) arr
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29]])
# Make all of the first column values be 0 arr[:, 0] = 0 arr
array([[ 0, 1, 2, 3, 4, 5], [ 0, 7, 8, 9, 10, 11], [ 0, 13, 14, 15, 16, 17], [ 0, 19, 20, 21, 22, 23], [ 0, 25, 26, 27, 28, 29]])
# Make the last entry in the array be 100 arr[-1, -1] = 100 print(arr) # Another way arr[arr.shape[0]-1, arr.shape[1]-1] = 29 print(arr)
[[ 0 1 2 3 4 5] [ 0 7 8 9 10 11] [ 0 13 14 15 16 17] [ 0 19 20 21 22 23] [ 0 25 26 27 28 100]] [[ 0 1 2 3 4 5] [ 0 7 8 9 10 11] [ 0 13 14 15 16 17] [ 0 19 20 21 22 23] [ 0 25 26 27 28 29]]
# Make the last row be twice the first row arr[-1,:] = 2*arr[0,:] print(arr) twos = np.full((1, 6),2) print(twos) arr[-1, :] = twos * arr[0,:] print(arr)
[[ 0 1 2 3 4 5] [ 0 7 8 9 10 11] [ 0 13 14 15 16 17] [ 0 19 20 21 22 23] [ 0 2 4 6 8 10]] [[2 2 2 2 2 2]] [[ 0 1 2 3 4 5] [ 0 7 8 9 10 11] [ 0 13 14 15 16 17] [ 0 19 20 21 22 23] [ 0 2 4 6 8 10]]
# Allocate space for a 5x6 array without initializing the values random_values = np.empty((5, 6)) print(random_values) print(type(random_values[0,0]))
[[2.55288620e-316 0.00000000e+000 0.00000000e+000 0.00000000e+000 6.92258687e-310 5.02034658e+175] [1.77103950e-051 1.39407471e+165 6.53316657e-042 1.22014882e+165 6.79378448e-144 1.12855837e+277] [6.32672840e+180 2.66371964e+233 5.04621361e+180 8.37170571e-144 3.97062405e+246 1.16318408e-028] [7.11426094e-038 3.30399831e+179 6.12975327e-062 3.14190039e+179 3.98450697e+252 6.74640243e-067] [4.57134226e-071 5.06636302e-086 3.35809595e-143 4.82337433e+228 6.14415221e-144 6.92278571e-310]] <class 'numpy.float64'>
numbers = np.random.randint(80, 100, 8) print(numbers)
[98 89 89 85 89 91 93 95]
sorted_indices = np.argsort(numbers) print(sorted_indices) sorted_list = numbers[sorted_indices] print(sorted_list)
[3 1 2 4 5 6 7 0] [85 89 89 89 91 93 95 98]
numbers = np.random.randint(70, 100, (8, 3)) print(numbers)
[[82 93 80] [88 85 79] [99 83 75] [85 73 98] [85 70 98] [99 78 96] [81 85 72] [89 95 71]]
sorted_by_last_exam_indexes = np.argsort(numbers[:, 2]) sorted_by_last_exam = numbers[sorted_by_last_exam_indexes] print(sorted_by_last_exam)
[[89 95 71] [81 85 72] [99 83 75] [88 85 79] [82 93 80] [99 78 96] [85 73 98] [85 70 98]]
# What were the scores for people who got a score in the 70s on the last exam? less_than_80_as_boolean = numbers[:, 2] < 80 at_least_70_as_boolean = numbers[:, 2] >= 70 print(less_than_80_as_boolean) print(at_least_70_as_boolean) in_the_70s = np.logical_and(less_than_80_as_boolean, at_least_70_as_boolean) in_the_70s = less_than_80_as_boolean & at_least_70_as_boolean print(numbers[in_the_70s][:, 2])
[False True True False False False True True] [ True True True True True True True True] [79 75 72 71]