{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_list = [4, 7, 4, 0, -3, 4, 7]\n", "x_tuple = (4, 7, 4, 0, -3, 4, 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute means explictly from the definition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list_total = 0\n", "for x in x_list:\n", " list_total += x\n", " \n", "tuple_total = 0\n", "for t in x_tuple:\n", " tuple_total += t\n", " \n", "list_average = list_total/len(x_list)\n", "tuple_average = tuple_total/len(x_tuple)\n", " \n", "print(f'The average of x_list = {list_average:.2f}')\n", "print(f'The average of x_tuple = {tuple_average:.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## statistics.mean function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import statistics\n", "\n", "mean_list = statistics.mean(x_list)\n", "mean_tuple = statistics.mean(x_tuple)\n", "\n", "print(f'statistics.mean(x_list) = {mean_list:.2f}')\n", "print(f'statistics.mean(x_tuple) = {mean_tuple:.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a NumPy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "x_array = np.array(x_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.mean and np.ndarray.mean functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np_mean_list = np.mean(x_list) # operate on a list\n", "np_mean_tuple = np.mean(x_tuple) # operate on a list\n", "np_mean_array = np.mean(x_array) # operate on an array\n", "ndarray_mean_array = np.ndarray.mean(x_array) # operate on an array only\n", "\n", "print(f'np.mean(x_list) = {np_mean_list:.2f}')\n", "print(f'np.mean(x_tuple) = {np_mean_tuple:.2f}')\n", "print(f'np.mean(x_array) = {np_mean_array:.2f}')\n", "print(f'np.ndarray.mean(x_array) = {ndarray_mean_array:.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2-d array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = np.array([[2, 5, 6, 3],\n", " [1, 7, 0, 4],\n", " [3, 1, 2, 6]])\n", "\n", "mean_of_all = np.mean(matrix)\n", "mean_of_cols = np.mean(matrix, axis=0)\n", "mean_of_rows = np.mean(matrix, axis=1)\n", "\n", "print(f'np.mean(matrix) [all] = {mean_of_all}')\n", "print(f'np.mean(matrix, axis=0) [cols] = {mean_of_cols}')\n", "print(f'np.mean(matrix, axis=1) [rows] = {mean_of_rows}')" ] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }