{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "46375879", "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.random.seed(1)\n", "X = np.random.randint(1, 10, size=30)\n", "X" ] }, { "cell_type": "code", "execution_count": 3, "id": "cfef5028", "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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.reshape(-1,3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "889f5953", "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": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X1 = X.reshape(-1 ,3)\n", "X1" ] }, { "cell_type": "code", "execution_count": 5, "id": "b1ea483b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 = X1[:, 2]\n", "last1" ] }, { "cell_type": "code", "execution_count": 6, "id": "6f78cb93", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 <= 3" ] }, { "cell_type": "code", "execution_count": 7, "id": "82645e95", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 3, 2, 2])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 返回符合条件的位置,位置以True False来表示\n", "temp1 = last1[last1 <= 3]\n", "temp1" ] }, { "cell_type": "code", "execution_count": 8, "id": "213aa159", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1, 2, 3, 5, 7], dtype=int64),)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 返回符合条件的位置, 索引值\n", "np.where(last1 <= 3)" ] }, { "cell_type": "code", "execution_count": 9, "id": "34f4d363", "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" ] }, { "cell_type": "code", "execution_count": 10, "id": "e4e5b5d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 5])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp2 = last1[(last1 > 3) & (last1 <= 6)]\n", "temp2" ] }, { "cell_type": "code", "execution_count": 11, "id": "907785bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([7, 9, 9])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp3 = last1[last1 > 6]\n", "temp3" ] }, { "cell_type": "code", "execution_count": 14, "id": "e8b3f707", "metadata": {}, "outputs": [], "source": [ "last1[last1 <= 3] = 0" ] }, { "cell_type": "code", "execution_count": 15, "id": "544de98e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 0, 0, 0, 5, 0, 7, 0, 9, 9])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 16, "id": "9a24a871", "metadata": {}, "outputs": [], "source": [ "last1[(last1 > 3) & (last1 <= 6)] = 1" ] }, { "cell_type": "code", "execution_count": 17, "id": "ff0ca5ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 7, 0, 9, 9])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 18, "id": "ea1df168", "metadata": {}, "outputs": [], "source": [ "last1[last1 > 6] = 2" ] }, { "cell_type": "code", "execution_count": 19, "id": "65f621ef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 " ] }, { "cell_type": "code", "execution_count": 20, "id": "dd098a15", "metadata": {}, "outputs": [], "source": [ "X_train = X1[:, 0:2]" ] }, { "cell_type": "code", "execution_count": 21, "id": "b354e52b", "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": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train" ] }, { "cell_type": "code", "execution_count": 22, "id": "cae0fb99", "metadata": {}, "outputs": [], "source": [ "y_train = last1\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "bf9155e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 1, 0, 2, 0, 2, 2])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train" ] }, { "cell_type": "code", "execution_count": 24, "id": "481dc82b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [8, 7],\n", " [5, 6],\n", " [8, 8],\n", " [8, 7]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 0] # 分类为0" ] }, { "cell_type": "code", "execution_count": 25, "id": "71ebf980", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train == 0" ] }, { "cell_type": "code", "execution_count": 26, "id": "088a88cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 9],\n", " [5, 3]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 1] # 分类为1" ] }, { "cell_type": "code", "execution_count": 27, "id": "bc1ce97f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[8, 1],\n", " [1, 2],\n", " [9, 4]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[y_train == 2] # 分类为2" ] }, { "cell_type": "code", "execution_count": null, "id": "ab8875ab", "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 }