{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# arrays" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]], dtype=int32)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)\n", "x" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.dtype" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[0, 1]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4], dtype=int32)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:, 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# inserting" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "x = np.append(x, [7, 8, 9])\n", "x" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "x = np.append(arr=x, values=[[7, 8, 9]], axis=0)\n", "x" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 4, 6],\n", " [ 8, 10, 12]], dtype=int32)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "x = np.add(x, x)\n", "x" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "x.tolist()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3],\n", " [4],\n", " [5],\n", " [6]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "np.reshape(x, (-1, 1))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n" ] }, { "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(x)\n", "x.transpose()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "x.flatten()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 3],\n", " [5, 7]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [4, 3, 5, 7, 6, 8]\n", "np.take(a, [[0, 1], [2, 3]])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-44, 1, -55, 3, 4])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(5)\n", "np.put(a, [0, 2], [-44, -55])\n", "a" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 1, 10, 3, 4])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(5)\n", "np.put(a, [0, 2], 10)\n", "a" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "[4 5 6]\n", "[3 6]\n" ] } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(x.max())\n", "print(x.max(axis=0))\n", "print(x.max(axis=1))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.125, 0.754, 0.345],\n", " [0.453, 0.896, 0.384]])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[0.12541, 0.753682, 0.3453453], [0.45261364, 0.8957225, 0.3842736]])\n", "np.round(x, 3)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n", "[ 6 15]\n" ] } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(np.sum(x))\n", "print(np.sum(x, axis=1))" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.5\n", "[2.5 3.5 4.5]\n" ] } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(np.mean(x))\n", "print(np.mean(x, axis=0))" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.707825127659933\n", "[1.5 1.5 1.5]\n" ] } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(np.std(x))\n", "print(np.std(x, axis=0))" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2 3 4]\n", " [5 6 7]]\n", "[[ 2 4 6]\n", " [ 8 10 12]]\n", "[[0.5 1. 1.5]\n", " [2. 2.5 3. ]]\n", "floor [[0 1 1]\n", " [2 2 3]]\n", "mod [[1 0 1]\n", " [0 1 0]]\n", "power [[ 1 4 9]\n", " [16 25 36]]\n" ] } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "print(x + 1)\n", "print(x * 2)\n", "print(x / 2)\n", "print('floor', x // 2)\n", "print('mod', x % 2)\n", "print('power', pow(x, 2))" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 4, 16, 36],\n", " [ 64, 100, 144]])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 2, 3], [4, 5, 6]])\n", "x *= 2\n", "x **= 2\n", "x" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }