{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "303095f4", "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)" ] }, { "cell_type": "code", "execution_count": 2, "id": "9670b1f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.shape" ] }, { "cell_type": "code", "execution_count": 3, "id": "b1ab79e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "797 ns ± 37.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" ] } ], "source": [ "%timeit X.dot(y)" ] }, { "cell_type": "code", "execution_count": 4, "id": "4d088725", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "17.9 µs ± 257 ns per loop (mean ± std. dev. of 7 runs, 100000 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": "df3dfeec", "metadata": {}, "source": [ "# #第二题" ] }, { "cell_type": "code", "execution_count": 5, "id": "81dc806e", "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": 5, "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": 7, "id": "adb7722a", "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": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.reshape(10,-1)" ] }, { "cell_type": "code", "execution_count": 8, "id": "6d0d6242", "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": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X1 = X.reshape(-1 ,3)\n", "X1" ] }, { "cell_type": "code", "execution_count": 9, "id": "a5854aeb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 = X1[:, 2]\n", "last1" ] }, { "cell_type": "code", "execution_count": 10, "id": "3c001e71", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 <= 3" ] }, { "cell_type": "code", "execution_count": 11, "id": "35b66010", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 3, 2, 2])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp1 = last1[last1 <= 3]\n", "temp1" ] }, { "cell_type": "code", "execution_count": 12, "id": "b41674c8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1, 2, 3, 5, 7], dtype=int64),)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(last1 <= 3)" ] }, { "cell_type": "code", "execution_count": 13, "id": "42b271c0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 14, "id": "ce487a19", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 5])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp2 = last1[(last1 > 3) & (last1 <= 6)]\n", "temp2" ] }, { "cell_type": "code", "execution_count": 15, "id": "82b9f3ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([7, 9, 9])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp3 = last1[last1 > 6]\n", "temp3" ] }, { "cell_type": "code", "execution_count": 16, "id": "f28879be", "metadata": {}, "outputs": [], "source": [ "last1[last1 <= 3] = 0" ] }, { "cell_type": "code", "execution_count": 17, "id": "4dc020af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 0, 0, 0, 5, 0, 7, 0, 9, 9])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 18, "id": "e475fd7b", "metadata": {}, "outputs": [], "source": [ "last1[(last1 > 3) & (last1 <= 6)] = 1" ] }, { "cell_type": "code", "execution_count": 19, "id": "cfeff15c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 7, 0, 9, 9])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 20, "id": "dda79074", "metadata": {}, "outputs": [], "source": [ "last1[last1 > 6] = 2" ] }, { "cell_type": "code", "execution_count": 21, "id": "9ee04243", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 " ] }, { "cell_type": "code", "execution_count": 22, "id": "255d8986", "metadata": {}, "outputs": [], "source": [ "X_train = X1[:, 0:2]" ] }, { "cell_type": "code", "execution_count": 23, "id": "6587e601", "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": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train" ] }, { "cell_type": "code", "execution_count": 24, "id": "78de1d84", "metadata": {}, "outputs": [], "source": [ "y_train = last1" ] }, { "cell_type": "code", "execution_count": 25, "id": "06103c3e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train" ] }, { "cell_type": "code", "execution_count": 26, "id": "b01a5a6f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [8, 7],\n", " [5, 6],\n", " [8, 8],\n", " [8, 7]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 0]" ] }, { "cell_type": "code", "execution_count": 27, "id": "54c10551", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train == 0" ] }, { "cell_type": "code", "execution_count": 28, "id": "e1eb67be", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9],\n", " [5, 3]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 1]" ] }, { "cell_type": "code", "execution_count": 29, "id": "da29cec8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[8, 1],\n", " [1, 2],\n", " [9, 4]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 2]" ] }, { "cell_type": "code", "execution_count": null, "id": "27d8f581", "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.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }