Guest User

Untitled

a guest
Oct 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "data": {
  10. "text/plain": [
  11. "array([[50, 40, 20],\n",
  12. " [30, 25, 15]])"
  13. ]
  14. },
  15. "execution_count": 1,
  16. "metadata": {},
  17. "output_type": "execute_result"
  18. }
  19. ],
  20. "source": [
  21. "import numpy as np\n",
  22. "table = np.array([[50,40,20],[30,25,15]])\n",
  23. "table"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 2,
  29. "metadata": {},
  30. "outputs": [
  31. {
  32. "name": "stdout",
  33. "output_type": "stream",
  34. "text": [
  35. "[80 65 35]\n",
  36. "[110 70]\n",
  37. "180\n"
  38. ]
  39. }
  40. ],
  41. "source": [
  42. "print(table.sum(axis=0))\n",
  43. "print(table.sum(axis=1))\n",
  44. "print(table.sum())"
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "execution_count": 3,
  50. "metadata": {},
  51. "outputs": [
  52. {
  53. "name": "stdout",
  54. "output_type": "stream",
  55. "text": [
  56. "[ 0.44444444 0.36111111 0.19444444]\n",
  57. "[ 0.61111111 0.38888889]\n"
  58. ]
  59. }
  60. ],
  61. "source": [
  62. "n = table.sum()\n",
  63. "col = table.sum(axis=0)/n\n",
  64. "row = table.sum(axis=1)/n\n",
  65. "print(col)\n",
  66. "print(row)"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 4,
  72. "metadata": {},
  73. "outputs": [
  74. {
  75. "name": "stdout",
  76. "output_type": "stream",
  77. "text": [
  78. "(0, 0) 50\n",
  79. "(0, 1) 40\n",
  80. "(0, 2) 20\n",
  81. "(1, 0) 30\n",
  82. "(1, 1) 25\n",
  83. "(1, 2) 15\n"
  84. ]
  85. }
  86. ],
  87. "source": [
  88. "for index, x in np.ndenumerate(table):\n",
  89. " print(index, x)"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": 5,
  95. "metadata": {},
  96. "outputs": [
  97. {
  98. "name": "stdout",
  99. "output_type": "stream",
  100. "text": [
  101. "[[ 48.88888889 39.72222222 21.38888889]\n",
  102. " [ 31.11111111 25.27777778 13.61111111]]\n"
  103. ]
  104. }
  105. ],
  106. "source": [
  107. "expected = np.empty(table.shape)\n",
  108. "for index, x in np.ndenumerate(table):\n",
  109. " pA = row[index[0]]\n",
  110. " pB = col[index[1]]\n",
  111. " expected[index] = pA*pB*n\n",
  112. "print(expected)"
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 6,
  118. "metadata": {},
  119. "outputs": [
  120. {
  121. "data": {
  122. "text/plain": [
  123. "0.30184101612673114"
  124. ]
  125. },
  126. "execution_count": 6,
  127. "metadata": {},
  128. "output_type": "execute_result"
  129. }
  130. ],
  131. "source": [
  132. "(((table-expected)**2)/expected).sum()"
  133. ]
  134. },
  135. {
  136. "cell_type": "code",
  137. "execution_count": 7,
  138. "metadata": {},
  139. "outputs": [
  140. {
  141. "name": "stdout",
  142. "output_type": "stream",
  143. "text": [
  144. "5.99146454711\n"
  145. ]
  146. }
  147. ],
  148. "source": [
  149. "from scipy.stats import chi2\n",
  150. "chi = chi2.isf(q=0.05, df=2)\n",
  151. "print(chi)"
  152. ]
  153. }
  154. ],
  155. "metadata": {
  156. "kernelspec": {
  157. "display_name": "Python 3",
  158. "language": "python",
  159. "name": "python3"
  160. },
  161. "language_info": {
  162. "codemirror_mode": {
  163. "name": "ipython",
  164. "version": 3
  165. },
  166. "file_extension": ".py",
  167. "mimetype": "text/x-python",
  168. "name": "python",
  169. "nbconvert_exporter": "python",
  170. "pygments_lexer": "ipython3",
  171. "version": "3.6.1"
  172. }
  173. },
  174. "nbformat": 4,
  175. "nbformat_minor": 2
  176. }
Add Comment
Please, Sign In to add comment