Test #1: Build
Naïve Bayes classifiers from give rock-paper-scissor
transcripts using numpy
- Rock-paper-scissor
transcripts: As in Lab 4 and Lab 5, examine the same zip file containing two sample transcripts generated
from playing with Agent#1 and Agent#2 respectively. For each transcript in
the zip file, use numpy.loadtext
to load the data into a 2-D numpy array to
conduct basic data analysis on the data collected in these two transcripts
as described below. For the test, we want to implement functions to learn
from the rock-paper-scissor
transcript data to build Naïve Bayes
classifiers for prediction.
- Implement the
following functions for Naïve Bayes classification: Create a Jupiter
notebook test1.ipynb to do the following
- Implement a
function getNaiveBayesStatistics(TrainingData)
that can process the rock-paper-scissor transcript stored in a 2-D numpy
array TrainingData
and return a python dictionary that holds the information of all the
statistics needed for Naïve Bayes
classification. The dictionary should be in the format of { 'agentPrev1': agentPrev1, 'agentPrev2': agentPrev2,
'userPrev1': userPrev1, 'userPrev2': userPrev2, 'prior':prior,
} where
a) prior is a 1-D numpy array storing the probability
distribution of the agent playing
rock or paper or scissor over all the matches recorded in the entire
transcript,
b) agentPrev1 is a 2-D numpy array storing the probability
distributions of the agent playing
rock or paper or scissor in the previous match given that the agent plays rock or paper or scissor respectively in the
current match,
c) agentPrev2 is a 2-D numpy array storing the probability
distributions of the agent playing
rock or paper or scissor 2 matches
ago given that the agent plays rock
or paper or scissor respectively in the current match,
d) userPrev1 is a 2-D numpy array storing the probability
distributions of the user playing
rock or paper or scissor in the previous match given that the agent plays rock or paper or scissor respectively in the
current match, and
e) userPrev2 is a 2-D numpy array storing the probability
distributions of the user playing
rock or paper or scissor 2 matches
ago given that the agent plays rock
or paper or scissor respectively in the current match.
Note that you can accomplish this task
conveniently based on similar things what you learned and did for Lab 4 and Lab
5 earlier.
- Implement a
function getProbabilityNaiveBayesOneCase(NiaiveStatisticsDictionary,
newCase) that can return a 1-D numpy
array storing the predicted probabilities of seeing the agent playing rock or paper or scissor in a newCase
given the Naïve Bayes statistics stored in NaiveStatisticsDictionary. Note that newCase should be a 1-D numpy array for storing the agent’s actions in the
previous two moves and the user’s actions in the previous two moves while
NaiveStatisticsDictionary should be a dictionary in the
format described in the implementation of getNaiveBayesStatistics above. The task of the function is to apply the Naïve Bayes
classification to the new case based on the Naïve Bayes statistics
stored in NaiveStatisticsDictionary.
- Implement a
function predictNaiveBayesOneCase(NiaiveStatisticsDictionary,
newCase) that can return the most
likely action (rock or paper or scissor as a number). This function is
essentially a variant of getProbabilityNaiveBayesOneCase(statisticsDictionary, newCase)
but return the most likely action (rock or paper or scissor as a number) instead of the probabilities of
all three of them.
- Implement a
function predictNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData) to examine all the matches
in a rock-paper-scissor transcript
stored in a 2-D numpy
array TesingData,
properly call predictNaiveBayesOneCase to predict the outcomes of all
the cases (except for the first two cases in the transcript), and return
the predicted outcomes as a 1-D
numpy array.
- Implement a
function getPrecisionNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData) is a variant of predictNaiveBayesWholeTranscript above. Instead of
returning the predicted outcomes as a 1-D numpy array, this function
compares the predicted outcomes with the actual outcomes in TestingData
and returns the percentage of correctly predicted outcomes
accomplished by the Naïve Bayes classifier.
- Use your
functions for Naïve Bayes classification: In your Jupiter notebook
test1.ipynb, do the following to test your implementation above.
- Use numpy.loadtext to
load the data of the transcript of Agent 1 into a 2-D numpy
array TrainingData. Use your
function getNaiveBayesStatistics
to learn from this training data and stored the
Naïve Bayes model returned as a dictionary NiaiveStatisticsDictionary.
Display the contents of the dictionary.
- Create a 1-D
numpy array newCase = np.array([0,1,2,0]) to represent a case where the agent played Rock
and Paper in the previous two moves while the user played Scissor and
Rock in the previous two moves. Call the function getProbabilityNaiveBayesOneCase(NiaiveStatisticsDictionary, newCase)
to determine predicted probabilities of seeing rock or paper or
scissor. Display the probabilities. Call the function predictNaiveBayesOneCase(NiaiveStatisticsDictionary, newCase)
to determine the predicted move. Display the predicted move.
- Use numpy.loadtext to
load the data of the transcript of Agent 1 into a 2-D numpy
array TestingData1. Call the function predictNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData1) to determine the predicted outcomes. Display the
predicted outcomes. Call the
function getPrecisionNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData1) to determine the precision of the prediction.
Display the precision.
- Use numpy.loadtext to
load the data of the transcript of Agent 2 into a 2-D numpy
array TestingData2. Call the function predictNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData2) to determine the predicted outcomes. Display the
predicted outcomes.
- Call the function getPrecisionNaiveBayesWholeTranscript(NiaiveStatisticsDictionary,
TestingData2) to determine the precision of the prediction.
Display the precision.
- Repeat 1 to
5 above again except that in step 1 use numpy.loadtext
to load the data of the transcript of Agent 2 into a 2-D numpy array TrainingData.
- Submission of
your work: Fill out this self-evaluation
report. Upload the self-evaluation report and your Test1.ipynb (to
show your code and results) and under canvas.