fix(AIandML): fix something

This commit is contained in:
iridiumR 2022-10-15 22:46:19 +08:00
parent a912c668a9
commit f77e92f0e5
4 changed files with 2013 additions and 0 deletions

View File

@ -0,0 +1,467 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 人工智能与机器学习-实验1\n",
"\n",
"## Part.I Python语言练习\n",
"\n",
"|学号 |姓名 |\n",
"|----------|--------|\n",
"|***REMOVED***|***REMOVED***|\n",
"|2020113874|何一涛|"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 名人名言\n",
"\n",
"找一句你钦佩的名人说的名言,将名人的姓名存储在变量`famous_person`中,再将他说的名言存储在变量`message`中,然后如下显示出该名人说了该名言(包括引号):\n",
"\n",
"`xxx once said, \"......\"`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Linus Torvalds once said, \"So nvidia, fuck you.\"\n"
]
}
],
"source": [
"famous_person=\"Linus Torvalds\"\n",
"message=\"So nvidia, fuck you.\"\n",
"print(\"%s once said, \\\"%s\\\"\" %(famous_person, message))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 朋友\n",
"\n",
"将一些朋友的姓名存储在一个列表中,并将其命名为`names`。\n",
"\n",
"1. 依次显示出列表中所有朋友的姓名;\n",
"\n",
"2. 使用`sort()`将列表中的姓名按字典顺序排序后,再显示出排好序的所有朋友的命名;\n",
"\n",
"3. 将`\"Albert Einstein\"`添加到你的朋友姓名列表中;\n",
"\n",
"4. 显示列表的长度;"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Linus Torvalds', 'Richard Stallman', 'Greg Kroah-Hartman']\n"
]
}
],
"source": [
"names=[\"Linus Torvalds\", \"Richard Stallman\", \"Greg Kroah-Hartman\"]\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Greg Kroah-Hartman', 'Linus Torvalds', 'Richard Stallman']\n"
]
}
],
"source": [
"names.sort()\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Greg Kroah-Hartman', 'Linus Torvalds', 'Richard Stallman', 'Albert Einstein']\n"
]
}
],
"source": [
"names.append(\"Albert Einstein\")\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"print(len(names))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 3的倍数\n",
"\n",
"创建一个列表其中包含3~30内能被3 整除的数字;再使用一个`for`循环将这个列表中的数字都打印出来。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 6 9 12 15 18 21 24 27 "
]
}
],
"source": [
"list=[]\n",
"for i in range(3,30):\n",
" if i%3==0:\n",
" list.append(i)\n",
"for i in list:\n",
" print(i,end=\" \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. 登录信息\n",
"\n",
"创建一个至少包含5 个用户名的列表,且其中一个用户名为`'admin'`。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。如果用户名为`'admin'`,就打印一条特殊的问候消息,如`“Hello admin, would you like to see a status report?”`。否则,打印一条普通的问候消息,如`“Hello Eric, thank you for logging in again”`。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello admin, would you like to see a status report?\n",
"Hello user1, thank you for logging in again.\n",
"Hello user2, thank you for logging in again.\n",
"Hello user3, thank you for logging in again.\n",
"Hello user4, thank you for logging in again.\n"
]
}
],
"source": [
"user=[\"admin\", \"user1\", \"user2\", \"user3\", \"user4\"]\n",
"for i in user: \n",
" if i==\"admin\":\n",
" print(\"Hello admin, would you like to see a status report?\")\n",
" else:\n",
" print(\"Hello %s, thank you for logging in again.\" %i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. 升序降序\n",
"\n",
"给你一个整数组成的列表`L`,按照下列条件输出:\n",
"* 若`L`是升序排列的,则输出\"UP\";\n",
"* 若`L`是降序排列的,则输出\"DOWN\";\n",
"* 若`L`无序,则输出\"WRONG\"。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"UP\n",
"DOWN\n",
"WRONG\n"
]
}
],
"source": [
"up=[1,5,7,9,10]\n",
"down=[11,9,7,5,3]\n",
"wrong=[2,4,6,8,5]\n",
"\n",
"def order(list:list)->str:\n",
" if list==sorted(list):\n",
" return \"UP\"\n",
" elif list==sorted(list, reverse=True):\n",
" return \"DOWN\"\n",
" else:\n",
" return \"WRONG\"\n",
"\n",
"print(order(up))\n",
"print(order(down))\n",
"print(order(wrong))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. IP地址\n",
"\n",
"互联网上的每台计算机都有一个IP合法的IP格式为`A.B.C.D`。其中`A`、`B`、`C`、`D`均为[0, 255]中的整数。为了简单起见我们规定这四个整数中不允许有前导零存在如001。\n",
"\n",
"现在给你一个字符串`s``s`不含空白符),请你判断`s`是不是合法IP若是输出`Yes`,否则输出`No`。如s=“202.115.32.24”则输出Yess=“a.11.11.11”, 则输出No。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes; s=\"202.115.32.24\"\n",
"No\n",
"No\n"
]
}
],
"source": [
"def isIP(s:str)->bool:\n",
" if len(s.split(\".\"))==4:\n",
" for i in s.split(\".\"):\n",
" if i.isdigit() and 0<=int(i)<=255:\n",
" if(int(i)!=0 and i[0]==\"0\"):\n",
" return False\n",
" continue\n",
" else:\n",
" return False\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"s=[\"202.115.32.24\",\"002.115.32.24\",\"256.115.32.24\"]\n",
"for i in s:\n",
" if(isIP(i)):\n",
" print(\"Yes; s=\\\"%s\\\"\" %i)\n",
" else:\n",
" print(\"No\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7. 三明治\n",
"\n",
"创建一个名为`sandwich_orders`的列表,在其中包含各种三明治的名字;\n",
"\n",
"再创建一个名为`finished_sandwiches`的空列表。\n",
"\n",
"遍历列表`sandwich_orders`对于其中的每种三明治都打印一条消息如I made your tuna sandwich并将其移到列表`finished_sandwiches`。\n",
"\n",
"所有三明治都制作好后,打印一条消息,将这些三明治列出来。"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I made your chicken sandwich.\n",
"I made your beef sandwich.\n",
"I made your tuna sandwich.\n",
"All sandwiches are made.\n",
"sandwich_orders: []\n",
"finished_sandwiches: ['chicken', 'beef', 'tuna']\n"
]
}
],
"source": [
"sandwich_orders=[\"tuna\", \"beef\", \"chicken\"]\n",
"finished_sandwiches=[]\n",
"while sandwich_orders:\n",
" buf = sandwich_orders.pop()\n",
" print(\"I made your %s sandwich.\" %buf)\n",
" finished_sandwiches.append(buf)\n",
"print(\"All sandwiches are made.\")\n",
"print(\"sandwich_orders: %s\" %sandwich_orders)\n",
"print(\"finished_sandwiches: %s\" %finished_sandwiches)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 8. 城市\n",
"\n",
"创建一个名为`cities`的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含`country`、`population`和`fact`等键。将每座城市的名字以及有关它们的信息都打印出来。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"City: Beijing\n",
"country: China\n",
"population: 21500000\n",
"fact: Capital of China\n",
"City: Tokyo\n",
"country: Japan\n",
"population: 13500000\n",
"fact: Capital of Japan\n",
"City: New York\n",
"country: USA\n",
"population: 53500000\n",
"fact: Biggest city of USA\n"
]
}
],
"source": [
"cities={\"Beijing\":{\"country\":\"China\", \"population\":21500000, \"fact\":\"Capital of China\"}, \\\n",
" \"Tokyo\":{\"country\":\"Japan\", \"population\":13500000, \"fact\":\"Capital of Japan\"}, \\\n",
" \"New York\":{\"country\":\"USA\", \"population\":53500000, \"fact\":\"Biggest city of USA\"} }\n",
"for city, info in cities.items():\n",
" print(\"City: %s\" %city)\n",
" for k, v in info.items():\n",
" print(\"%s: %s\" %(k, v))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 9. 时间\n",
"\n",
"给你一个时间`t``t`是一个字典,共有六个字符串键`(year, month, day, hour, minute, second)`,每个值为数字组成的字符串,如`t = {year:2013, month:9, day:30, hour:16, minute:45, second:2}`。\n",
"\n",
"请将其按照以下格式输出:`XXXX-XX-XX XX:XX:XX`。如上例应该输出:`2013-09-30 16:45:02`。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2013-09-30 16:45:02\n"
]
}
],
"source": [
"t = {\"year\":\"2013\", \"month\":\"9\", \"day\":\"30\", \"hour\":\"16\", \"minute\":\"45\", \"second\":\"2\"}\n",
"print(\"%s-%02d-%02d %02d:%02d:%02d\" %(t[\"year\"], int(t[\"month\"]), int(t[\"day\"]), int(t[\"hour\"]), int(t[\"minute\"]), int(t[\"second\"])))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 10. 最喜欢的书\n",
"\n",
"编写一个名为`favorite_book()`的函数,其中包含一个名为`title`的形参。这个函数打印一条消息如One of my favorite books is Alice in Wonderland。调用这个函数并将一本图书的名称作为实参传递给它。"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"One of my favorite books is Alice in Wonderland.\n"
]
}
],
"source": [
"def favorite_book(title:str)->None:\n",
" print(\"One of my favorite books is %s.\" %title)\n",
"\n",
"favorite_book(\"Alice in Wonderland\")"
]
}
],
"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.10.7"
},
"vscode": {
"interpreter": {
"hash": "cdd56286faa2ee04874a677dc80784b0c94990d70e3798725c60e428b9bb159e"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 人工智能与机器学习-实验1\n",
"\n",
"## Part.II Numpy练习\n",
"\n",
"|学号 |姓名 |\n",
"|----------|--------|\n",
"|***REMOVED***|***REMOVED***|\n",
"|2020113874|何一涛|"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 数组\n",
"\n",
"创建一个长度为10全为0的一维数组然后让第5个元素等于1。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 向量内积\n",
"\n",
"随机生成2个长度为$10^6$的向量分别用numpy函数和for循环计算其内积。并比较耗时情况。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250200.07698508396\n",
"numpy:0.9696483612060547ms\n",
"250200.07698508215\n",
"for:509.42516326904297ms\n"
]
}
],
"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\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 矩阵乘法\n",
"\n",
"生成一个$4\\times5$和一个$5\\times4$的矩阵,并计算它们的乘积。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[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"
]
}
],
"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"
]
},
{
"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",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.11783325 0.48074153 0.73572425 0.20476283 0.88648351]\n",
"[0.44669072 0.45593449 0.41020043 0.45832847 0.36950327]\n"
]
}
],
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. 平均值\n",
"\n",
"随机生成一个5行10列的矩阵然后每行元素减去该行的平均值"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[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"
]
}
],
"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)"
]
}
],
"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.10.7"
},
"vscode": {
"interpreter": {
"hash": "1f0d395e06aa83586067b19165efc9b683889967164248deef4bbf1fa27cfb00"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long