#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ENGG1811 Lecture 7 Examples on numpy """ # This python file contains a number of examples on using # the numpy library to construct an array # %% import numpy import numpy as np # %% # This cell is similar to the file numpy_elements.py # from Week 6's lecture # # The examples below show how you can create numpy # arrays by specifying the elements # Creating a 1-dimensional array and print it list_a = [1, 2, 3, -3, -5, -6, 8, 12] array1 = np.array(list_a) # Print the array print('array1 is', array1) # Creating a 2-dimensional array and array2 = np.array([ [-1.2, 2, -3.1, 4.5], [ 4, -5, 3.5, 7.1], [ 2.7, 9, 1.7, 3.4]]) print('array2 is', array2) # %% # # Sometimes you want to specify linearly spaced numbers # E.g. an array with elements 0, 0.1, 0.2, 0.3, ..., 1.9, 2 # # In Week 3, you did that with range() and list comprehension # # range() which is a Python function but it can only # generate a list of integers # # numpy has a function called arange(), which is very # similar to range(), but can be used with floats # The inputs for arange() are start, stop, step array3 = np.arange(0,2.01,0.1) print('array3 is', array3) # You can also use arange() with 1 or 2 arguments # See its manual page at: # https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html # %% # # For arange(), you get a linearly spaced sequence of numbers # by specifying the start value, end value and the increment # (= step) # # An alternative is to use linspace(). You need to specify # the start value, end value and the number of points you # want. # The inputs for arange() are start, stop, number of points array4 = np.linspace(0,2,21) print('array4 is', array4) print('The number of entries in array4 is',len(array4)) # See its manual page at: # https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html#numpy.linspace # Remark: linspace() generates arithmetic progressions. # There is also a function called logspace() to # generate geometric progressions. # # Manual page for logspace() # https://docs.scipy.org/doc/numpy/reference/generated/numpy.logspace.html#numpy.logspace # %% # # Sometimes you may want an array pre-filled with # either zeros or ones array5 = np.zeros((2,4),dtype='float') array6 = np.ones(5,dtype='int') print('array5 is', array5) print('array6 is', array6) # Manual page # zeros() # https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html # ones() # https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html # %% # # Sometimes you may want to have an array of zeros or ones # which has the same shape as another array # array2, which we used earlier, has a shape of (3,4) array7 = np.zeros_like(array2, dtype='float') # gives an array filled with zeros and has the same # shape as array2 print('array7 is ', array7) # Manual page for zeros_like() # https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros_like.html # # There is also ones_like() # https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones_like.html #