251 lines
49 KiB
Plaintext
251 lines
49 KiB
Plaintext
|
{
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0,
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"provenance": []
|
||
|
},
|
||
|
"kernelspec": {
|
||
|
"name": "python3",
|
||
|
"display_name": "Python 3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"name": "python"
|
||
|
}
|
||
|
},
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"source": [
|
||
|
"Importing the libraries"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"id": "sCd8w2OwBTLm"
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"id": "kWsq3C9zxvlr"
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import csv\n",
|
||
|
"import math\n",
|
||
|
"from matplotlib import pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"source": [
|
||
|
"Create the BasicStatistics class"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"id": "Bw9qskz1BXha"
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"source": [
|
||
|
"class BasicStatistics:\n",
|
||
|
" \"\"\"\n",
|
||
|
" Basic statistics class\n",
|
||
|
"\n",
|
||
|
" Attributes:\n",
|
||
|
" dataset: list of numbers\n",
|
||
|
"\n",
|
||
|
" Methods:\n",
|
||
|
" mean: calculates the mean of the dataset\n",
|
||
|
" median: calculates the median of the dataset\n",
|
||
|
" variance: calculates the variance of the dataset\n",
|
||
|
" normalize: normalizes the dataset\n",
|
||
|
" plot: plots the dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" def __init__(self, dataset):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Constructor for the BasicStatistics class\n",
|
||
|
" :param dataset: list of numbers\n",
|
||
|
" \"\"\"\n",
|
||
|
" self.dataset = dataset\n",
|
||
|
"\n",
|
||
|
" def mean(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Calculates the mean of the dataset\n",
|
||
|
" :return: mean of the dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" return sum(self.dataset) / len(self.dataset)\n",
|
||
|
"\n",
|
||
|
" def median(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Calculates the median of the dataset\n",
|
||
|
" :return: median of the dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" self.dataset.sort()\n",
|
||
|
" if len(self.dataset) % 2 == 0:\n",
|
||
|
" return (self.dataset[len(self.dataset) // 2] + self.dataset[len(self.dataset) // 2 - 1]) / 2\n",
|
||
|
" else:\n",
|
||
|
" return self.dataset[len(self.dataset) // 2]\n",
|
||
|
"\n",
|
||
|
" def variance(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Calculates the variance of the dataset\n",
|
||
|
" :return: variance of the dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" return sum([(x - self.mean()) ** 2 for x in self.dataset]) / len(self.dataset)\n",
|
||
|
"\n",
|
||
|
" def normalize(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Normalizes the dataset\n",
|
||
|
" :return normalized dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" return [(x - self.mean()) / self.variance() for x in self.dataset]\n",
|
||
|
"\n",
|
||
|
" def standard_deviation(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Calculates the standard deviation of the dataset\n",
|
||
|
" :return: standard deviation of the dataset\n",
|
||
|
" \"\"\"\n",
|
||
|
" return math.sqrt(self.variance())\n",
|
||
|
"\n",
|
||
|
" def plot(self):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Plots the dataset\n",
|
||
|
" :return:\n",
|
||
|
" \"\"\"\n",
|
||
|
" # get the data\n",
|
||
|
" mean = self.mean()\n",
|
||
|
" median = self.median()\n",
|
||
|
" variance = self.variance()\n",
|
||
|
" std = math.sqrt(variance)\n",
|
||
|
" data_norm = self.normalize()\n",
|
||
|
" # get the number of bins\n",
|
||
|
" nrBins = math.floor(len(self.dataset) / 50)\n",
|
||
|
"\n",
|
||
|
" # set up figure and plot grid\n",
|
||
|
" fig = plt.figure(figsize=(10, 8))\n",
|
||
|
" grid = plt.GridSpec(2, 2)\n",
|
||
|
" ax1 = plt.subplot(grid[:1, :])\n",
|
||
|
" ax2 = plt.subplot(grid[1:, :1])\n",
|
||
|
" ax3 = plt.subplot(grid[1:, 1:])\n",
|
||
|
"\n",
|
||
|
" # set titles\n",
|
||
|
" fig.suptitle('Basic Statistics')\n",
|
||
|
" ax1.set_title('Data Distribution')\n",
|
||
|
" ax2.set_title('Raw Data')\n",
|
||
|
" ax3.set_title('Normalized Data')\n",
|
||
|
"\n",
|
||
|
" # set axes\n",
|
||
|
" ax1.set_xlabel('Values')\n",
|
||
|
" ax1.set_ylabel('Frequency')\n",
|
||
|
" ax2.set_xlabel('Sample')\n",
|
||
|
" ax2.set_ylabel('Value')\n",
|
||
|
" ax3.set_xlabel('Sample')\n",
|
||
|
" ax3.set_ylabel('Standardized Value')\n",
|
||
|
"\n",
|
||
|
" # data plotting\n",
|
||
|
" ax1.hist(data, bins=nrBins, density=True, label='Histogram')\n",
|
||
|
" ax1.vlines(x=mean, ymin=0, ymax=0.3, colors='r', ls='--', label='Mean')\n",
|
||
|
" ax1.vlines(x=median, ymin=0, ymax=0.3, colors='y', ls='--', label='Median')\n",
|
||
|
" ax1.vlines(x=mean + std, ymin=0, ymax=0.3, colors='g', ls='--', label='Standard deviation')\n",
|
||
|
" ax1.vlines(x=mean - std, ymin=0, ymax=0.3, colors='g', ls='--')\n",
|
||
|
" ax1.legend()\n",
|
||
|
" # raw data plotting\n",
|
||
|
" x = [i for i in range(1, len(data) + 1)]\n",
|
||
|
" ax2.scatter(x, data, s=3, label='Data')\n",
|
||
|
" ax2.hlines(y=mean, xmin=0, xmax=len(data), colors='r', ls='--', label='Mean')\n",
|
||
|
" ax2.hlines(y=mean + std, xmin=0, xmax=len(data), colors='g', ls='--', label='Standard deviation')\n",
|
||
|
" ax2.hlines(y=mean - std, xmin=0, xmax=len(data), colors='g', ls='--')\n",
|
||
|
" ax2.legend()\n",
|
||
|
" # normalized data plotting\n",
|
||
|
" ax3.scatter(x, data_norm, s=3, label='Data')\n",
|
||
|
" ax3.hlines(y=0, xmin=0, xmax=len(data), colors='r', ls='--', label='Mean')\n",
|
||
|
" ax3.hlines(y=0 + 1, xmin=0, xmax=len(data), colors='g', ls='--', label='Standard deviation')\n",
|
||
|
" ax3.hlines(y=0 - 1, xmin=0, xmax=len(data), colors='g', ls='--')\n",
|
||
|
" ax3.legend()\n",
|
||
|
"\n",
|
||
|
" plt.show()\n",
|
||
|
" # save the figure\n",
|
||
|
" fig.savefig(\"Basic_Statistics.png\", format=\"png\")\n"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"id": "cqSJ_htS_04g"
|
||
|
},
|
||
|
"execution_count": 3,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"source": [
|
||
|
"Demonstrate the BasicStatistics class on the data.csv file"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"id": "xeMDhdMqBd2n"
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"source": [
|
||
|
"# define the path to the data file\n",
|
||
|
"path = 'data.csv'\n",
|
||
|
"# open the file and read the data\n",
|
||
|
"with open(path, 'r') as f:\n",
|
||
|
" # create a csv reader\n",
|
||
|
" reader = csv.reader(f)\n",
|
||
|
" # convert the data to a list of floats\n",
|
||
|
" data = list(reader)\n",
|
||
|
" data = [float(x[0]) for x in data]\n",
|
||
|
" # create a BasicStatistics object\n",
|
||
|
" bs = BasicStatistics(data)\n",
|
||
|
" # print the mean, median, and variance\n",
|
||
|
" print(f'Mean: {bs.mean():.2f}')\n",
|
||
|
" print(f'Median: {bs.median():.2f}')\n",
|
||
|
" print(f'Variance: {bs.variance():.2f}')\n",
|
||
|
" # plot the data\n",
|
||
|
" bs.plot()"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/",
|
||
|
"height": 606
|
||
|
},
|
||
|
"id": "UQVfNd_j_29Z",
|
||
|
"outputId": "e3cab288-537c-4937-b8e0-a7473770400d"
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"name": "stdout",
|
||
|
"text": [
|
||
|
"Mean: 4.08\n",
|
||
|
"Median: 4.13\n",
|
||
|
"Variance: 4.15\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "display_data",
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 720x576 with 3 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmgAAAIZCAYAAAASkcMtAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAAsTAAALEwEAmpwYAAB7GUlEQVR4nO3deXwV1f3/8dfHgISAAgIqq2BVQASDRlSsFoUqKipWKuBS0faL4v6rtlXrXq20ta3FpUpbi1XZ6ooIbQXBFdEgUdlURDY3FgVkUwKf3x8ziTfJTXJD7s3ce/N+Ph73kTtzZvlkGE4+c+bMGXN3RERERCR97BZ1ACIiIiJSlhI0ERERkTSjBE1EREQkzShBExEREUkzStBERERE0owSNBEREZE0owRNRNKemU0zswuijqM8M7vBzP6+i+tuMrP9kx2TiGQH0zhoIpIsZrYM2AfYAWwHXgcucfeVEcVzBnAbsD/wLfAu8FN3/9jMbgUOcPfzEtxWX+Axd2+/C3HMCtfdpWROROoftaCJSLKd5u5NgTbAF8C9UQRhZgcA/wKuAZoBnYH7CZJHEZG0pgRNRFLC3bcBTwAHl8wzs1PNbJ6ZbTSzlWErVklZ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f8Of64zs7fd/WvgyjCmrwiSx8kx+11M0GduafhEZdty+6sq/r8Ag83sKzMbHSfWa4H3gLeAL4HfEdTPCR0TEck+GqhWREREJM2oBU1EREQkzShBExEREUkzStBERERE0owSNBEREZE0owRNREREJM0oQRMRERFJM0rQRERERNKMEjQRERGRNKMETURERCTNKEETERERSTNK0ERERETSjBI0ERERkTSjBE1EREQkzShBExEREUkzStBERERE0owSNBEREZE0owRNREREJM0oQRMRERFJM0rQRERERNKMEjQRERGRNKMETURERCTNKEETERERSTNK0ERERETSjBI0ERERkTSjBE1EREQkzShBExEREUkzStBERERE0owSNBEREZE0owRNREREJM0oQRMRERFJM0rQRERERNKMEjQRqdfMbJqZXZCkbR1rZu/HTC8zs/7J2Ha4vQVm1jdZ2xOR9KUETURSIkxOtprZ12a23sxeN7NLzCyhesfMOpmZm1mDWsTgZrbZzDaZ2Tozm2FmQ2KXcfeT3f2RBLd1QFXLuPsr7t5lV+Mtt7+xZnZHue13d/dZydi+iKQ3JWgikkqnufsewH7AKOBXwD/qOIZD3b0p0AUYC9xnZrckeye1SSRFRMpTgiYiKefuG9x9MjAEuMDMDgEws1PNbJ6ZbTSzlWZ2a8xqL4c/14ctYEeb2ffM7MWwNWytmT1uZs0TjGGtuz8KjASuN7OWYQyzzOxn4fcDzOwlM9sQbn9iOL8klnfCWIaYWV8zW2VmvzKzz4F/lswrt+sjzGyhmX1lZv80s9xwm8PN7NXYBUta6cxsBHAu8Mtwf8+F5aW3TM2skZndY2afhp97zKxRWFYS2zVmttrMPjOzCxM5TiKSHpSgiUidcfc3gVXAseGszcBPgObAqcBIMxsUlh0X/mzu7k3dfTZgwF1AW6Ab0AG4tYZhPAs0AHrHKfsN8D+gBdAeuDeMuySWQ8NYJobT+wJ7EbQQjqhkf+cCJwHfAw4CbqwuQHcfAzwO/D7c32lxFvs1cBSQDxwa/j6x294XaAa0A34K3G9mLarbt4ikByVoIlLXPiVIanD3We7+nrvvdPd3gfHADypb0d2XuPsL7v6Nu68B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Yywbds2WrZsqeSsHjMzWrZsuUutqKnsg9YbWOLuS939W2ACcEbsAu6+MWayCVDy5vYzgAnu/o27fwwsCbcnIiKSMZScya6eA6m8xdkOWBkzvQo4svxCZnYZ8HNgd+CEmHXfKLduuzjrjgBGAHTs2DEpQYtkk+fefw6A07qcFnEkWeS54JiuPTq
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"source": [
|
||
|
"# New Section"
|
||
|
],
|
||
|
"metadata": {
|
||
|
"id": "p4TrEpvsABUU"
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
}
|