import pandas as pd
import numpy as np
%pylab inline
Populating the interactive namespace from numpy and matplotlib
data = pd.read_csv("data_1304.csv",encoding='latin-1')
data.dropna(inplace=True)
data['_unit_id'].head(10)
0     4
1     6
2    18
3     1
4     6
5    20
6    13
7    30
8     9
9     7
Name: _unit_id, dtype: int64

Time spent on a question (can be useful for worker ability)

pd.to_datetime(data['_created_at'])- pd.to_datetime(data['_started_at']) #use pd.to_numeric() to convert to number of ns
0    00:25:00
1    00:19:00
2    00:28:00
3    00:23:00
4    00:07:00
5    00:25:00
6    00:14:00
7    00:17:00
8    00:16:00
9    00:23:00
10   00:13:00
11   00:10:00
12   00:25:00
13   00:18:00
14   00:18:00
15   00:25:00
16   00:28:00
17   00:26:00
18   00:28:00
19   00:22:00
20   00:09:00
21   00:06:00
22   00:28:00
23   00:28:00
24   00:24:00
25   00:24:00
26   00:21:00
27   00:18:00
28   00:13:00
29   00:10:00
       ...   
70   00:25:00
71   00:29:00
72   00:24:00
73   00:16:00
74   00:25:00
75   00:29:00
76   00:16:00
77   00:21:00
78   00:24:00
79   00:20:00
80   00:20:00
81   00:20:00
82   00:25:00
83   00:19:00
84   00:15:00
85   00:18:00
86   00:26:00
87   00:21:00
88   00:21:00
89   00:23:00
90   00:11:00
91   00:25:00
92   00:11:00
93   00:26:00
94   00:21:00
95   00:20:00
96   00:29:00
97   00:24:00
98   00:22:00
99   00:27:00
Length: 100, dtype: timedelta64[ns]
len(data)
100
data.describe()
_unit_id _trust _worker_id similarity_0 asi1
count 100.000000 100.000000 100.000000 100.000000 100.000000
mean 15.730000 0.542089 49.500000 5.640000 3.680000
std 8.539693 0.301171 29.011492 1.275408 1.071957
min 0.000000 0.033270 0.000000 2.000000 0.000000
25% 9.000000 0.295970 24.750000 5.000000 3.000000
50% 15.000000 0.571722 49.500000 6.000000 4.000000
75% 23.000000 0.807306 74.250000 7.000000 4.000000
max 31.000000 0.988551 99.000000 7.000000 5.000000

Let's see how many judgments we have per unit

data.groupby('_unit_id').size()
_unit_id
0     1
1     3
2     3
3     2
4     4
5     1
6     2
7     7
8     1
9     2
10    4
11    3
12    2
13    4
14    6
15    7
16    3
17    5
18    3
19    1
20    2
21    4
22    1
23    6
24    5
25    3
26    2
27    4
28    2
29    1
30    4
31    2
dtype: int64
data.groupby('_unit_id').size().values
array([1, 3, 3, 2, 4, 1, 2, 7, 1, 2, 4, 3, 2, 4, 6, 7, 3, 5, 3, 1, 2, 4,
       1, 6, 5, 3, 2, 4, 2, 1, 4, 2])
data.groupby('_unit_id').size().hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff0abee0d30>

Let's remove the units that have only one judgment

(data.groupby('_unit_id').size()==1).values
array([ True, False, False, False, False,  True, False, False,  True,
        True, False,  True, False, False, False, False, False, False,
        True,  True, False, False, False, False, False, False, False,
       False,  True])
