{ "cells": [ { "cell_type": "code", "execution_count": 41, "id": "fd50f54d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([37.2, 37.6, 36.8])" ] }, "execution_count": 41, "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", "\n", "Y = np.array([5,10,9]).T\n", "\n", "X.dot(Y)" ] }, { "cell_type": "code", "execution_count": 42, "id": "8b22cb4a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.shape" ] }, { "cell_type": "code", "execution_count": 43, "id": "bbef7ab3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "963 ns ± 20.9 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": 37, "id": "33143bac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16.3 µs ± 173 ns 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": "markdown", "id": "531e04e3", "metadata": {}, "source": [ "# 第二题" ] }, { "cell_type": "code", "execution_count": 47, "id": "e8c1b618", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8, 1, 7, 8,\n", " 7, 2, 1, 2, 9, 9, 4, 9])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(1)\n", "X = np.random.randint(1, 10, size=30)\n", "X" ] }, { "cell_type": "code", "execution_count": 48, "id": "42e4691b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9, 6],\n", " [1, 1, 2],\n", " [8, 7, 3],\n", " [5, 6, 3],\n", " [5, 3, 5],\n", " [8, 8, 2],\n", " [8, 1, 7],\n", " [8, 7, 2],\n", " [1, 2, 9],\n", " [9, 4, 9]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.reshape(-1,3)" ] }, { "cell_type": "code", "execution_count": 49, "id": "2fcdc70b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9, 6],\n", " [1, 1, 2],\n", " [8, 7, 3],\n", " [5, 6, 3],\n", " [5, 3, 5],\n", " [8, 8, 2],\n", " [8, 1, 7],\n", " [8, 7, 2],\n", " [1, 2, 9],\n", " [9, 4, 9]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X1 = X.reshape(-1 ,3)\n", "X1" ] }, { "cell_type": "code", "execution_count": 50, "id": "dd6264eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 = X1[:, 2]\n", "last1" ] }, { "cell_type": "code", "execution_count": 51, "id": "5c08677b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 <= 3" ] }, { "cell_type": "code", "execution_count": 52, "id": "3a0ab05d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 3, 2, 2])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp1 = last1[last1 <= 3]\n", "temp1" ] }, { "cell_type": "code", "execution_count": 53, "id": "2c55c6d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1, 2, 3, 5, 7], dtype=int64),)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(last1 <= 3)" ] }, { "cell_type": "code", "execution_count": 54, "id": "09e25915", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 55, "id": "5733adf3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 5])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp2 = last1[(last1 > 3) & (last1 <= 6)]\n", "temp2" ] }, { "cell_type": "code", "execution_count": 56, "id": "390d173f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([7, 9, 9])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp3 = last1[last1 > 6]\n", "temp3" ] }, { "cell_type": "code", "execution_count": 57, "id": "da7b652b", "metadata": {}, "outputs": [], "source": [ "last1[last1 <= 3] = 0" ] }, { "cell_type": "code", "execution_count": 58, "id": "d830c247", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 0, 0, 0, 5, 0, 7, 0, 9, 9])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 59, "id": "945ad281", "metadata": {}, "outputs": [], "source": [ "last1[(last1 > 3) & (last1 <= 6)] = 1" ] }, { "cell_type": "code", "execution_count": 60, "id": "732374a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 7, 0, 9, 9])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 61, "id": "85719b27", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1[last1 > 6] = 2\n", "last1" ] }, { "cell_type": "code", "execution_count": 62, "id": "e0b3baa5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9],\n", " [1, 1],\n", " [8, 7],\n", " [5, 6],\n", " [5, 3],\n", " [8, 8],\n", " [8, 1],\n", " [8, 7],\n", " [1, 2],\n", " [9, 4]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train = X1[:, 0:2]\n", "X_train" ] }, { "cell_type": "code", "execution_count": 63, "id": "05e1907c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y_train = last1\n", "Y_train" ] }, { "cell_type": "code", "execution_count": 65, "id": "130a4dda", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [8, 7],\n", " [5, 6],\n", " [8, 8],\n", " [8, 7]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[Y_train == 0]" ] }, { "cell_type": "code", "execution_count": 66, "id": "c33848c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y_train == 0" ] }, { "cell_type": "code", "execution_count": 67, "id": "a56448cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9],\n", " [5, 3]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[Y_train == 1]" ] }, { "cell_type": "code", "execution_count": 68, "id": "ac8a7b14", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[8, 1],\n", " [1, 2],\n", " [9, 4]])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[Y_train == 2]" ] }, { "cell_type": "code", "execution_count": null, "id": "b497e945", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }