diff options
author | 2024-02-23 20:30:35 -0800 | |
---|---|---|
committer | 2024-02-23 20:30:35 -0800 | |
commit | ce6ce0d793640068dcf54855103ee65964927a93 (patch) | |
tree | 529cb9becbfce14793273ea7bb224c0e6a2c43e1 /CS105MiniProject.ipynb | |
parent | 7d9962d6e4d95419120e05a29e6462ad2fb83953 (diff) | |
download | CS105MiniProject-ce6ce0d793640068dcf54855103ee65964927a93.tar.gz CS105MiniProject-ce6ce0d793640068dcf54855103ee65964927a93.tar.zst CS105MiniProject-ce6ce0d793640068dcf54855103ee65964927a93.zip |
Changes chi-squared code
Diffstat (limited to 'CS105MiniProject.ipynb')
-rw-r--r-- | CS105MiniProject.ipynb | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/CS105MiniProject.ipynb b/CS105MiniProject.ipynb index 3e15525..35b9887 100644 --- a/CS105MiniProject.ipynb +++ b/CS105MiniProject.ipynb @@ -55,8 +55,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.385493Z", - "start_time": "2024-02-24T02:30:50.364241Z" + "end_time": "2024-02-24T04:30:18.761606Z", + "start_time": "2024-02-24T04:30:18.740080Z" } }, "id": "b68b27041fdab1a5", @@ -126,8 +126,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.398700Z", - "start_time": "2024-02-24T02:30:50.386214Z" + "end_time": "2024-02-24T04:30:18.773188Z", + "start_time": "2024-02-24T04:30:18.762221Z" } }, "id": "3f72adcb3bc0285e", @@ -156,8 +156,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.408153Z", - "start_time": "2024-02-24T02:30:50.400240Z" + "end_time": "2024-02-24T04:30:18.781369Z", + "start_time": "2024-02-24T04:30:18.774710Z" } }, "id": "285236650ff590d8", @@ -182,8 +182,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.417032Z", - "start_time": "2024-02-24T02:30:50.408722Z" + "end_time": "2024-02-24T04:30:18.787562Z", + "start_time": "2024-02-24T04:30:18.782006Z" } }, "id": "6516c926e6efd1c3", @@ -224,8 +224,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.608877Z", - "start_time": "2024-02-24T02:30:50.418071Z" + "end_time": "2024-02-24T04:30:19.032770Z", + "start_time": "2024-02-24T04:30:18.788319Z" } }, "id": "6deea60d8966fa15", @@ -258,8 +258,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.805764Z", - "start_time": "2024-02-24T02:30:50.611563Z" + "end_time": "2024-02-24T04:30:19.179017Z", + "start_time": "2024-02-24T04:30:19.034991Z" } }, "id": "c6372820e5ee501f", @@ -294,8 +294,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:50.904997Z", - "start_time": "2024-02-24T02:30:50.807674Z" + "end_time": "2024-02-24T04:30:19.283024Z", + "start_time": "2024-02-24T04:30:19.181016Z" } }, "id": "67df9b48e43a5307", @@ -321,8 +321,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:51.009476Z", - "start_time": "2024-02-24T02:30:50.906524Z" + "end_time": "2024-02-24T04:30:19.368513Z", + "start_time": "2024-02-24T04:30:19.284400Z" } }, "id": "1163d27db8106025", @@ -368,8 +368,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:51.029851Z", - "start_time": "2024-02-24T02:30:51.010794Z" + "end_time": "2024-02-24T04:30:19.387872Z", + "start_time": "2024-02-24T04:30:19.371005Z" } }, "id": "5cbb7ab4d38de9ef", @@ -382,36 +382,30 @@ "name": "stdout", "output_type": "stream", "text": [ - "Chi-squared Value: 4.183390044200403\n", - "Degrees of Freedom: 6\n" + "Chi-squared value: 4.183390044200403\n" ] } ], "source": [ - "# Extract the observed values from the contingency table\n", - "observed_values = roommates_major_table.iloc[:-1, :-1].values\n", + "num_rows, num_cols = roommates_major_table.shape\n", + "# Initialize expected frequencies\n", + "expected_frequencies = []\n", + "chi_squared = 0\n", + "for i in range(num_rows - 1):\n", + " row_totals = roommates_major_table.iloc[i, -1]\n", + " for j in range(num_cols - 1):\n", + " col_totals = roommates_major_table.iloc[-1, j]\n", + " expected_frequency = (row_totals * col_totals) / roommates_major_table.iloc[-1, -1]\n", + " expected_frequencies.append(expected_frequency)\n", + " chi_squared += ((roommates_major_table.iloc[i, j] - expected_frequency)**2) / expected_frequency\n", "\n", - "# Calculate expected values\n", - "row_totals = roommates_major_table.iloc[:-1, -1].values\n", - "col_totals = roommates_major_table.iloc[-1, :-1].values\n", - "total = np.sum(row_totals)\n", - "\n", - "expected_values = np.outer(row_totals, col_totals) / total\n", - "\n", - "# Calculate chi-squared statistic\n", - "chi2_statistic = np.sum((observed_values - expected_values) ** 2 / expected_values)\n", - "\n", - "# Degrees of freedom\n", - "degrees_of_freedom = (roommates_major_table.shape[0] - 1) * (roommates_major_table.shape[1] - 1)\n", - "\n", - "# Print results\n", - "print(f\"Chi-squared Value: {chi2_statistic}\\nDegrees of Freedom: {degrees_of_freedom}\")" + "print(\"Chi-squared value:\", chi_squared)" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-02-24T02:30:51.035050Z", - "start_time": "2024-02-24T02:30:51.030607Z" + "end_time": "2024-02-24T04:30:19.394678Z", + "start_time": "2024-02-24T04:30:19.389670Z" } }, "id": "2fbaac2d0722a7e3", |