In [1]:
%load_ext autoreload
%autoreload 2

from agile_analyst import Agile_Analyst
import pandas as pd
import numpy as np

In [2]:
aff_ai = Agile_Analyst()

Load and Preprocess Training Data


In [3]:
vocab = pd.DataFrame.from_csv('../../data/pairing_01(2017-08-21 00:03:13.056).csv')
sizes = pd.DataFrame.from_csv('../../data/pairing_02(2017-08-21 00:01:41.166).csv')
print(vocab.head(), sizes.head())


                          ['agony 3', 'anguish 3', 'attraction 3']
fawn                                                              
convolute        ['alarm 3', 'dismay 3', 'fatigue 3', 'flatness...
conjury                                             ['devotion 3']
daftness         ['foolishness 3', 'foolishness 2', 'fury 3', '...
visual property  ['anger 3', 'badness 3', 'boredom 3', 'brightn...
snack bar                                         ['acceptance 3']                     19
worry 1               
affectionateness 1  12
eroticism 2         16
eroticism 3         85
negation 2          56
eroticism 1          9

In [4]:
# clean_vocab = vocab.filter(regex="(\w+(\S))")
clean_vocab = pd.DataFrame()
for row in range(len(vocab)):
    # print (vocab.iloc[row].name.split(" "))
    if len(vocab.iloc[row].name.split(" ")) == 1:
        entries = vocab.iloc[row][0][1:-1]
#         print(entries)
        entries = entries.replace("'","")
        entries = [entries.split(', ')]
#         print(entries)
        entries = pd.DataFrame([entries],index=[vocab.iloc[row].name])
#         print(entries)
        clean_vocab = clean_vocab.append(entries)

In [5]:
print(clean_vocab.head(10))
print(clean_vocab.iloc[0], clean_vocab.iloc[0].name)


                                                            0
convolute   [alarm 3, dismay 3, fatigue 3, flatness 3, imp...
conjury                                          [devotion 3]
daftness    [foolishness 3, foolishness 2, fury 3, indulge...
aegis       [blame 3, concern 3, confidence 3, denial 3, f...
forgather   [aggravation 3, ambiance 3, anguish 3, audacio...
preface     [ambiance 3, consideration 3, darkness 3, desp...
conjuring                                        [devotion 3]
shamble                         [attraction 3, disturbance 3]
conjure     [abhorrence 3, abomination 3, agony 3, alarm 3...
regularize  [alarm 3, compromise 3, consonance 3, desire 3...
0    [alarm 3, dismay 3, fatigue 3, flatness 3, imp...
Name: convolute, dtype: object convolute

In [6]:
print(len(clean_vocab), clean_vocab.shape[0])


14054 14054

In [7]:
weights = {}
for row in range(len(sizes)):
    weights[sizes.iloc[row].name] = 100 / sizes.iloc[row][0]

Train an Instance of Affect AI


In [8]:
aff_ai.train(clean_vocab, weights)

In [9]:
print(list(aff_ai.vocab.keys())[:100])


['situation', 'morpheme', 'gospeler', 'sustentation', 'paraphrasis', 'hover', 'tremble', 'advertiser', 'quad', 'provisions', 'wateriness', 'clearness', 'workman', 'quotient', 'flutter', 'catamenia', 'demon', 'assimilation', 'barrenness', 'capriciousness', 'valuable', 'Eros', 'owner', 'requester', 'cleaning', 'aegis', 'stringency', 'equilibrise', 'refulgency', 'sheet', 'barque', 'interpenetrate', 'misuse', 'dramatize', 'say', 'nest', 'student', 'jurisdiction', 'thriftiness', 'deceiver', 'nictitation', 'sepulture', 'asterisk', 'lallygag', 'cheerlessness', 'keepsake', 'substructure', 'nimbleness', 'mercilessness', 'thrower', 'intend', 'fastidiousness', 'cognizance', 'renovate', 'savoring', 'disparagement', 'decry', 'enough', 'hatful', 'pout', 'vacillation', 'carangid', 'heel', 'tunnel', 'defame', 'interdiction', 'controversy', 'exponent', 'quell', 'placidity', 'lucidity', 'Sung', 'grapple', 'founder', 'privilege', 'John', 'aviator', 'Service', 'curtain', 'haulage', 'restlessness', 'taxon', 'latex', 'monish', 'pane', 'fly', 'mobilise', 'faerie', 'authorities', 'deftness', 'chunk', 'jeweller', 'perversion', 'duct', 'admirer', 'sandwich', 'orbit', 'get-up-and-go', 'image', 'instrumentality']

In [10]:
print(aff_ai.corpora['fatigue 3'])


679

In [11]:
import time
t1 = time.clock()
print(aff_ai.score("lemon cake"))
print(aff_ai.score("why did you choose lemon cake"))
t2 = time.clock()
print('time required:',t2-t1)


{'aversion 3': 0.31746031746031744, 'safety 3': 0.029886431560071727, 'triumph 3': 0.025967281225655673, 'distance 3': 0.021463833440652502, 'thrill 3': 0.018653236336504384, 'defeat 3': 0.038095238095238099, 'reception 3': 0.027917364600781685}
{'boldness 3': 0.064977257959714096, 'strength 3': 0.045620437956204379, 'delight 3': 0.069541029207232263, 'serenity 3': 0.11123470522803114, 'compromise 3': 0.033806626098715348, 'power 3': 0.027878449958182325, 'resistance 3': 0.068634179821551136, 'submission 3': 0.052687038988408853, 'tenacity 3': 0.1519756838905775, 'worry 3': 0.026874496103198066, 'unification 3': 0.10152284263959391, 'hopefulness 3': 0.10672358591248667, 'spite 3': 0.030543677458766034, 'appraisal 3': 0.14285714285714285, 'love 3': 0.02246686137946529, 'dissonance 3': 0.0764525993883792, 'fascination 3': 0.092081031307550645, 'sweetness 3': 0.086805555555555552, 'pleasantness 3': 0.18484288354898337, 'lust 3': 0.11587485515643106, 'guilt 3': 0.13054830287206268, 'wonder 3': 0.061881188118811881, 'trust 3': 0.017677214071062399, 'fear 3': 0.045330915684496827, 'compromise 2': 0.45454545454545453, 'stress 3': 0.024746349913387776, 'content 3': 0.034782608695652174, 'irritation 3': 0.053648068669527899, 'honor 3': 0.02615062761506276, 'awkwardness 3': 0.07407407407407407, 'defeat 3': 0.076190476190476197, 'attraction 3': 0.027359781121751026, 'negativeness 3': 0.0764525993883792, 'loss 3': 0.03151591553734636, 'eagerness 3': 0.16, 'easiness 3': 0.061957868649318466, 'sourness 3': 0.11173184357541899, 'neglect 3': 0.03551136363636364, 'disturbance 3': 0.024588148512417016, 'modesty 3': 0.18083182640144665, 'dominance 3': 0.058997050147492625, 'audaciousness 3': 0.069060773480662987, 'tenseness 3': 0.053050397877984087, 'inspiration 3': 0.07745933384972889, 'distance 3': 0.042927666881305004, 'sulkiness 3': 0.10040160642570281, 'thrill 3': 0.037306472673008767, 'blame 3': 0.029377203290246769, 'importance 3': 0.14025245441795231, 'despair 3': 0.091407678244972576, 'spirit 3': 0.028943560057887119, 'pleasure 2': 0.5376344086021505, 'dimension 3': 0.032541490400260331, 'sunniness 3': 0.071073205401563616, 'ignorance 3': 0.35842293906810035, 'desire 3': 0.040567951318458417, 'dullness 3': 0.061012812690665039, 'straightness 3': 0.042881646655231559, 'triumph 3': 0.051934562451311346, 'determination 2': 0.29239766081871343, 'admiration 3': 0.10515247108307045, 'blame 2': 0.32154340836012862, 'ambiance 3': 0.060422960725075532, 'positiveness 3': 0.066800267201068811, 'indulgence 3': 0.039385584875935409, 'ego 3': 0.037481259370314844, 'disgrace 3': 0.046061722708429294, 'vigor 3': 0.050942435048395317, 'disdain 3': 0.072992700729927001, 'safety 3': 0.059772863120143453, 'keenness 3': 0.054674685620557682, 'lunacy 3': 0.04208754208754209, 'darkness 3': 0.047596382674916705, 'dismay 3': 0.056369785794813977, 'anger 3': 0.029308323563892145, 'dislike 3': 0.10799136069114471, 'flatness 3': 0.058719906048150326, 'negation 3': 0.20964360587002095, 'emptiness 3': 0.058072009291521488, 'intrigue 3': 0.049627791563275438, 'cheerfulness 3': 0.20491803278688525, 'relief 3': 0.018573551263001486, 'saltiness 3': 0.064267352185089971, 'respect 3': 0.022075055187637971, 'hostility 3': 0.1040582726326743, 'consideration 3': 0.060864272671941569, 'devastation 3': 0.047460844803037493, 'liberation 3': 0.029222676797194622, 'ecstasy 3': 0.03718854592785422, 'impulse 3': 0.028571428571428571, 'depression 3': 0.039635354736424891, 'alteration 3': 0.097560975609756101, 'rage 3': 0.033726812816188868, 'grace 3': 0.043687199650502405, 'pleasure 3': 0.04791566842357451, 'luckiness 3': 0.068493150684931503, 'alienation 3': 0.087183958151700089, 'freedom 3': 0.15948963317384371, 'fatigue 3': 0.027948574622694244, 'reception 3': 0.05583472920156337, 'aggressiveness 3': 0.052521008403361345, 'aversion 3': 0.31746031746031744, 'alarm 3': 0.049236829148202856, 'reliability 3': 0.65359477124183007, 'scorn 3': 0.072254335260115612, 'shame 3': 0.023551577955723033, 'luck 3': 0.044863167339614179, 'inferiority 3': 0.22222222222222221, 'motivation 3': 0.065746219592373437, 'determination 3': 0.030703101013202335, 'disbelief 3': 0.2544529262086514, 'engagement 3': 0.025833118057349523, 'surprise 3': 0.027151778441487917, 'bitterness 3': 0.09950248756218906, 'tenaciousness 3': 0.1519756838905775, 'content 2': 0.36231884057971014, 'honor 2': 0.27548209366391185, 'fate 3': 0.04065040650406504, 'fortune 3': 0.0414765657403567, 'ecstasy 2': 0.44247787610619471, 'peacefulness 3': 0.13908205841446453, 'diligence 3': 0.068259385665529013, 'concern 3': 0.028042624789680313, 'intensity 3': 0.052301255230125521, 'enjoyment 3': 0.10822510822510822, 'hurt 3': 0.02246686137946529, 'shock 3': 0.017869907076483203, 'withdrawal 3': 0.085689802913453295, 'stubbornness 3': 0.093896713615023469, 'regret 3': 0.053022269353128315, 'perturbation 3': 0.049652432969215489, 'exploitation 3': 0.074571215510812833, 'satisfaction 3': 0.068027210884353748, 'innocence 3': 0.11467889908256881, 'harmony 3': 0.061996280223186609, 'disrespect 3': 0.032268473701193935, 'validation 3': 0.082372322899505759, 'appreciation 3': 0.032164683177870697, 'reservation 3': 0.057110222729868647, 'relaxation 3': 0.0471253534401508, 'attraction 2': 0.34129692832764508, 'joy 3': 0.082236842105263164, 'interest 3': 0.017889087656529516, 'belief 3': 0.071428571428571425, 'unity 3': 0.04928536224741252, 'lifelessness 3': 0.21367521367521367}
time required: 0.011656000000002109

Test Scorer Storage and Retrieval


In [12]:
from sklearn.externals import joblib
import copy

In [13]:
joblib.dump(aff_ai, 'affect_ai_8-27-2017')


Out[13]:
['affect_ai_8-27-2017']

In [14]:
aa2 = joblib.load('affect_ai_8-27-2017')

In [15]:
aa2.score('gimme lemons cake yum')


Out[15]:
{'aversion 3': 0.31746031746031744,
 'defeat 3': 0.038095238095238099,
 'distance 3': 0.021463833440652502,
 'reception 3': 0.027917364600781685,
 'safety 3': 0.029886431560071727,
 'thrill 3': 0.018653236336504384,
 'triumph 3': 0.025967281225655673}

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: