Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 2, numpy-module
- # little Python warmup:
- # pep8 is the standard usually followed
- # when it comes to Python coding style
- numbers = [3, 5, 6, 7, 8, 6, 5, 4, 6, 7]
- print(numbers)
- print(numbers[5])
- print(numbers[0:3])
- for n in numbers:
- print(n)
- # a list of lists, or in numpy => Matrix
- day_1 = [20, 24, 18, 23]
- day_2 = [15, 13, 12, 10]
- day_3 = [30, 32, 27, 28]
- temperatures = [day_1, day_2, day_3]
- print()
- print(temperatures[1][0])
- print()
- for day in temperatures:
- print("NEW DAY!")
- for t in day:
- print(t)
- # NEW FILE!
- import numpy as np
- # if you have existing Python lists, or lists of lists
- # you can easily convert these into numpy arrays!
- numbers = [1, 2, 3, 400, 3]
- data = np.array(numbers)
- list1 = [1, 2, 3, 4]
- list2 = [8, 9, 10, 11]
- list3 = [15, 16, 17, 18]
- combined = [list1, list2, list3]
- matrix = np.array(combined)
- # NEW FILE!
- import numpy as np
- # first parameter is included in the list
- # second parameter is excluded in the list
- # so last number is 20 - 1 = 19
- data = np.arange(0, 20)
- data = np.arange(5, 101, 5)
- ones = np.ones(10)
- zeroes = np.zeros(15)
- # matrix of ones, 5 by 5
- one_matrix = np.ones((5, 5))
- # 10 by 10 matrix = 10 * 10 = 100
- zero_matrix = np.zeros((10, 10))
- # if you ever need an identity matrix quickly,
- # this is the way!
- id_matrix = np.eye(8)
- # a list of evenly distributed values
- lin_data = np.linspace(0, 150, 11)
- # NEW FILE!
- import numpy as np
- # vector, 30 values
- data = np.random.rand(30)
- # 10 by 10 = 100 values
- data = np.random.rand(10, 10)
- standard = np.random.randn(10)
- number = np.random.randint(0, 100)
- numbers = np.random.randint(1, 1000, 50)
- # reshape-function!
- data = np.arange(1, 501)
- data = data.reshape(50, 10)
- # the recommended way, chained functions.
- # first create a large vector
- # and immediately reshape it to matrix
- data = np.arange(1, 101).reshape(10, 10)
- numbers = np.random.randint(1, 1000, 50).reshape(10, 5)
- # if you ever need to know the dimensions of the data,
- # use the shape
- print(numbers.shape)
- # these work mostly on vectors
- max_val = numbers.max()
- min_val = numbers.min()
- max_val_pos = numbers.argmax()
- min_val_pos = numbers.argmin()
- # NEW FILE!
- import numpy as np
- # basic index selections are identical to plain Python
- data = np.arange(50, 100)
- single_value = data[15]
- sublist1 = data[1:10]
- sublist2 = data[:25]
- sublist3 = data[25:]
- matrix = np.arange(5, 46, 5).reshape(3, 3)
- # first row and column
- value = matrix[0][0]
- # second row, third column
- value = matrix[1,2]
- # third row
- row = matrix[2]
- # everything BEFORE second row,
- # everything AFTER second column
- section = matrix[:1, 1:]
- matrix = np.arange(1, 26).reshape(5,5)
- section = matrix[:3, 2:]
- # taking the fifth column
- column = matrix[:, 4]
- # NEW FILE!
- import numpy as np
- numbers = np.random.randint(1, 1000, 20)
- big_numbers = numbers[numbers >= 500]
- odd_numbers = numbers[numbers % 2 != 0]
- # broadcasting
- # remember to make a copy of your data
- # if you don't want operations to
- # affect original data as well!
- # this example shows what happens:
- # compare data and data_copy
- data = np.arange(1, 101)
- data_copy = data[0:10]
- data_copy[:] = -1
- # version that uses copy()
- data = np.arange(1, 101)
- data_copy = data[0:10].copy()
- data_copy[:] = -1
- # arithmetic operations
- data = np.arange(1, 101)
- combined = data + data
- reduced = data - data
- multiplied = data * data
- increased = data + 100
- data = np.ones(10) * 25
- # NEW FILE!
- import numpy as np
- data = np.arange(0, 11)
- # 0/0 => NaN => Not a Number
- result = data / data
- # 1 / 0 => inf => infinite
- other = 1 / data
- # sum and standard deviation
- numbers = np.random.randint(1, 1000, 5)
- random_total = np.sum(numbers)
- # low standard deviation means the values are close to the average value
- # high standard deviation menas the values are far apart from each other
- random_deviation = np.std(numbers)
Add Comment
Please, Sign In to add comment