This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/AIandML/e1_python_basics/e1.1_numpy.ipynb

249 lines
6.5 KiB
Text
Raw Normal View History

2022-09-21 03:18:09 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 人工智能与机器学习-实验1\n",
"\n",
"## Part.II Numpy练习\n",
"\n",
"|学号 |姓名 |\n",
"|----------|--------|\n",
2023-06-18 07:57:59 +00:00
"|2020114490|江一和|\n",
2022-09-23 12:00:14 +00:00
"|2020113874|何一涛|"
2022-09-21 03:18:09 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 数组\n",
"\n",
"创建一个长度为10全为0的一维数组然后让第5个元素等于1。"
]
},
{
"cell_type": "code",
2022-10-12 01:30:07 +00:00
"execution_count": 1,
2022-09-21 03:18:09 +00:00
"metadata": {},
2022-09-21 03:31:58 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n"
]
}
],
"source": [
"import numpy as np\n",
"a = np.zeros(10)\n",
"a[4] = 1\n",
"print(a)\n"
]
2022-09-21 03:18:09 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 向量内积\n",
"\n",
"随机生成2个长度为$10^6$的向量分别用numpy函数和for循环计算其内积。并比较耗时情况。"
]
},
2022-09-21 03:31:58 +00:00
{
"cell_type": "code",
2022-10-12 01:30:07 +00:00
"execution_count": 2,
2022-09-21 03:31:58 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-12 01:30:07 +00:00
"250200.07698508396\n",
"numpy:0.9696483612060547ms\n",
"250200.07698508215\n",
"for:509.42516326904297ms\n"
2022-09-21 03:31:58 +00:00
]
}
],
"source": [
"import numpy as np\n",
"import time\n",
"a = np.random.rand(1000000)\n",
"b = np.random.rand(1000000)\n",
"tic = time.time()\n",
"c = np.dot(a,b)\n",
"toc = time.time()\n",
"print(c)\n",
"print(\"numpy:\" + str(1000*(toc-tic)) + \"ms\")\n",
"c = 0\n",
"tic = time.time()\n",
"for i in range(1000000):\n",
" c += a[i]*b[i]\n",
"toc = time.time()\n",
"print(c)\n",
"print(\"for:\" + str(1000*(toc-tic)) + \"ms\")"
]
},
2022-09-21 03:18:09 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 矩阵乘法\n",
"\n",
"生成一个$4\\times5$和一个$5\\times4$的矩阵,并计算它们的乘积。"
]
},
{
"cell_type": "code",
2022-10-12 01:30:07 +00:00
"execution_count": 3,
2022-09-21 03:18:09 +00:00
"metadata": {},
2022-09-21 03:31:58 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-12 01:30:07 +00:00
"[[0.23931254 0.18071967 0.37310061 0.12620676 0.13512026]\n",
" [0.30783951 0.91376617 0.67098953 0.32039883 0.01034717]\n",
" [0.96111339 0.50345747 0.44325113 0.69203477 0.68494092]\n",
" [0.72068532 0.70385745 0.863872 0.04211034 0.64406034]]\n",
"[[0.77360967 0.69983706 0.6007846 0.86168637]\n",
" [0.81774926 0.45233242 0.25631712 0.78721015]\n",
" [0.70753124 0.97739082 0.85224573 0.38800176]\n",
" [0.99206956 0.76979289 0.32823954 0.65932597]\n",
" [0.28726144 0.0885248 0.9055978 0.25378116]]\n",
"[[0.76091893 0.72300482 0.67186089 0.61074278]\n",
" [1.78095556 1.5321393 1.10554506 1.45880543]\n",
" [2.35214635 1.9269402 1.93165995 2.02658896]\n",
" [1.97111455 1.75651195 1.94670048 1.70148712]]\n"
2022-09-21 03:31:58 +00:00
]
}
],
"source": [
"import numpy as np\n",
"a = np.random.rand(4,5)\n",
"b = np.random.rand(5,4)\n",
"c = np.dot(a,b)\n",
"print(a)\n",
"print(b)\n",
"print(c)\n"
]
2022-09-21 03:18:09 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. 函数\n",
"\n",
"函数$f$定义如下,其中$x$是任意实数:\n",
"$$\n",
"f(x)=\\frac{\\sin(x+1)(x+1)}{2e^x}\n",
"$$\n",
"\n",
"生成一个数组$a\\in \\mathbb{R}^5$,计算$b = f(a)$。"
]
},
{
"cell_type": "code",
2022-10-12 01:30:07 +00:00
"execution_count": 4,
2022-09-21 03:18:09 +00:00
"metadata": {},
2022-09-21 03:31:58 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-12 01:30:07 +00:00
"[0.11783325 0.48074153 0.73572425 0.20476283 0.88648351]\n",
"[0.44669072 0.45593449 0.41020043 0.45832847 0.36950327]\n"
2022-09-21 03:31:58 +00:00
]
}
],
"source": [
"import numpy as np\n",
"def f(x):\n",
" return np.sin(x+1)*(x+1)/(2*np.exp(x))\n",
"a = np.random.rand(5)\n",
"b = f(a)\n",
"print(a)\n",
"print(b)"
]
2022-09-21 03:18:09 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. 平均值\n",
"\n",
"随机生成一个5行10列的矩阵然后每行元素减去该行的平均值"
]
},
{
"cell_type": "code",
2022-10-12 01:30:07 +00:00
"execution_count": 5,
2022-09-21 03:18:09 +00:00
"metadata": {},
2022-09-21 03:31:58 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-12 01:30:07 +00:00
"[[0.24236313 0.55447699 0.95304607 0.79666384 0.92822022 0.27138421\n",
" 0.47722097 0.74169242 0.21323861 0.83588465]\n",
" [0.81246374 0.77727719 0.45686829 0.08864242 0.00963438 0.50045921\n",
" 0.51131599 0.1215012 0.16156761 0.9299535 ]\n",
" [0.53098614 0.39056825 0.4658711 0.26826016 0.42102393 0.11597775\n",
" 0.8880885 0.96429013 0.79650161 0.42915054]\n",
" [0.71644738 0.84851284 0.03352013 0.89332467 0.3272176 0.67425642\n",
" 0.58702405 0.39927189 0.56726108 0.92623776]\n",
" [0.30084031 0.32070728 0.05854519 0.83081661 0.70783952 0.20550454\n",
" 0.08244311 0.93941383 0.13424483 0.71656674]]\n",
"[[-0.35905598 -0.04694212 0.35162696 0.19524473 0.32680111 -0.3300349\n",
" -0.12419814 0.14027331 -0.3881805 0.23446554]\n",
" [ 0.37549539 0.34030884 0.01989994 -0.34832593 -0.42733397 0.06349086\n",
" 0.07434764 -0.31546716 -0.27540075 0.49298515]\n",
" [ 0.00391433 -0.13650356 -0.06120071 -0.25881165 -0.10604788 -0.41109406\n",
" 0.36101669 0.43721832 0.2694298 -0.09792127]\n",
" [ 0.11913999 0.25120546 -0.56378725 0.29601729 -0.27008979 0.07694904\n",
" -0.01028333 -0.19803549 -0.0300463 0.32893038]\n",
" [-0.12885189 -0.10898492 -0.37114701 0.40112441 0.27814732 -0.22418765\n",
" -0.34724909 0.50972163 -0.29544737 0.28687455]]\n"
2022-09-21 03:31:58 +00:00
]
}
],
"source": [
"import numpy as np\n",
"a = np.random.rand(5,10)\n",
"b = a - np.mean(a,axis=1,keepdims=True)\n",
"print(a)\n",
"print(b)"
]
2022-09-21 03:18:09 +00:00
}
],
"metadata": {
"kernelspec": {
2022-10-12 01:30:07 +00:00
"display_name": "Python 3 (ipykernel)",
2022-09-21 03:18:09 +00:00
"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",
2022-09-21 03:31:58 +00:00
"version": "3.10.7"
},
"vscode": {
"interpreter": {
"hash": "1f0d395e06aa83586067b19165efc9b683889967164248deef4bbf1fa27cfb00"
}
2022-09-21 03:18:09 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}