a = np.where((data.groupby('_unit_id').size()==1))
a
(array([ 0,  5,  8,  9, 11, 18, 19, 28]),)
a = list(a[0])
a
[0, 5, 8, 9, 11, 18, 19, 28]
data[data['_unit_id'].isin(a)]
better_0 _unit_id _started_at _created_at _trust _worker_id _city age similarity_0 explanation_0 asi1
2 Search engine query 18 1/10/2018 15:53:29 1/10/2018 16:21:35 0.551213 21 Pune 36-50 6 All the images represents the search better... 1
8 Your keyword 9 1/11/2018 04:55:31 1/11/2018 05:11:43 0.512431 29 Mangalagiri 19-25 6 THEY ARE THINKING 3
25 Search engine query 28 1/10/2018 15:44:18 1/10/2018 16:08:12 0.935156 83 Pune 19-25 5 working person uses the things that i mentioned 4
34 Search engine query 5 1/11/2018 04:13:24 1/11/2018 04:38:07 0.690587 14 Mangalagiri 19-25 7 both are different 3
49 Search engine query 0 1/10/2018 15:57:40 1/10/2018 16:24:15 0.230164 89 Aligarh 26-35 6 beacuse it is a hot air balloon 3
61 The two keywords are completely identical 18 1/10/2018 15:42:35 1/10/2018 16:03:41 0.547720 18 Hyderabad 36-50 6 They are similar 4
66 The two keywords are completely identical 28 1/11/2018 04:26:36 1/11/2018 04:38:58 0.362590 68 Mangalagiri 26-35 7 both were similar 5
76 Search engine query 19 1/10/2018 16:31:02 1/10/2018 16:47:56 0.967048 59 Pune 19-25 6 Their some people shouting at each other 4
95 The two keywords are completely identical 11 1/11/2018 05:45:54 1/11/2018 06:05:51 0.988551 23 Mangalagiri 0-18 7 similar 5
96 Search engine query 8 1/11/2018 02:23:26 1/11/2018 02:52:59 0.520720 0 Hyderabad 26-35 5 Since not all the images belong to science exa... 4
data = data[~data['_unit_id'].isin(a)]
len(data)
63
  1. Create a column with time spent (use pd.to_datetime)
  2. Compute the average time per worker
data['time_spent'] = pd.to_datetime(data['_created_at']) - pd.to_datetime(data['_started_at'])
data.head()
better_0 _unit_id _started_at _created_at _trust _worker_id _city age similarity_0 explanation_0 asi1 time_spent
0 Your keyword 4 1/10/2018 15:55:41 1/10/2018 16:20:41 0.385118 32 Ernakulam 36-50 6 they are all dressed well and using computers ... 4 00:25:00
1 The two keywords are completely identical 6 1/10/2018 17:04:22 1/10/2018 17:23:42 0.033270 13 Kolkata 36-50 6 Almost identicalexcept the tiny spelling diffe... 3 00:19:20
4 The two keywords are completely identical 6 1/11/2018 05:14:03 1/11/2018 05:21:25 0.708808 70 Mangalagiri 19-25 7 both are similar 5 00:07:22
5 The two keywords are completely identical 20 1/10/2018 16:41:16 1/10/2018 17:06:26 0.899786 95 Patna 26-35 7 they both describe the same kind of people 3 00:25:10
6 Your keyword 13 1/10/2018 15:47:20 1/10/2018 16:01:19 0.873825 37 Ulhasnagar 19-25 6 We can see a relaxed state in that images 5 00:13:59

Basic aggregation

Quantitative variables

data.groupby('_unit_id')['similarity_0'].mean()
_unit_id
1     5.500000
2     4.666667
3     6.000000
4     5.750000
6     6.500000
7     5.400000
10    6.000000
13    6.333333
14    4.000000
15    5.166667
16    5.000000
17    5.666667
20    7.000000
21    7.000000
23    6.500000
24    6.200000
25    4.333333
26    4.500000
27    5.666667
30    5.333333
31    4.000000
Name: similarity_0, dtype: float64

If we are also doing a per-worker analysis, we can compute values from the worker

data.groupby('_worker_id')['_trust'].mean().values
array([0.45086904, 0.92770687, 0.62552536, 0.93464997, 0.04204837,
       0.77016858, 0.04609719, 0.60929146, 0.5741613 , 0.81400838,
       0.03326987, 0.91541507, 0.95153081, 0.18914215, 0.30560117,
       0.80167709, 0.97441615, 0.92891881, 0.96747946, 0.09118499,
       0.62159951, 0.58563959, 0.58804797, 0.38511825, 0.12424342,
       0.79730475, 0.88870635, 0.87382468, 0.67732971, 0.85318828,
       0.34576951, 0.28074398, 0.80507253, 0.05786407, 0.09042158,
       0.42789365, 0.05809224, 0.32548398, 0.30012607, 0.03610733,
       0.85113121, 0.37399525, 0.79652694, 0.62465149, 0.3546574 ,
       0.91410825, 0.70880836, 0.28350176, 0.91083596, 0.33243423,
       0.03891988, 0.52527424, 0.26484709, 0.21250903, 0.63448413,
       0.03589091, 0.91445626, 0.2602371 , 0.9408621 , 0.9150934 ,
       0.85169676, 0.89978581, 0.61376345])
