Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[50, 40, 20],\n",
- " [30, 25, 15]])"
- ]
- },
- "execution_count": 1,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "import numpy as np\n",
- "table = np.array([[50,40,20],[30,25,15]])\n",
- "table"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[80 65 35]\n",
- "[110 70]\n",
- "180\n"
- ]
- }
- ],
- "source": [
- "print(table.sum(axis=0))\n",
- "print(table.sum(axis=1))\n",
- "print(table.sum())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 0.44444444 0.36111111 0.19444444]\n",
- "[ 0.61111111 0.38888889]\n"
- ]
- }
- ],
- "source": [
- "n = table.sum()\n",
- "col = table.sum(axis=0)/n\n",
- "row = table.sum(axis=1)/n\n",
- "print(col)\n",
- "print(row)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(0, 0) 50\n",
- "(0, 1) 40\n",
- "(0, 2) 20\n",
- "(1, 0) 30\n",
- "(1, 1) 25\n",
- "(1, 2) 15\n"
- ]
- }
- ],
- "source": [
- "for index, x in np.ndenumerate(table):\n",
- " print(index, x)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 48.88888889 39.72222222 21.38888889]\n",
- " [ 31.11111111 25.27777778 13.61111111]]\n"
- ]
- }
- ],
- "source": [
- "expected = np.empty(table.shape)\n",
- "for index, x in np.ndenumerate(table):\n",
- " pA = row[index[0]]\n",
- " pB = col[index[1]]\n",
- " expected[index] = pA*pB*n\n",
- "print(expected)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.30184101612673114"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "(((table-expected)**2)/expected).sum()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5.99146454711\n"
- ]
- }
- ],
- "source": [
- "from scipy.stats import chi2\n",
- "chi = chi2.isf(q=0.05, df=2)\n",
- "print(chi)"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment