See More

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Python数据分析\n", "\n", "### 第二章 Numpy基础\n", "- 数据类型\n", "- 数组类型\n", "- 类型转换\n", "- 创建数组\n", "- 数组索引\n", "- 数组切片\n", "- 改变维度" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def pythonsum(n):\n", " a = range(n)\n", " b = range(n)\n", " c = []\n", " \n", " for i in range(len(a)):\n", " a[i] = i ** 2\n", " b[i] = i ** 3\n", " c.append(a[i] + b[i])\n", " \n", " return c" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def numpysum(n):\n", " a = numpy.arange(n) ** 2\n", " b = numpy.arange(n) ** 3\n", " c = a + b\n", " return c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.1 Numpy数组对象\n", "\n", "ndarray多维数组对象:实际的数据 + 描述数据的元数据\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使用arange()函数创建数组,并获取其数据类型:\n", "import numpy\n", "\n", "a = np.arange(5)\n", "a.dtype # dtype('int64')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个向量(一维Numpy数组),并确定其向量的维度:\n", "a\n", "np.array([0, 1, 2, 3, 4])\n", "# 数组的shape属性返回一个元组(tuple)\n", "a.shape # (5,)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.2 实践:创建多维数组" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [0, 1]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个多维数组,并显示其维度:\n", "# np.arange()函数创建的数组作为列表元素,np.array()函数创建2 * 2的数组\n", "m = np.array([np.arange(2), np.arange(2)])\n", "m" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ " m.shape" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [0, 1, 2],\n", " [0, 1, 2]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个3*3的多维数组\n", "b = np.array([np.arange(3), np.arange(3), np.arange(3)])\n", "b" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 一、选取数组元素" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 选取数组中的某个特定元素\n", "a = np.array([[1, 2], [3, 4]])\n", "a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0, 0]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0, 1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1, 0]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##### 二、Numpy数据类型" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.float64(42)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'int8' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m int8(\u001b[38;5;241m42.0\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'int8' is not defined" ] } ], "source": [ "np.int8(42.0)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42.0)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(True)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(False)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(7, dtype = np.uint16)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1+0j)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 将浮点数转换为复数,反之不行,触发TypeError:\n", "complex(1.0)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "float() argument must be a string or a real number, not 'complex'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[48], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mfloat\u001b[39m(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m0\u001b[39mj)\n", "\u001b[0;31mTypeError\u001b[0m: float() argument must be a string or a real number, not 'complex'" ] } ], "source": [ "float(1+0j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 三、数据类型对象\n", "数据类型对象给出单个数组元素在内存中占用的字节数,dtype的itemsize属性:\n", "\n", "numpy.dtype" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 四、字符编码\n", "\n", "![](http://www.shminger.cn:3380/images/2024/04/28/202404282229978.png)\n", "\n", "注:在Numpy中优先使用dtype对象表示数据类型" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 1., 2., 3., 4., 5., 6.], dtype=float32)" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个单精度浮点数数组:\n", "np.arange(7, dtype = 'f')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个复数数组:\n", "np.arange(7, dtype = 'D')" ] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }