{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "4101b1dc", "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": "f29377ad", "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": 5, "id": "35728cd0", "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": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X1 = X.reshape(-1 ,3)\n", "X1" ] }, { "cell_type": "code", "execution_count": 7, "id": "a56d498f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 = X1[:, 2]\n", "last1" ] }, { "cell_type": "code", "execution_count": 8, "id": "74aea089", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True, True, False, True, False, True, False,\n", " False])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1 <= 3" ] }, { "cell_type": "code", "execution_count": 9, "id": "cbfd9bea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 3, 2, 2])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 返回符合条件的位置,位置以True False来表示\n", "temp1 = last1[last1 <= 3]\n", "temp1" ] }, { "cell_type": "code", "execution_count": 10, "id": "fdbca627", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1, 2, 3, 5, 7], dtype=int64),)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 返回符合条件的位置, 索引值\n", "np.where(last1 <= 3)" ] }, { "cell_type": "code", "execution_count": 11, "id": "51cfeddb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 2, 3, 3, 5, 2, 7, 2, 9, 9])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last1" ] }, { "cell_type": "code", "execution_count": 12, "id": "9cb114bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 5])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp2 = last1[(last1 > 3) & (last1 <= 6)]\n", "temp2" ] }, { "cell_type": "code", "execution_count": 13, "id": "7f905667", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([7, 9, 9])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp3 = last1[last1 > 6]\n", "temp3" ] }, { "cell_type": "code", "execution_count": 14, "id": "021ef249", "metadata": {}, "outputs": [], "source": [ "last1[last1 <= 3] = 0" ] }, { "cell_type": "code", "execution_count": 15, "id": "595eaf8f", "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": "85512737", "metadata": {}, "outputs": [], "source": [ "last1[(last1 > 3) & (last1 <= 6)] = 1" ] }, { "cell_type": "code", "execution_count": 17, "id": "27e9c521", "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": "c96d5f85", "metadata": {}, "outputs": [], "source": [ "last1[last1 > 6] = 2" ] }, { "cell_type": "code", "execution_count": 19, "id": "fb088f0a", "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": "30188eef", "metadata": {}, "outputs": [], "source": [ "X_train = X1[:, 0:2]" ] }, { "cell_type": "code", "execution_count": 21, "id": "9e04eedc", "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": "381b15c1", "metadata": {}, "outputs": [], "source": [ "y_train = last1" ] }, { "cell_type": "code", "execution_count": 23, "id": "71a17757", "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": "7d65d3d3", "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": "1ab58beb", "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": "eb5f3808", "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": "75335111", "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": "e3cc0c8e", "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 }