data.groupby('_worker_id')['_trust'].mean().hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff0b3eff390>

Categorical variables

Now we can't do the following because the following is a categorical variable:

data.groupby('_unit_id')['better_0'].mean()
---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
<ipython-input-20-2f584eae24bb> in <module>()
----> 1 data.groupby('_unit_id')['better_0'].mean()

/srv/paws/lib/python3.6/site-packages/pandas/core/groupby.py in mean(self, *args, **kwargs)
   1126         nv.validate_groupby_func('mean', args, kwargs, ['numeric_only'])
   1127         try:
-> 1128             return self._cython_agg_general('mean', **kwargs)
   1129         except GroupByError:
   1130             raise

/srv/paws/lib/python3.6/site-packages/pandas/core/groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
    925 
    926         if len(output) == 0:
--> 927             raise DataError('No numeric types to aggregate')
    928 
    929         return self._wrap_aggregated_output(output, names)

DataError: No numeric types to aggregate

Let's explore what is this column and decide what to do

data.groupby('_unit_id')['better_0'].describe() 
count unique top freq
_unit_id
1 2 2 Your keyword 1
2 3 2 Your keyword 2
3 2 2 Your keyword 1
4 4 3 The two keywords are completely identical 2
6 2 1 The two keywords are completely identical 2
7 5 2 Your keyword 3
10 4 2 The two keywords are completely identical 2
13 3 2 Your keyword 2
14 3 2 Search engine query 2
15 6 2 Your keyword 4
16 2 1 Search engine query 2
17 3 3 Your keyword 1
20 1 1 The two keywords are completely identical 1
21 2 2 Search engine query 1
23 4 1 The two keywords are completely identical 4
24 5 2 Search engine query 3
25 3 3 Your keyword 1
26 2 1 Search engine query 2
27 3 2 The two keywords are completely identical 2
30 3 2 Your keyword 2
31 1 1 Search engine query 1
print(data['better_0'].unique())
len(data['better_0'].unique())
['Your keyword' 'The two keywords are completely identical'
 'Search engine query']
3

The majority vote of an array is simply the mode

data['better_0'].mode()
0    Search engine query
1           Your keyword
dtype: object

How is the variable distributed?

data.groupby('better_0')['better_0'].size()
better_0
Search engine query                          22
The two keywords are completely identical    19
Your keyword                                 22
Name: better_0, dtype: int64

Let's compute the majority voting

data.groupby('_unit_id')['better_0'].apply(lambda x: x.mode())
_unit_id   
1         0    The two keywords are completely identical
          1                                 Your keyword
2         0                                 Your keyword
3         0    The two keywords are completely identical
          1                                 Your keyword
4         0    The two keywords are completely identical
6         0    The two keywords are completely identical
7         0                                 Your keyword
10        0                          Search engine query
          1    The two keywords are completely identical
13        0                                 Your keyword
14        0                          Search engine query
15        0                                 Your keyword
16        0                          Search engine query
17        0                          Search engine query
          1    The two keywords are completely identical
          2                                 Your keyword
20        0    The two keywords are completely identical
21        0                          Search engine query
          1    The two keywords are completely identical
23        0    The two keywords are completely identical
24        0                          Search engine query
25        0                          Search engine query
          1    The two keywords are completely identical
          2                                 Your keyword
26        0                          Search engine query
27        0    The two keywords are completely identical
30        0                                 Your keyword
31        0                          Search engine query
Name: better_0, dtype: object

Sometimes this returns two values, let's get the first in that case (better way would be random)

data.groupby('_unit_id')['better_0'].apply(lambda x: x.mode()[0])
_unit_id
1     The two keywords are completely identical
2                                  Your keyword
3     The two keywords are completely identical
4     The two keywords are completely identical
6     The two keywords are completely identical
7                                  Your keyword
10                          Search engine query
13                                 Your keyword
14                          Search engine query
15                                 Your keyword
16                          Search engine query
17                          Search engine query
20    The two keywords are completely identical
21                          Search engine query
23    The two keywords are completely identical
24                          Search engine query
25                          Search engine query
26                          Search engine query
27    The two keywords are completely identical
30                                 Your keyword
31                          Search engine query
Name: better_0, dtype: object

