Python-100-Days/Day66-70/code/Day67.ipynb

863 lines
16 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array1 = np.array([1, 2, 3, 4, 5])\n",
"array1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array2 = np.arange(0, 20, 2)\n",
"array2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array3 = np.linspace(-5, 5, 101)\n",
"array3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array4 = np.random.rand(10)\n",
"array4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array5 = np.random.randint(1, 101, 10)\n",
"array5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array6 = np.random.normal(50, 10, 20)\n",
"array6"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array7 = np.array([[1, 2, 3], [4, 5, 6]])\n",
"array7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array8 = np.zeros((3, 4))\n",
"array8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array9 = np.ones((3, 4))\n",
"array9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array10 = np.full((3, 4), 10)\n",
"array10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array11 = np.eye(4)\n",
"array11"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array12 = np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3)\n",
"array12"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array13 = np.random.rand(3, 4)\n",
"array13"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array14 = np.random.randint(1, 100, (3, 4))\n",
"array14"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array15 = np.random.randint(1, 100, (3, 4, 5))\n",
"array15"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array16 = np.arange(1, 25).reshape((2, 3, 4))\n",
"array16"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array17 = np.random.randint(1, 100, (4, 6)).reshape((4, 3, 2))\n",
"array17"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array18 = plt.imread('guido.jpg')\n",
"array18"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array19 = np.arange(1, 100, 2)\n",
"array20 = np.random.rand(3, 4)\n",
"print(array19.size, array20.size)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array19.shape, array20.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array19.dtype, array20.dtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array19.ndim, array20.ndim)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array21 = np.arange(1, 100, 2, dtype=np.int8)\n",
"print(array19.itemsize, array20.itemsize, array21.itemsize)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array19.nbytes, array20.nbytes, array21.nbytes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import Iterable\n",
"\n",
"print(isinstance(array20.flat, np.ndarray), isinstance(array20.flat, Iterable))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array22 = array19[:]\n",
"print(array22.base is array19, array22.base is array21)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array23 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"print(array23[0], array23[array23.size - 1])\n",
"print(array23[-array23.size], array23[-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array24 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print(array24[2])\n",
"print(array24[0][0], array24[-1][-1])\n",
"print(array24[1][1], array24[1, 1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array24[1][1] = 10\n",
"print(array24)\n",
"array24[1] = [10, 11, 12]\n",
"print(array24)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[:2, 1:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[2])\n",
"print(array24[2, :])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[2:, :])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[:, :2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[1, :2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[1:2, :2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array24[1:2, :2].base"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[::2, ::2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(array24[::-2, ::-2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"scrolled": false
},
"outputs": [],
"source": [
"guido_image = plt.imread('guido.jpg')\n",
"guido_shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image[::-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image[:,::-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split"
},
"outputs": [],
"source": [
"plt.imshow(guido_image[30:350, 90:300])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array25 = np.array([50, 30, 15, 20, 40])\n",
"array25[[0, 1, -1]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array26 = np.array([[30, 20, 10], [40, 60, 50], [10, 90, 80]])\n",
"array26[[0, 2]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array26[[0, 2], [1, 2]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array26[[0, 2], [1]]\n",
"array26[[0, 2], 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array27 = np.arange(1, 10)\n",
"array27[[True, False, True, True, False, False, False, False, True]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array27 >= 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"~(array27 >= 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array27[array27 >= 5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array28 = np.array([1, 2, 3, 4, 5, 5, 4, 3, 2, 1])\n",
"print(array28.sum())\n",
"print(array28.mean())\n",
"print(array28.max())\n",
"print(array28.min())\n",
"print(array28.std())\n",
"print(array28.var())\n",
"print(array28.cumsum())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array29 = np.array([3, 4])\n",
"array30 = np.array([5, 6])\n",
"array29.dot(array30)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array31 = np.array([[1, 2, 3], [4, 5, 6]])\n",
"array32 = np.array([[1, 2], [3, 4], [5, 6]])\n",
"array31.dot(array32)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array31.dump('array31-data')\n",
"array32 = np.load('array31-data', allow_pickle=True)\n",
"array32"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array32.flatten()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array33 = np.array([35, 96, 12, 78, 66, 54, 40, 82])\n",
"array33.sort()\n",
"array33"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array32.swapaxes(0, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array32.transpose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array34 = array33.take([0, 2, -3, -1])\n",
"array34"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array35 = np.arange(1, 10)\n",
"print(array35 + 10)\n",
"print(array35 * 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array36 = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3])\n",
"print(array35 + array36)\n",
"print(array35 * array36)\n",
"print(array35 ** array36)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(np.sqrt(array35))\n",
"print(np.log2(array35))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array37 = np.array([[4, 5, 6], [7, 8, 9]])\n",
"array38 = np.array([[1, 2, 3], [3, 2, 1]])\n",
"print(array37 * array38)\n",
"print(np.power(array37, array38))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array39 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
"array40 = np.array([1, 2, 3])\n",
"array39 + array40"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array41 = np.array([[1], [2], [3], [4]])\n",
"array39 + array41"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array42 = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
"array43 = np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6]])\n",
"np.hstack((array42, array43))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.vstack((array42, array43))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate((array42, array43))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate((array42, array43), axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.array([1, 2, 3])\n",
"y = np.array([4, 5, 6])\n",
"np.cross(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m1 = np.matrix('1 2 3; 4 5 6')\n",
"m1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m2 = np.asmatrix(np.array([[1, 1], [2, 2], [3, 3]]))\n",
"m2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m1.dot(m2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m1 * m2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m3 = np.array([[1., 2.], [3., 4.]])\n",
"np.linalg.inv(m3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m4 = np.array([[1, 3, 5], [2, 4, 6], [4, 7, 9]])\n",
"np.linalg.det(m4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 解线性方程组ax=b\n",
"# 3x + y = 9x + 2y = 8\n",
"a = np.array([[3,1], [1,2]])\n",
"b = np.array([9, 8])\n",
"np.linalg.solve(a, b)"
]
}
],
"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.7"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}