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
2022-09-23 20:00:14 +08:00

253 lines
6.7 KiB
Text
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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": 3,
"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": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250517.08615769504\n",
"numpy:0.9500980377197266ms\n",
"250517.08615771346\n",
"for:450.66380500793457ms\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": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.832603 0.01479218 0.74726877 0.11899442 0.5139687 ]\n",
" [0.85742314 0.20950708 0.31393951 0.69463518 0.58622781]\n",
" [0.92412297 0.93890447 0.27295923 0.44996205 0.98461149]\n",
" [0.56097043 0.81885742 0.31026755 0.65530345 0.51932541]]\n",
"[[0.54243804 0.59199752 0.2088532 0.09047042]\n",
" [0.96482797 0.66663309 0.7223203 0.96680344]\n",
" [0.1848234 0.88155844 0.98424619 0.99646051]\n",
" [0.76723488 0.42095062 0.55842772 0.58467868]\n",
" [0.56045915 0.6447964 0.05470569 0.317023 ]]\n",
"[[0.98337534 1.54301691 1.01463973 1.0667643 ]\n",
" [1.58676566 1.59441744 1.05937409 1.184938 ]\n",
" [2.35467111 2.23789856 1.44499002 1.83856268]\n",
" [1.94552528 1.76219908 1.40836713 1.69937482]]\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": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.89379618 0.03735883 0.51341215 0.49607132 0.15340217]\n",
"[0.36734363 0.43023904 0.45210542 0.45422143 0.45221582]\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": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.39753532 0.54410703 0.60167078 0.24255209 0.67574439 0.01004289\n",
" 0.55770556 0.10702142 0.48778556 0.51648967]\n",
" [0.98453781 0.66164632 0.66346184 0.5019827 0.74216596 0.37279722\n",
" 0.02415767 0.21952032 0.94425462 0.83570665]\n",
" [0.366748 0.38391583 0.21428393 0.56555728 0.73883924 0.38754266\n",
" 0.7720652 0.68972519 0.69671146 0.43759293]\n",
" [0.93430798 0.57475894 0.23919737 0.31449016 0.56069848 0.23824185\n",
" 0.61332708 0.8877036 0.895658 0.39875045]\n",
" [0.80075957 0.85481337 0.10696488 0.91660988 0.95048433 0.62219214\n",
" 0.61386599 0.1400393 0.68407094 0.08965478]]\n",
"[[-0.01653016 0.13004156 0.18760531 -0.17151338 0.26167892 -0.40402258\n",
" 0.14364009 -0.30704405 0.07372009 0.10242419]\n",
" [ 0.3895147 0.06662321 0.06843873 -0.09304041 0.14714285 -0.22222589\n",
" -0.57086544 -0.37550279 0.34923151 0.24068354]\n",
" [-0.15855017 -0.14138234 -0.31101425 0.0402591 0.21354107 -0.13775551\n",
" 0.24676703 0.16442702 0.17141329 -0.08770524]\n",
" [ 0.36859459 0.00904555 -0.32651602 -0.25122323 -0.00501491 -0.32747154\n",
" 0.04761369 0.32199021 0.32994461 -0.16696294]\n",
" [ 0.22281405 0.27686785 -0.47098064 0.33866436 0.37253881 0.04424662\n",
" 0.03592048 -0.43790622 0.10612542 -0.48829074]]\n",
"[[ 0.00000000e+00]\n",
" [ 6.66133815e-17]\n",
" [-3.33066907e-17]\n",
" [ 1.11022302e-17]\n",
" [-3.33066907e-17]]\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.10.7 ('.venv': venv)",
"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
}