Weighted measures

Weighted mean

def weigthed_mean(df,weights,values): #df is a dataframe containing a single question
    sum_values = (df[weights]*df[values]).sum()
    total_weight = df[weights].sum()
    return sum_values/total_weight
data.groupby('_unit_id').apply(lambda x: weigthed_mean(x,'_trust','similarity_0'))
_unit_id
1     5.532764
2     4.961362
3     6.806888
4     5.789938
6     6.955167
7     5.675547
10    5.739468
13    6.437989
14    4.000000
15    5.166934
16    4.175357
17    5.840521
20    7.000000
21    7.000000
23    6.465985
24    6.481138
25    4.525120
26    4.556271
27    4.706914
30    4.415340
31    4.000000
dtype: float64
data.groupby('_unit_id').apply(lambda x: (x['_trust']*x['similarity_0']).sum()/(x['_trust'].sum()))
_unit_id
1     5.532764
2     4.961362
3     6.806888
4     5.789938
6     6.955167
7     5.675547
10    5.739468
13    6.437989
14    4.000000
15    5.166934
16    4.175357
17    5.840521
20    7.000000
21    7.000000
23    6.465985
24    6.481138
25    4.525120
26    4.556271
27    4.706914
30    4.415340
31    4.000000
dtype: float64

Weighted majority voting

Now we need, for each unit, to find the category with the highest trust score

data.head()
better_0 _unit_id _started_at _created_at _trust _worker_id _city age similarity_0 explanation_0 asi1 time_spent
0 Your keyword 4 1/10/2018 15:55:41 1/10/2018 16:20:41 0.385118 32 Ernakulam 36-50 6 they are all dressed well and using computers ... 4 00:25:00
1 The two keywords are completely identical 6 1/10/2018 17:04:22 1/10/2018 17:23:42 0.033270 13 Kolkata 36-50 6 Almost identicalexcept the tiny spelling diffe... 3 00:19:20
4 The two keywords are completely identical 6 1/11/2018 05:14:03 1/11/2018 05:21:25 0.708808 70 Mangalagiri 19-25 7 both are similar 5 00:07:22
5 The two keywords are completely identical 20 1/10/2018 16:41:16 1/10/2018 17:06:26 0.899786 95 Patna 26-35 7 they both describe the same kind of people 3 00:25:10
6 Your keyword 13 1/10/2018 15:47:20 1/10/2018 16:01:19 0.873825 37 Ulhasnagar 19-25 6 We can see a relaxed state in that images 5 00:13:59
def weigthed_majority(df,weights,values): #df is a dataframe containing a single question
    #print(df.groupby(values)[weights].sum())
    best_value = df.groupby(values)[weights].sum().argmax()
    return best_value
data.groupby('_unit_id').apply(lambda x: weigthed_majority(x,'_trust','better_0'))
/srv/paws/lib/python3.6/site-packages/ipykernel_launcher.py:3: FutureWarning: 'argmax' is deprecated. Use 'idxmax' instead. The behavior of 'argmax' will be corrected to return the positional maximum in the future. Use 'series.values.argmax' to get the position of the maximum now.
  This is separate from the ipykernel package so we can avoid doing imports until
_unit_id
1     The two keywords are completely identical
2                                  Your keyword
3     The two keywords are completely identical
4     The two keywords are completely identical
6     The two keywords are completely identical
7                           Search engine query
10                          Search engine query
13                                 Your keyword
14                          Search engine query
15                                 Your keyword
16                          Search engine query
17    The two keywords are completely identical
20    The two keywords are completely identical
21                          Search engine query
23    The two keywords are completely identical
24                          Search engine query
25    The two keywords are completely identical
26                          Search engine query
27    The two keywords are completely identical
30                                 Your keyword
31                          Search engine query
dtype: object

Creating a summary table

results = pd.DataFrame()
results['better'] = data.groupby('_unit_id').apply(lambda x: weigthed_majority(x,'_trust','better_0'))
results['similarity'] = data.groupby('_unit_id').apply(lambda x: weigthed_mean(x,'_trust','similarity_0'))
results['better_code'] = results['better'].astype('category').cat.codes
results
/srv/paws/lib/python3.6/site-packages/ipykernel_launcher.py:3: FutureWarning: 'argmax' is deprecated. Use 'idxmax' instead. The behavior of 'argmax' will be corrected to return the positional maximum in the future. Use 'series.values.argmax' to get the position of the maximum now.
  This is separate from the ipykernel package so we can avoid doing imports until
