{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "af977a9d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([37.2, 37.6, 36.8])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "X = np.array([[1.2, 1.5, 1.8],\n", " [1.3, 1.4, 1.9],\n", " [1.1, 1.6, 1.7]])\n", "y = np.array([5, 10, 9]).T\n", "\n", "\n", "\n", "\n", "\n", "X.dot(y)\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "9bdc397c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 10, 9])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 3, "id": "f0f0b081", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.shape" ] }, { "cell_type": "code", "execution_count": 4, "id": "6e4391e4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "790 ns ± 28.8 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n" ] } ], "source": [ "%timeit X.dot(y)" ] }, { "cell_type": "code", "execution_count": 5, "id": "20fe1bd1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13.7 µs ± 1.29 µs per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n" ] } ], "source": [ "%%timeit\n", "total = []\n", "for i in range(X.shape[0]):\n", " each_price = 0\n", " for j in range(X.shape[1]):\n", " each_price += X[i,j] * y[j]\n", "# each_price = each_price + X[i,j] * y[j]\n", " total.append(round(each_price, 1))\n", "total " ] }, { "cell_type": "code", "execution_count": null, "id": "4334b3ce", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }