markov model, numpy introduction - ms.mcmaster.camatt/1mp3/notes/week8-handout.pdf · markov model,...
TRANSCRIPT
Markov models (dictionary example)
I A Markov model is one where the future state of a systemdepends only on its current state (not on any previous states).
I They’re widely used in lots of fields: physics, chemistry, queuingtheory, economics, genetics, mathematical biology, sports, . . . .
I Here is an example from this wikipedia page:https://en.wikipedia.org/wiki/Markov_chain.
I Suppose that you start with $10, and you wager $1 on anunending, fair, coin toss indefinitely, or until you lose all of yourmoney. If Xn represents the number of dollars you have after ntosses, with X0 = 10, then the sequence {Xn : n ∈ N} is aMarkov process. If I know that you have $12 now, then itwould be expected that with even odds, you will either have$11 or $13 after the next toss. This guess is not improved bythe added knowledge that you started with $10, then went upto $11, down to $10, up to $11, and then to $12.
I Markov models can be used for text analysis.
I A Markov model of text would say that the next word in apiece of text (or letter, depending on what scale we’re workingat) depends only on the current word.
I We will create some code that will analyse some text and basedon the frequency of word pairs, will produce a short “sentence”from the words in the text, using the Markov model.
I The key element of this exercise will be a dictionary withkey:value pairs of the form word:list, where word is aword from the text and list is the list of words in the textthat immediately follow word.
Some issues
I The text that we use, for example Kafka’s Metamorphosis(http://www.gutenberg.org/files/5200/5200.txt) or Melville’sMoby Dick (http://www.gutenberg.org/files/2701/2701-0.txt),will contain lots of symbols, such as punctuation, that weshould remove first.
I We should also convert all words to lower case.I The text that we use will either be in a file stored locally, or
maybe accessed using its url.I There is a random element to Markov processes and so we will
need to be able to generate numbers randomly (orpseudo-randomly).
Cleaning strings
I Strings have a lower() method that can be used to convertall upper case letters to lower case.
I text/data cleaning is an inevitable part of dealing with text filesor data sets.
I python has a function called translate() that can be used toscrub certain characters from a string, but it is a littlecomplicated.
I For a discussion of this, check out:https://machinelearningmastery.com/clean-text-machine-learning-python/
I here is a simple function that will delete from a given string scharacters that appear in the string delete_chars.
I The python string string.punctuation is equal to!"#$%&\'()*+,-./:;<=>?@[\\]ˆ_{|}~‘.
I To use it import string must be executed.
def clean_string(s,delete_chars):for i in delete_chars:
s = s.replace(i,"")return(s)
x = "ab,Cde!?Q@#$I"import stringprint(clean_string(x,string.punctuation))
## abCdeQI
Description of the Markov text model algorithm
1. Open and read the text file.2. Clean the file.3. Create the text dictionary with items word: list of words
that follow.4. Randomly select a word from the text and then create a
“sentence” using randomly selected words from the dictionary.
markov_create function
def markov_create(file_name, sentence_length = 20):# open the file and store its contents in a string.text_file = open(file_name, 'r')text = text_file.read()# clean the text and then split it into wordsclean_text = clean_string(text)word_list = clean_text.split()# create the markov dictionarytext_dict = markov_dict(word_list)# Produce a sentence (a list of strings) of length# sentence_length using the dictionarysentence = markov_sentence(text_dict, sentence_length)# print out the sentence as a string using# the .join() method.return " ".join(sentence)
To complete this exercise, we need to produce the followingfunctions:
def clean_string(s,delete_chars = string.punctuation):
that strips the text of punctuation and converts upper case wordsinto lower case.
def markov_dict(word_list):
a function that creates a dictionary from a list of words, and
def markov_sentence(text_dict, sentence_length):
a function that randomly produces a sentence using the dictionary.
the random moduleI The module random can be used to generate pseudo-random
numbers or to pseudo-randomly select items.I Doc: https://docs.python.org/3/library/random.htmlI A random integer from a prescribed range can be generated
using the randrange method.I The method choice(seq) randomly chooses an element from
a sequence, such as a list or tuple.I shuffle and sample can be used to shuffle the items in a list
and to sample elements from a list, tuple, or set.
import randomrandom.randrange(2, 102, 2) # random even numbersrandom.choice([1, 2, 3, 4, 5]) # random choice from listrandom.sample([1, 2, 3, 4, 5], 3) # rand. sample of 3 itemsrandom.random() # random float between 0 and 1random.uniform(3, 7) # random num between 3 and 7
NumPy Installation
I NumPy is the fundamental package for scientific computingwith Python. It contains among other things:
I a powerful N-dimensional array objectI sophisticated (broadcasting) functionsI useful linear algebra and random number capabilities
I NumPy should already be installed on the spyder IDE. If notyou will need to install it.
I Good NumPy documentation can be found here (as pdf) andhere.
Arrays
I The array() is numpy’s main data structure.I It is similar to a python list, butI while a list can contain items of various types,I arrays contain a homogeneous set of values (e.g. floating point
(float64) or integer (int64) or str).
import numpy as npx = [1, 2, 3]a = np.array([1, 4, 5, 8], float)print(type(a)), print(a), print(a.shape)
## <class 'numpy.ndarray'>## [1. 4. 5. 8.]## (4,)
I The shape of an array is a tuple that lists its dimensions.I np.array([1,2]) produces a 1-dimensional (1-D) array of
length 2 whose entries have type intI np.array([1,2], float) produces a 1-dimensional (1-D)
array of length 2 whose entries have type float.
a1 = np.array([1,2])print(a1.dtype), print(a1.shape), print(len(a1))
## int32## (2,)## 2
a2 = np.array([1,2],float)print(a2.dtype)
## float64
I arrays can be created from lists or tuples.I arrays can also be created using the range function.I NumPy has a similar function, called arange that can be used
to create arrays.I an array of all zeroes or ones can be created using the zeros()
and ones() functions.
x = [1, 'a', 3]a = np.array(x)b = np.array(range(10), float)c = np.arange(5, dtype=float)d = np.arange(2,4, 0.5, dtype=float)np.ones(10)np.zeros(4)
I array slicing and indexing of 1-D arrays works the same way aslists/tuples/strings
I arrays are mutable like lists/dictionaries, so we can setelements (e.g. a[1]=0)
I or use the .copy() method (works for lists etc. too!)
a1 = np.array([1.0, 2, 3, 4, 5, 6])a1[1]a1[:-3]b1 = a1c1 = a1.copy()b1[1] = 23a1[1]c1[1]
Multi-dimensional arrays
I we have seen nested lists of lists and have used them torepresent matrices.
I in numpy, 2-dimensional arrays serve the same purpose but areeasier to work with.
I they can be created by passing a list of lists/tuple of tuples tothe array() function.
I Elements of an array are indexed via a[i,j] rather thana[i][j]
nested = [[1, 2, 3], [4, 5, 6]]a = np.array(nested, float)nested[0][2]a[0,2]aa.shape
slicing, reshapingI slicing of multiple dimensional arrays works in a similar way as
for lists and strings.I for each dimension, we can specify a particular sliceI : indicates that everything along a dimension will be used.
a = np.array([[1, 2, 3], [4, 5, 6]], float)a[1, :]a[:, 2]a[-1:, -2:]
I An array can be reshaped using the reshape(tuple) function,where tuple gives the new dimensions of the array.
a = np.array(range(10), float)a = a.reshape((5,2))print(a)
I An array with a given shape can be flattened into a 1-D array:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])print(a), print(a.flatten())
## [[1 2 3]## [4 5 6]## [7 8 9]]## [1 2 3 4 5 6 7 8 9]
I arrays consisting of just 0’s or 1’s that have the shape of agiven array can also be easily created using the fill(),zeros_like() or ones_like() functions:
b = np.ones_like(a)b.fill(33)
I The identity matrix (with 1’s down the diagonal) can becreated using the identity() or the eye() function.
I With the eye() function, ones can also be placed onoff-diagonals;
print(np.identity(4, dtype=float)),
## [[1. 0. 0. 0.]## [0. 1. 0. 0.]## [0. 0. 1. 0.]## [0. 0. 0. 1.]]
print(np.eye(4, k = -1, dtype=int))
## [[0 0 0 0]## [1 0 0 0]## [0 1 0 0]## [0 0 1 0]]
array mathematicsI for lists (or tuples or strings), the + operation is used to
concatenate two lists to create a longer one.I this operation for arrays plays a different role.I to concatenate two arrays of suitable shapes, the
concatenate() function can be used:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])b = np.array([[10, 11,12], [13, 14, 15], [16, 17, 18]])print(np.concatenate((a,b)))
## [[ 1 2 3]## [ 4 5 6]## [ 7 8 9]## [10 11 12]## [13 14 15]## [16 17 18]]
I When the + operation is used on arrays, it is applied on anelement-by-element basis.
I This also applies to most other standard mathematicaloperations.
print(a+b), print(a*b), print(a/b)
## [[11 13 15]## [17 19 21]## [23 25 27]]## [[ 10 22 36]## [ 52 70 90]## [112 136 162]]## [[0.1 0.18181818 0.25 ]## [0.30769231 0.35714286 0.4 ]## [0.4375 0.47058824 0.5 ]]
I To add a number, say 1, to every element of an array a, type a+ 1
I similarly for other operations, like -, *, **, /, . . .
print(a + 1), print(a/2), print(a ** 3)
## [[ 2 3 4]## [ 5 6 7]## [ 8 9 10]]## [[0.5 1. 1.5]## [2. 2.5 3. ]## [3.5 4. 4.5]]## [[ 1 8 27]## [ 64 125 216]## [343 512 729]]
I NumPy comes with a large library of common functions, suchas sin, cos, log, exp, . . .
I There are functions that can be applied to arrays to extractvarious properties,
I for example a.sum() and a.prod() will produce the sum andthe product of the items in a:
print(np.sin(a)), print(a.sum()), print(a.prod()), \print(a.mean())
## [[ 0.84147098 0.90929743 0.14112001]## [-0.7568025 -0.95892427 -0.2794155 ]## [ 0.6569866 0.98935825 0.41211849]]## 45## 362880## 5.0