better similarity better_code
_unit_id
1 The two keywords are completely identical 5.532764 1
2 Your keyword 4.961362 2
3 The two keywords are completely identical 6.806888 1
4 The two keywords are completely identical 5.789938 1
6 The two keywords are completely identical 6.955167 1
7 Search engine query 5.675547 0
10 Search engine query 5.739468 0
13 Your keyword 6.437989 2
14 Search engine query 4.000000 0
15 Your keyword 5.166934 2
16 Search engine query 4.175357 0
17 The two keywords are completely identical 5.840521 1
20 The two keywords are completely identical 7.000000 1
21 Search engine query 7.000000 0
23 The two keywords are completely identical 6.465985 1
24 Search engine query 6.481138 0
25 The two keywords are completely identical 4.525120 1
26 Search engine query 4.556271 0
27 The two keywords are completely identical 4.706914 1
30 Your keyword 4.415340 2
31 Search engine query 4.000000 0

Free text

Now we analyse the case in which we have free text

data['better_0'].unique()
array(['Your keyword', 'The two keywords are completely identical',
       'Search engine query'], dtype=object)
data['explanation_0'].unique()
array(['they are all dressed well and using computers so its more like a  business scenario.',
       'Almost identicalexcept the tiny spelling difference.',
       'both are similar', 'they both describe the same kind of people',
       'We can see a relaxed state in that images', 'YES',
       'A person is generalized and one cannot find the images of Einstein or kids in them.',
       'they are calm', 'genious', 'only 1 image',
       'interested in their work',
       'i think this is correct that calm person because every one is calm in this images',
       'images looks like taking a deep breath',
       'it now seems more like to give these results whn we think of interested person rather than thinking and surprising',
       'based on result of image', 'whipping', 'Yes',
       'calm person and calmness same',
       'result suits more to this kerword', 'yes', 'anger',
       'hot air baloon', 'both are the same', 'same attitude of boss',
       'the results are same',
       'all my words are feature of Search engine query',
       'They all are working in the office',
       'in image person looking very casual',
       'both refer to the same traits but intelligent word is more suited',
       'i know', 'Because all people here look casual.', 'both are same',
       'Casualness is used in both the words',
       'interested person only can do Research, smart, thinging',
       'Casual person is more accurate of the images.',
       'i believe this is my personal theory..so i think aggressive person would be better keyword for these images',
       'My keyword "happy people" and Search engine query "calm person" is almost same.',
       'My answer is more specific regarding images.',
       'i know need search engine when i already knew it',
       'BOTH ARE SIMILAR',
       'by query image i understood that person seems very angry',
       'Everything is related with warm',
       'it gives better ideas about all the image',
       'we got the same image when search in google',
       'with the facial expression we can find him too aggresive',
       'very much about that', "It's the image of that",
       'Smart Person Bring Innovation and must have high IQ',
       'person in aggression is shouting at others',
       'they are all were casual dress', 'By nature',
       'because it shows that',
       'people are working i guess working people is more apt',
       'They also look happy',
       'On detailed viewing smart person might be a better keyword.',
       'everybody is yelling',
       'a complete act of expression works out here'], dtype=object)

We can't use the weighted majority voting here! We need first to assign a score to this values.

Exercise

  • Create a function that assigns a score to each value of the column 'explanation_0' (for example the text lenght len(text)) look here for reference https://pandas.pydata.org/pandas-docs/stable/text.html
  • create a column with this score
  • generate a weighted mean for it (using '_trust')
def compute_score(text):
    score = len(text)
    return score
data['score'] = data['explanation_0'].apply(compute_score)
data.groupby('_unit_id').apply(lambda x: weigthed_mean(x,'_trust','score'))
48.263201486681275

Exercise

  1. aggregate per _unit_id using average means for time_spent (you need to apply pd.to_numeric() and divide by 1e9 to get a column in seconds
  2. create a code that assigns 1 if the text contains any element for a list of words (list_words=['similar','same'], by doing a for loop (for i in list_words) and checking with (if i in text)
  3. compute the weigthed mean for that