Returns the reliability of agreement as a number between -1.0 and +1.0, for a number of votes per category per task. The given m is a list in which each row represents a task. Each task is a list with the number of votes per category. Each column represents a categor
(m)
| 214 | # -1.0 = total disagreement |
| 215 | |
| 216 | def fleiss_kappa(m): |
| 217 | """ Returns the reliability of agreement as a number between -1.0 and +1.0, |
| 218 | for a number of votes per category per task. |
| 219 | The given m is a list in which each row represents a task. |
| 220 | Each task is a list with the number of votes per category. |
| 221 | Each column represents a category. |
| 222 | For example, say 5 people are asked to vote "cat" and "dog" as "good" or "bad": |
| 223 | m = [# + - |
| 224 | [3,2], # cat |
| 225 | [5,0]] # dog |
| 226 | """ |
| 227 | N = len(m) # Total number of tasks. |
| 228 | n = sum(m[0]) # The number of votes per task. |
| 229 | k = len(m[0]) # The number of categories. |
| 230 | if n == 1: |
| 231 | return 1.0 |
| 232 | assert all(sum(row) == n for row in m[1:]), "numer of votes for each task differs" |
| 233 | # p[j] = the proportion of all assignments which were to the j-th category. |
| 234 | p = [sum(m[i][j] for i in xrange(N)) / float(N*n) for j in xrange(k)] |
| 235 | # P[i] = the extent to which voters agree for the i-th subject. |
| 236 | P = [(sum(m[i][j]**2 for j in xrange(k)) - n) / float(n * (n-1)) for i in xrange(N)] |
| 237 | # Pm = the mean of P[i] and Pe. |
| 238 | Pe = sum(pj**2 for pj in p) |
| 239 | Pm = sum(P) / N |
| 240 | K = (Pm - Pe) / ((1 - Pe) or 1) # kappa |
| 241 | return K |
| 242 | |
| 243 | agreement = fleiss_kappa |
| 244 |