Browse Source

Fix some errors, add tips for train deep network

pull/1/MERGE
bushuhui 5 years ago
parent
commit
f9c8dadc14
25 changed files with 1696 additions and 1313 deletions
  1. +47
    -23
      0_python/1_Basics.ipynb
  2. +25
    -29
      0_python/2_Print_Statement.ipynb
  3. +115
    -93
      0_python/3_Data_Structure_1.ipynb
  4. +76
    -48
      0_python/4_Data_Structure_2.ipynb
  5. +77
    -22
      0_python/5_Control_Flow.ipynb
  6. +100
    -56
      0_python/6_Function.ipynb
  7. +183
    -91
      0_python/7_Class.ipynb
  8. +21
    -19
      1_numpy_matplotlib_scipy_sympy/matplotlib_simple_tutorial.ipynb
  9. +509
    -480
      1_numpy_matplotlib_scipy_sympy/numpy_tutorial.ipynb
  10. +3
    -0
      1_numpy_matplotlib_scipy_sympy/random-matrix.csv
  11. BIN
      1_numpy_matplotlib_scipy_sympy/random-matrix.npy
  12. +18
    -19
      1_numpy_matplotlib_scipy_sympy/scipy_tutorial.ipynb
  13. +53
    -53
      1_numpy_matplotlib_scipy_sympy/sympy_tutorial.ipynb
  14. +10
    -10
      2_knn/knn_classification.ipynb
  15. +43
    -38
      3_kmeans/k-means.ipynb
  16. +91
    -98
      6_pytorch/1_NN/deep-nn.ipynb
  17. +140
    -102
      6_pytorch/1_NN/nn-sequential-module.ipynb
  18. +20
    -14
      6_pytorch/1_NN/optimizer/adagrad.ipynb
  19. +58
    -64
      6_pytorch/1_NN/param_initialize.ipynb
  20. BIN
      6_pytorch/2_CNN/CNN_Introduction.pptx
  21. +6
    -3
      6_pytorch/2_CNN/lr-decay.ipynb
  22. +11
    -15
      6_pytorch/2_CNN/resnet.ipynb
  23. +41
    -35
      6_pytorch/2_CNN/vgg.ipynb
  24. +48
    -0
      demo_code/3_CNN_VGG16.py
  25. +1
    -1
      tips/构建深度神经网络的一些实战建议.md

+ 47
- 23
0_python/1_Basics.ipynb View File

@@ -11,6 +11,30 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"a = 10\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## import\n",
"\n",
"```\n",
"import os\n",
"```\n",
"\n",
"$$\n",
"f(x) = sin(x)\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
@@ -60,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -71,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -95,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -104,7 +128,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -210,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -219,7 +243,7 @@
"0.5"
]
},
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -345,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -354,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -363,7 +387,7 @@
"True"
]
},
"execution_count": 18,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -374,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -383,7 +407,7 @@
"False"
]
},
"execution_count": 19,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -601,7 +625,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -655,7 +679,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -664,7 +688,7 @@
"'b'"
]
},
"execution_count": 32,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -675,7 +699,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -684,7 +708,7 @@
"98"
]
},
"execution_count": 33,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -709,7 +733,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 12,
"metadata": {
"scrolled": false
},
@@ -789,7 +813,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -873,7 +897,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -908,14 +932,14 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type something here and it will be stored in variable abc \taa\n"
"Type something here and it will be stored in variable abc \tHello world!\n"
]
}
],
@@ -925,7 +949,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -934,7 +958,7 @@
"str"
]
},
"execution_count": 3,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}


+ 25
- 29
0_python/2_Print_Statement.ipynb View File

@@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {
"scrolled": true
},
@@ -123,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -147,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -180,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -243,24 +243,24 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I want %d to be printed here\n"
"I want to be printed here\n"
]
}
],
"source": [
"print(\"I want %%d to be printed %s\" %'here')"
"print(\"I want to be printed %s\" %'here')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -277,7 +277,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -301,7 +301,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -325,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -342,19 +342,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Routine:\n",
"\t- Eat\n",
"\t- Sleep\n",
"\t- Repeat\n",
"\n"
"ename": "SyntaxError",
"evalue": "Missing parentheses in call to 'print' (<ipython-input-18-92e95899b5a9>, line 5)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-18-92e95899b5a9>\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m \"\"\"\u001b[0m\n\u001b[0m \n^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'\n"
]
}
],
@@ -384,7 +380,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -393,7 +389,7 @@
"'3.121312'"
]
},
"execution_count": 13,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -411,7 +407,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -420,7 +416,7 @@
"'3.12131'"
]
},
"execution_count": 14,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -438,7 +434,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -447,7 +443,7 @@
"' 3.12131'"
]
},
"execution_count": 15,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}


+ 115
- 93
0_python/3_Data_Structure_1.ipynb View File

@@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -151,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -167,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -185,7 +185,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -194,7 +194,7 @@
"'orange'"
]
},
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -215,7 +215,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -240,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -249,7 +249,7 @@
"'apple'"
]
},
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -310,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -332,7 +332,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -358,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -367,7 +367,7 @@
"[0, 3, 6]"
]
},
"execution_count": 6,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -392,7 +392,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -401,7 +401,7 @@
"10"
]
},
"execution_count": 7,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -419,7 +419,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -428,7 +428,7 @@
"0"
]
},
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -439,7 +439,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -448,7 +448,7 @@
"9"
]
},
"execution_count": 20,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -466,7 +466,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -475,7 +475,7 @@
"[1, 2, 3, 5, 4, 7]"
]
},
"execution_count": 21,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -493,7 +493,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@@ -509,7 +509,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -518,7 +518,7 @@
"False"
]
},
"execution_count": 23,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@@ -529,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -538,7 +538,7 @@
"True"
]
},
"execution_count": 24,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -549,7 +549,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -558,7 +558,7 @@
"False"
]
},
"execution_count": 26,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -596,7 +596,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
@@ -605,7 +605,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -631,7 +631,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
@@ -640,7 +640,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -673,7 +673,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -703,7 +703,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -712,7 +712,7 @@
"['h', 'e', 'l', 'l', 'o']"
]
},
"execution_count": 23,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -730,7 +730,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
@@ -739,7 +739,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -764,7 +764,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -773,7 +773,7 @@
"3"
]
},
"execution_count": 10,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
@@ -791,7 +791,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
@@ -800,7 +800,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@@ -825,7 +825,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -850,7 +850,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@@ -859,7 +859,7 @@
"0"
]
},
"execution_count": 14,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@@ -870,7 +870,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 34,
"metadata": {},
"outputs": [
{
@@ -880,7 +880,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-15-344da5bcf759>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlst\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m999\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-34-344da5bcf759>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlst\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m999\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: 999 is not in list"
]
}
@@ -898,7 +898,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -948,16 +948,16 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2]"
"[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4]"
]
},
"execution_count": 19,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
@@ -976,16 +976,16 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
"4"
]
},
"execution_count": 22,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
@@ -996,14 +996,14 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2]\n"
"[1, 1, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4]\n"
]
}
],
@@ -1020,19 +1020,19 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n"
"[1, 1, 8, 7, 1, [5, 4, 2, 8], 5, 4]\n"
]
}
],
"source": [
"lst.remove('Python')\n",
"lst.remove('name')\n",
"print(lst)"
]
},
@@ -1072,14 +1072,14 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 5, [5, 4, 2, 8], 1, 7, 1]\n"
"[4, 5, [5, 4, 2, 8], 1, 7, 8, 1, 1]\n"
]
}
],
@@ -1099,7 +1099,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -1125,7 +1125,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -1150,7 +1150,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@@ -1179,7 +1179,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@@ -1214,7 +1214,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
@@ -1223,7 +1223,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -1248,7 +1248,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 49,
"metadata": {},
"outputs": [
{
@@ -1269,7 +1269,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 50,
"metadata": {},
"outputs": [
{
@@ -1374,7 +1374,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 51,
"metadata": {},
"outputs": [
{
@@ -1408,7 +1408,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
@@ -1425,7 +1425,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 53,
"metadata": {},
"outputs": [
{
@@ -1434,7 +1434,7 @@
"(27,)"
]
},
"execution_count": 41,
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
@@ -1452,7 +1452,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 54,
"metadata": {},
"outputs": [
{
@@ -1461,7 +1461,7 @@
"(27, 27)"
]
},
"execution_count": 42,
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
@@ -1479,7 +1479,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 55,
"metadata": {
"scrolled": true
},
@@ -1509,7 +1509,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 56,
"metadata": {},
"outputs": [
{
@@ -1536,7 +1536,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
@@ -1562,7 +1562,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 58,
"metadata": {},
"outputs": [
{
@@ -1594,9 +1594,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.count('a')"
]
@@ -1610,9 +1621,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 60,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.index('a')"
]
@@ -1635,7 +1657,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 61,
"metadata": {},
"outputs": [
{
@@ -1653,7 +1675,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 62,
"metadata": {},
"outputs": [
{
@@ -1685,7 +1707,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
@@ -1694,7 +1716,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
@@ -1710,7 +1732,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 65,
"metadata": {},
"outputs": [
{
@@ -1719,7 +1741,7 @@
"{1, 2, 3, 4, 5}"
]
},
"execution_count": 52,
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
@@ -1737,7 +1759,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 66,
"metadata": {},
"outputs": [
{
@@ -1753,7 +1775,7 @@
"{0, 1, 2, 3}"
]
},
"execution_count": 56,
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
@@ -1773,7 +1795,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 67,
"metadata": {},
"outputs": [
{
@@ -1782,7 +1804,7 @@
"{2, 3}"
]
},
"execution_count": 57,
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
@@ -1800,7 +1822,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 68,
"metadata": {},
"outputs": [
{
@@ -1809,7 +1831,7 @@
"{0, 1}"
]
},
"execution_count": 58,
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}


+ 76
- 48
0_python/4_Data_Structure_2.ipynb View File

@@ -16,9 +16,17 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"String0 = 'Taj Mahal is beautiful'\n",
"String1 = \"Taj Mahal is beautiful\"\n",
@@ -29,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -59,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -92,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -118,7 +126,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -142,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -168,7 +176,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -193,7 +201,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -202,7 +210,7 @@
"' Taj Mahal is beautiful '"
]
},
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -220,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -229,7 +237,7 @@
"'------------------------Taj Mahal is beautiful------------------------'"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -247,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -256,7 +264,7 @@
"'00000000Taj Mahal is beautiful'"
]
},
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -274,7 +282,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -305,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -323,7 +331,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-a7d6b97b4839>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-14-a7d6b97b4839>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: substring not found"
]
}
@@ -343,7 +351,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -658,7 +666,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -674,7 +682,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -683,7 +691,7 @@
"'hello'"
]
},
"execution_count": 24,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -701,7 +709,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
@@ -710,7 +718,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -719,7 +727,7 @@
"' ***----hello---******* '"
]
},
"execution_count": 26,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -737,7 +745,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -803,7 +811,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -829,7 +837,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -848,14 +856,14 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'key1': 1, 3: (1, 4, 6), 'key2': [1, 2, 4]}\n"
"{'key2': [1, 2, 4], 'key1': 1, 3: (1, 4, 6)}\n"
]
}
],
@@ -873,7 +881,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -897,7 +905,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -914,14 +922,14 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'One': 1, 'Five': 5, 'Four': 4, 'Three': 3, 'Two': 2}\n"
"{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
]
}
],
@@ -941,18 +949,20 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n"
"{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
]
}
],
"source": [
"d2 = zip(names,numbers)\n",
"\n",
"a1 = dict(d2)\n",
"print(a1)"
]
@@ -973,7 +983,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -998,14 +1008,32 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
]
}
],
"source": [
"a1 = {names[i]:numbers[i] for i in range(len(names))}\n",
"print(a1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'One': 1, 'Five': 5, 'Four': 4, 'Three': 3, 'Two': 2}\n"
"{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
]
}
],
@@ -1024,16 +1052,16 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([1, 5, 4, 3, 2])"
"dict_values([1, 4, 3, 5, 2])"
]
},
"execution_count": 38,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -1051,16 +1079,16 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['One', 'Five', 'Four', 'Three', 'Two'])"
"dict_keys(['One', 'Four', 'Three', 'Five', 'Two'])"
]
},
"execution_count": 39,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -1078,7 +1106,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -1086,9 +1114,9 @@
"output_type": "stream",
"text": [
"[ One] 1\n",
"[ Five] 5\n",
"[ Four] 4\n",
"[ Three] 3\n",
"[ Five] 5\n",
"[ Two] 2\n"
]
}
@@ -1109,14 +1137,14 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'One': 1, 'Five': 5, 'Three': 3, 'Two': 2}\n",
"{'One': 1, 'Three': 3, 'Five': 5, 'Two': 2}\n",
"4\n"
]
}


+ 77
- 22
0_python/5_Control_Flow.ipynb View File

@@ -25,21 +25,23 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n"
"Welcome!\n"
]
}
],
"source": [
"x = 12\n",
"x = 4\n",
"if x >10:\n",
" print(\"Hello\")"
" print(\"Hello\")\n",
"else:\n",
" print(\"Welcome!\")"
]
},
{
@@ -271,7 +273,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -281,12 +283,15 @@
"1\n",
"2\n",
"3\n",
"\n",
"4\n",
"5\n",
"6\n",
"\n",
"7\n",
"8\n",
"9\n"
"9\n",
"\n"
]
}
],
@@ -294,7 +299,8 @@
"list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"for list1 in list_of_lists:\n",
" for x in list1:\n",
" print(x)"
" print(x)\n",
" print()"
]
},
{
@@ -315,7 +321,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -410,7 +416,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -455,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -483,7 +489,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -492,7 +498,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 15,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -517,7 +523,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -526,7 +532,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 16,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -537,7 +543,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 13,
"metadata": {
"scrolled": true
},
@@ -557,7 +563,7 @@
" '81': 81}"
]
},
"execution_count": 17,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -568,7 +574,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -577,7 +583,7 @@
"(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)"
]
},
"execution_count": 18,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -595,22 +601,71 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
"[1,\n",
" 2,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 28,\n",
" 29,\n",
" 30,\n",
" 31,\n",
" 32,\n",
" 33,\n",
" 34,\n",
" 35,\n",
" 36,\n",
" 37,\n",
" 55,\n",
" 56,\n",
" 57,\n",
" 58,\n",
" 59,\n",
" 60,\n",
" 61,\n",
" 62,\n",
" 63,\n",
" 64,\n",
" 82,\n",
" 83,\n",
" 84,\n",
" 85,\n",
" 86,\n",
" 87,\n",
" 88,\n",
" 89,\n",
" 90,\n",
" 91,\n",
" 109,\n",
" 110,\n",
" 111,\n",
" 112,\n",
" 113,\n",
" 114,\n",
" 115,\n",
" 116,\n",
" 117,\n",
" 118]"
]
},
"execution_count": 19,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[27*z for i in range(50) if i==27 for z in range(1,11)]"
"[27*i+z for i in range(50) if i<5 for z in range(1,11)]"
]
}
],


+ 100
- 56
0_python/6_Function.ipynb View File

@@ -74,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -114,7 +114,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -125,14 +125,14 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Bu\n"
"Please enter your name : Willam\n"
]
}
],
@@ -149,15 +149,15 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hey Bu!\n",
"Bu, How do you do?\n"
"Hey Willam!\n",
"Willam, How do you do?\n"
]
}
],
@@ -221,7 +221,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -239,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -271,7 +271,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -282,7 +282,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -307,7 +307,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -328,7 +328,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -344,7 +344,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@@ -362,7 +362,7 @@
" lowest = min(eglist)\n",
" first = eglist[0]\n",
" last = eglist[-1]\n",
" return (highest,lowest,first,last)"
" return highest,lowest,first,last"
]
},
{
@@ -392,7 +392,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -427,7 +427,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
@@ -451,7 +451,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -460,7 +460,7 @@
"7"
]
},
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -478,7 +478,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -487,7 +487,7 @@
"8"
]
},
"execution_count": 19,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -532,7 +532,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
@@ -554,7 +554,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -570,7 +570,7 @@
"15"
]
},
"execution_count": 25,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@@ -607,6 +607,41 @@
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[30, 10, 20]\n"
]
},
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def add_nd(**kwargs):\n",
" res = 0\n",
" reslist = []\n",
" for (k,v) in kwargs.items():\n",
" reslist.append(v)\n",
" print(reslist)\n",
" return sum(reslist)\n",
"\n",
"add_nd(x=10, y=20, c=30)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -622,7 +657,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
@@ -638,7 +673,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
@@ -650,8 +685,8 @@
" print(\"This is happening inside the function :\", eg2)\n",
" \n",
" # what's the difference between following two lines?\n",
" eg1.append(7)\n",
" #eg1 = [1, 3, 5, 6]\n",
" #eg1.append(7)\n",
" eg1 = [1, 2, 3, 4, 5, 7]\n",
" \n",
" print(\"This is happening before the function is called : \", eg1)\n",
" thirdfunc(eg1)\n",
@@ -662,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -672,7 +707,7 @@
"This is happening before the function is called : [1, 2, 3, 4, 5, 7]\n",
"This is happening inside the function : [1, 2, 3, 4, 5, 7, 6]\n",
"This is happening outside the function : [1, 2, 3, 4, 5, 7]\n",
"[1, 2, 3, 4, 5, 7]\n"
"[1, 2, 3, 4, 5]\n"
]
}
],
@@ -707,11 +742,18 @@
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<ipython-input-44-9b54e42c4e6f>:8: SyntaxWarning: name 'eg1' is used prior to global declaration\n",
" global eg1\n"
]
}
],
"source": [
"def egfunc1():\n",
" def thirdfunc(arg1):\n",
@@ -720,6 +762,8 @@
" eg2.append(6)\n",
" print(\"This is happening inside the function :\", eg2)\n",
" print(\"This is happening before the function is called : \", eg1)\n",
" global eg1\n",
" eg1 = [1, 2, 3, 4, 5, 7]\n",
" thirdfunc(eg1)\n",
" print(\"This is happening outside the function :\", eg1) \n",
" print(\"Accessing a variable declared inside the function from outside :\" , eg2)"
@@ -761,7 +805,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
@@ -770,7 +814,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 49,
"metadata": {},
"outputs": [
{
@@ -779,7 +823,7 @@
"64"
]
},
"execution_count": 36,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
@@ -790,7 +834,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 50,
"metadata": {},
"outputs": [
{
@@ -799,7 +843,7 @@
"(6, 8)"
]
},
"execution_count": 40,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
@@ -811,7 +855,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 51,
"metadata": {},
"outputs": [
{
@@ -820,7 +864,7 @@
"function"
]
},
"execution_count": 42,
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
@@ -831,7 +875,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 52,
"metadata": {},
"outputs": [
{
@@ -840,7 +884,7 @@
"function"
]
},
"execution_count": 43,
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
@@ -868,7 +912,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
@@ -877,7 +921,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 54,
"metadata": {},
"outputs": [
{
@@ -895,7 +939,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 55,
"metadata": {},
"outputs": [
{
@@ -986,7 +1030,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
@@ -1002,7 +1046,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 57,
"metadata": {},
"outputs": [
{
@@ -1027,16 +1071,16 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[True, True, True, True, False, False, False, False, False]"
"<map at 0x7f72a418c780>"
]
},
"execution_count": 39,
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
@@ -1054,16 +1098,16 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 8]"
"<filter at 0x7f72a4195240>"
]
},
"execution_count": 40,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}


+ 183
- 91
0_python/7_Class.ipynb View File

@@ -32,12 +32,12 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"class FirstClass:\n",
" pass"
" pass\n"
]
},
{
@@ -56,7 +56,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -74,7 +74,7 @@
"__main__.FirstClass"
]
},
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -94,7 +94,7 @@
"type"
]
},
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -128,7 +128,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -138,7 +138,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-d15e7b8e3d78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0meg0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFirstClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0meg0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-8-d15e7b8e3d78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0meg0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFirstClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0meg0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'init'"
]
}
@@ -166,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -186,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@@ -196,21 +196,23 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"one 1\n",
"two 2\n"
"onex 11\n",
"two 2\n",
"My first class\n"
]
}
],
"source": [
"print(eg1.name, eg1.symbol)\n",
"print(eg2.name, eg2.symbol)"
"print(eg2.name, eg2.symbol)\n",
"print(eg1.__doc__)"
]
},
{
@@ -222,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 16,
"metadata": {
"scrolled": false
},
@@ -257,7 +259,7 @@
" '__weakref__']"
]
},
"execution_count": 22,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -348,7 +350,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
@@ -367,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
@@ -377,7 +379,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -387,7 +389,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-28-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-23-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
]
}
@@ -406,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -441,7 +443,7 @@
" 's']"
]
},
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -534,10 +536,8 @@
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"eg1.cube = 1\n",
@@ -546,16 +546,43 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'cube', 'n', 's']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'cube',\n",
" 'n',\n",
" 's']"
]
},
"execution_count": 20,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -579,7 +606,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
@@ -599,7 +626,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -619,7 +646,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -643,10 +670,8 @@
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"class FirstClass:\n",
@@ -663,10 +688,8 @@
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"eg4 = FirstClass('Five',5)"
@@ -674,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 37,
"metadata": {},
"outputs": [
{
@@ -687,8 +710,8 @@
}
],
"source": [
"print eg4.square()\n",
"print eg4.cube()"
"print(eg4.square())\n",
"print(eg4.cube())"
]
},
{
@@ -761,10 +784,8 @@
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true
},
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"class SoftwareEngineer:\n",
@@ -778,7 +799,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
@@ -787,7 +808,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -804,16 +825,41 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'salary']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'salary']"
]
},
"execution_count": 32,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@@ -831,7 +877,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
@@ -839,7 +885,7 @@
" def __init__(self,name,age):\n",
" self.name = name\n",
" self.age = age\n",
" def money(self,value):\n",
" def salary(self,value):\n",
" self.money = value\n",
" print(self.name,\"earns\",self.money)\n",
" def artform(self, job):\n",
@@ -849,10 +895,8 @@
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": true
},
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"b = Artist('Nitin',20)"
@@ -860,7 +904,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -873,22 +917,48 @@
}
],
"source": [
"b.money(50000)\n",
"b.salary(50000)\n",
"b.artform('Musician')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'artform', 'money']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'artform',\n",
" 'money']"
]
},
"execution_count": 36,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
@@ -906,7 +976,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
@@ -918,7 +988,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
@@ -927,16 +997,42 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'artform', 'salary']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'artform',\n",
" 'salary']"
]
},
"execution_count": 39,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
@@ -947,7 +1043,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 49,
"metadata": {},
"outputs": [
{
@@ -973,10 +1069,8 @@
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": true
},
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"class Artist(SoftwareEngineer):\n",
@@ -991,10 +1085,8 @@
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": true
},
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"c = Artist('Nishanth',21)"
@@ -1002,7 +1094,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 52,
"metadata": {},
"outputs": [
{
@@ -1029,7 +1121,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
@@ -1046,7 +1138,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
@@ -1055,7 +1147,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 59,
"metadata": {},
"outputs": [
{
@@ -1068,7 +1160,7 @@
],
"source": [
"xc.one(1)\n",
"print xc.data"
"print(xc.data)"
]
},
{
@@ -1080,7 +1172,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 60,
"metadata": {},
"outputs": [
{
@@ -1093,12 +1185,12 @@
],
"source": [
"xc.data.append(8)\n",
"print xc.data"
"print(xc.data)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 61,
"metadata": {},
"outputs": [
{
@@ -1111,7 +1203,7 @@
],
"source": [
"xc.two(3)\n",
"print xc.data"
"print(xc.data)"
]
},
{
@@ -1123,7 +1215,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
@@ -1134,7 +1226,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
@@ -1143,7 +1235,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 64,
"metadata": {},
"outputs": [
{
@@ -1152,7 +1244,7 @@
"'IDoNotKnowWhatToType'"
]
},
"execution_count": 51,
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}


+ 21
- 19
1_numpy_matplotlib_scipy_sympy/matplotlib_simple_tutorial.ipynb View File

@@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
@@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"lines_to_next_cell": 2
},
@@ -56,10 +56,10 @@
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f06c3cfb048>]"
"[<matplotlib.lines.Line2D at 0x7f24fa55e7b8>]"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
@@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -114,7 +114,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -138,7 +138,9 @@
"t = np.arange(0., 5., 0.2)\n",
"\n",
"# red dashes, blue squares and green triangles\n",
"plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')\n",
"plt.plot(t, t, 'r--', \\\n",
" t, t**2, 'bs', \\\n",
" t, t**3, 'g^')\n",
"plt.show()"
]
},
@@ -163,7 +165,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -190,10 +192,10 @@
"t2 = np.arange(0.0, 5.0, 0.02)\n",
"\n",
"plt.figure(1)\n",
"plt.subplot(211)\n",
"plt.subplot(2,1,1)\n",
"plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')\n",
"\n",
"plt.subplot(212)\n",
"plt.subplot(2,1,2)\n",
"plt.plot(t2, np.cos(2*np.pi*t2), 'r--')\n",
"plt.show()"
]
@@ -207,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -243,16 +245,16 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7f06c3db07b8>"
"<matplotlib.image.AxesImage at 0x7f24f83a01d0>"
]
},
"execution_count": 14,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
@@ -276,16 +278,16 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.colorbar.Colorbar at 0x7f06c3c8bda0>"
"<matplotlib.colorbar.Colorbar at 0x7f24f83787b8>"
]
},
"execution_count": 15,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
},
@@ -317,7 +319,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -407,7 +409,7 @@
" <a list of 256 Patch objects>)"
]
},
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},


+ 509
- 480
1_numpy_matplotlib_scipy_sympy/numpy_tutorial.ipynb
File diff suppressed because it is too large
View File


+ 3
- 0
1_numpy_matplotlib_scipy_sympy/random-matrix.csv View File

@@ -0,0 +1,3 @@
0.85031 0.33331 0.64003
0.52522 0.21573 0.33288
0.74605 0.35135 0.45873

BIN
1_numpy_matplotlib_scipy_sympy/random-matrix.npy View File


+ 18
- 19
1_numpy_matplotlib_scipy_sympy/scipy_tutorial.ipynb View File

@@ -910,37 +910,36 @@
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": true
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from scipy.linalg import *"
"from scipy.linalg import *\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"A = array([[1,2,3], [4,5,6], [7,8,9]])\n",
"b = array([1,2,3])"
"A = np.array([[1,2], [4,5]])\n",
"b = np.array([1,2])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.33333333, 0.66666667, 0. ])"
"array([-0.33333333, 0.66666667])"
]
},
"execution_count": 35,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -953,23 +952,23 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ -1.11022302e-16, 0.00000000e+00, 0.00000000e+00])"
"array([0., 0.])"
]
},
"execution_count": 36,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check\n",
"dot(A, x) - b"
"np.dot(A, x) - b"
]
},
{
@@ -1067,7 +1066,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -1076,16 +1075,16 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.08466629+0.j, 0.33612878+0.j, -0.28229973+0.j])"
"array([-0.46410162+0.j, 6.46410162+0.j])"
]
},
"execution_count": 42,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}


+ 53
- 53
1_numpy_matplotlib_scipy_sympy/sympy_tutorial.ipynb View File

@@ -168,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -177,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -186,7 +186,7 @@
"False"
]
},
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -331,7 +331,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -341,7 +341,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -354,7 +354,7 @@
"4/5"
]
},
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -365,7 +365,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -380,7 +380,7 @@
"20"
]
},
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -433,7 +433,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -446,7 +446,7 @@
"3.1415926535897932384626433832795028841971693993751"
]
},
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -457,7 +457,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -466,7 +466,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -480,7 +480,7 @@
"(x + 3.1416) "
]
},
"execution_count": 16,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -736,7 +736,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -749,7 +749,7 @@
"(x + 1)⋅(x + 2)⋅(x + 3)"
]
},
"execution_count": 23,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -760,7 +760,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -774,7 +774,7 @@
"x + 6⋅x + 11⋅x + 6"
]
},
"execution_count": 24,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -792,7 +792,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -805,7 +805,7 @@
"sin(a + b)"
]
},
"execution_count": 25,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -816,7 +816,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -829,7 +829,7 @@
"sin(a)⋅cos(b) + sin(b)⋅cos(a)"
]
},
"execution_count": 26,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@@ -854,7 +854,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -867,7 +867,7 @@
"(x + 1)⋅(x + 2)⋅(x + 3)"
]
},
"execution_count": 27,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -894,7 +894,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -907,7 +907,7 @@
"(x + 1)⋅(x + 2)⋅(x + 3)"
]
},
"execution_count": 28,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -919,7 +919,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -932,7 +932,7 @@
"1"
]
},
"execution_count": 29,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -944,7 +944,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -959,7 +959,7 @@
"tan(x)"
]
},
"execution_count": 27,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -1167,7 +1167,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -1181,7 +1181,7 @@
"(x + π) "
]
},
"execution_count": 28,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -1192,7 +1192,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -1206,7 +1206,7 @@
"4⋅(x + π) "
]
},
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -1224,7 +1224,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -1238,7 +1238,7 @@
"12⋅(x + π) "
]
},
"execution_count": 30,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -1249,12 +1249,12 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAbBAMAAACekfw3AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiuzKJ\nRN0MreaOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAByElEQVQ4EZWTPUjDYBCG3/6bNqmKKOgi6OCm\nQToKFgV/Fi2oi5NQFFy0W8HBdhF0kY5OtaM41cnRgigUBTs5d1I6iBVRB3/i3Zd8bVO1knfId/fe\nPbkcfAGcSF1ecNJOvUVMOyQqOMs4Qx5wWHJGAP26U+LOKaDGmomDZqOe73G4I3Nl6TjPsZKTzs/T\nmwG0gkZ9rkEgAvWTewLk/iVPAjjt7NMRWXwGJoEB7qwNFZhrRRzycQmMGAZlfiKGgXkah4qsirOJ\nGJVFJlK6IJSyNH8jAiWrygTpUadxMcCzNntL81lyxqZhGC+Aj6pCJqF9UBJMA2PYX7k3KxahXvUc\nrGeAtoRpiz2AcJTScAdwjhu910acoOjPk+Mqm7ZFLHHmLgE6tkRBTSa3h5LJKCc5r07PUJVjkvgq\n3sAkgFcOWXKPUDTMqZ3ohpIxvwqhdy6zJOFLt/MMrcwmiWeEYvATwZt3+auIi0KNGNfbS+TYNs9e\nX8yRRzdTewpWFQJZcsYG3AVKffwg+SbeZpAyjC+Kaa5nKr66Kwp14gjeAlmBvOU3Hi1vSbaxU8b2\nm+ih1Rpk3YQGh8KgvcdW5Nv+U63+qD/e9t9fS0O+AbbpaS6aluDxAAAAAElFTkSuQmCC\n",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAbBAMAAACekfw3AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAzRAiu5mrdu/dZjKJRFRer8KoAAAB1UlEQVQ4EZWTP0gjQRTGv2xi1phdTcTGyilELIRLccVpYworqyBqHbXwtDFoIYJwiI1XHCoWlgbEQm20tdCgiIWC6WzFP9h5QRAR/6zvzcwqExJhXzHz3ve9376ZZRcIEk5/X5B26j3CUEBiBQepYMg/dBeDEcB/EZRYDQo4mXJir1z4qrc53ZB1IxAe6NnhPJyWSsUlSh1uwaX15wPQBueN20KClmqRAHbvbwRaB4kYBha4cdbotlJGSeai55FkE/EXmKBxmDda6nNG2e5XTLQISbhLvij3MiJ0qV0mKNYEwO/Omh65OJEKfOKP53mPypWGItxXKiJ5oAObuWWDcE7vuqZSQF1CyfIeQCxNZewSOMS5uFaWnrGPI3uHlPolJWuin6vaIiDQpI3PUyEdFaTFS9qQp7KzXDEBPPECJ5lsnksmM5TGszFWTOIW4ZQ6FeIvsLiBjpFTe02+QVD2+SZ5RjwDmwi++ZVdwqTq9IkfoqFIinHzzrPjMdKcLNz3SClMIIdPzKC2QGVNhkXafz+Po8Xznimnudbo5OyWNL6IXkQLJIV4UHmsGII/Q4u/DFMX6vP3HTfvZ3LXX4KhIULXrxZWopLz3R9V5WldlZ6jtHVtfQDvsmc+fdUw/AAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$12 \\left(x + \\pi\\right)^{2}$$"
],
@@ -1263,7 +1263,7 @@
"12⋅(x + π) "
]
},
"execution_count": 51,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -1281,7 +1281,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
@@ -1290,7 +1290,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
@@ -1306,7 +1306,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -1319,7 +1319,7 @@
"-x⋅(x⋅y⋅cos(x⋅y) + 2⋅sin(x⋅y))"
]
},
"execution_count": 37,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
@@ -1344,12 +1344,12 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAKAAAAAUBAMAAAD4uit9AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMnZUZs0Qu91E7yKJ\nmaurDqYVAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACz0lEQVQ4EXVVX0hTURz+rtvu3bybWRD1pBcD\nH6rV6KHCoIYUBCqsh1ERxCWIIKRECqFBDCoifBk+SQSaYCARDJIMIRiDgihpBeKL0YoKwgKtSFaa\nfffsnvtnzQN35/t9v+/3nfu75547oHZMeokub1AX60kPHcxXA2XMJfU+FwNNtsLL+fGUL1yoRsqK\ny4Z8FmrKzdRHp3z0WV8kgh4/VbTDgOHnZRQsSSTmqOELrWDQz5y3w/UMw76GoI4Kvb7RcdGzDhQg\nlKvG6xk+9stxEsq2ztTUX2x+sGmXwWQDW1DbFoaKOPMG+j3E7JakoTo/k0Bv23NRR32c8riBaRPD\nE0oSeItzQBangZuG/ocCjWQvjho3UOhAwyKCKZIc0nA6odxXBqEZog64BEQDo9gNtT8+R90R3DZh\n4CpwBfhJIpwAnuGVeUBPzELrQyBLkkMaphFYChegLIk64BrwMJbCAFSIF24/wss7YBleBr6zsDEH\nmDjORjCGDQaURZLRdPrEeDpdJrvKsJWLrog6YILyRkOUbjGtHNSnayXLcMA1rKLIL7Q2Vw0ptO8w\nYi16iIsuizphiBFTWQIiZaZo+AjBisfQapkN8YeNbKcsS8hhGzp3uCrqRMuYRcMocFDohrEH+Ogx\ntDZlLzejHVrZoms3JU3vxhIiFVEnNoUyagMlNMHalA4TGatfu+UoxT+0RR75WF+EuxQrUMUhN2U6\niRfKXYQKog6YYW4OI0ls7e68RTyPO53duZa1zy1r7/f9LosW1Q/t8cN8/l/jfI9CzVRxSEP12848\nnrz8IurID/O6UHyXRyk4zkeLY7z8w3P0+BQhT4I09Iv5muUtZlbStQeNfI/MfcJFLlm0Q1UUypw7\n8+PQlFIrkogaEjmzJiuvo599p5zEOiCDcFIzZLLO58v5wA7Fm3kSpb2s+G+eQqTttcNmHOSCSRcC\nXd6gLq79C/gHf4qzfkFAyyAAAAAASUVORK5CYII=\n",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAKAAAAAVBAMAAAAz5vjYAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMnZUzRC73UTviSKZZqszMyTAAAACz0lEQVQ4EbVUTWgTQRT+tsluNtmkVhE82S6RHqxBc1GpCi4Yq8c9iGAPWooUpChFvfUSeil6aSwVCvUQioLUogGhEFRYpXgQ0VgKQlVIRUH8gUax1FqtbyaZnZ2QHn2X+b7vvffNvJlNgP8e24M7vAqShlgrBeRES5Voo1K0eiQGmmsVQU3FMwpdrDJtVaphV2JCjsIakKOKFlYYJx2qJKheUHXBmvIC8bWOMm1CKcCpGg3Zqi5YVN1Iz/GEtVnkYQ74kINwuso3MhxWy3EE2o6kM/MXW29s2W1TMuYBevLL0CS6n8C6zjnrEYb6s6k0upO3eR/p01Q+baPoYnicPfg0TgIDuAB02dZvKjBI7MZr+zLynWhaQsIhkUIYFtPahDYIw+Z9wHk6QyiHPdB7Uy+obh5XXOSZ4TngDwkRGvEW7rkHrfQsjB6EBpidNMwgVInkoVV4H3AJuB930AcdVpnq9iOyvAvM8CzwnYRoFnDRRYNgFJtsaEskUtROqK8RbqNNV3kfME7lUZu3trK6Nug31z1m2CcNq8j8ibaWqmEskzk2lsl4gMk2PZwFlnkfN8SIq1UoRWlm+ACJlYAhG5kGogPGHeykMnVk/4RrvI+PjFk05YBDzA/DOAB8CxiyR9lLj9EOo8zk+kfJ0PRRD+YK7+OPQmVUG/LQTIbz6HTRz+atjRwrw/xhLFklxHtMeqW4x/b17xDFEu5o1xDO8z7gLuWeYqSEbQtzxwk/x9W5hWzr+sfW9cV9v8p8RP19e8c7epXPKfqOwlnwEJ+N/rWjgIdTn3gfZdiHfXryZQFeYixLmJmqMSgp3SKKNSoMZbaKIgW2zgrZosusi7eCf8CZLDBZoybdbaNI0NU5+opIxWyB/NXgWxK9iF6a2/ETG4B+RGzDFskTAsjV/4MdSrXQL1HYy4I6NAMz+djX+n0kwSMJgTdB0hDT9yAjUcA/yrOz2e5GeOoAAAAASUVORK5CYII=\n",
"text/latex": [
"$$\\sin{\\left (x y \\right )} + \\cos{\\left (y z \\right )}$$"
],
@@ -1357,7 +1357,7 @@
"sin(x⋅y) + cos(y⋅z)"
]
},
"execution_count": 55,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@@ -1368,24 +1368,24 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "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\nojUnOm7/Aywn7A6s7fMGAAAAAElFTkSuQmCC\n",
"image/png": "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\n",
"text/latex": [
"$$x \\cos{\\left (y z \\right )} + \\begin{cases} 0 & \\text{for}\\: y = 0 \\\\- \\frac{1}{y} \\cos{\\left (x y \\right )} & \\text{otherwise} \\end{cases}$$"
"$$x \\cos{\\left (y z \\right )} + \\begin{cases} - \\frac{\\cos{\\left (x y \\right )}}{y} & \\text{for}\\: y \\neq 0 \\\\0 & \\text{otherwise} \\end{cases}$$"
],
"text/plain": [
" ⎛⎧ 0 for y = 0⎞\n",
" ⎛⎧-cos(x⋅y) ⎞\n",
" ⎜⎪────────── for y ≠ 0⎟\n",
"x⋅cos(y⋅z) + ⎜⎨ y ⎟\n",
" ⎜⎪ ⎟\n",
"x⋅cos(y⋅z) + ⎜⎨-cos(x⋅y) ⎟\n",
" ⎜⎪────────── otherwise⎟\n",
" ⎝⎩ y ⎠"
" ⎝⎩ 0 otherwise⎠"
]
},
"execution_count": 56,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}


+ 10
- 10
2_knn/knn_classification.ipynb
File diff suppressed because it is too large
View File


+ 43
- 38
3_kmeans/k-means.ipynb View File

@@ -219,7 +219,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -304,7 +304,7 @@
"4 5.0 3.6 1.4 0.2 Iris-setosa"
]
},
"execution_count": 1,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -319,6 +319,7 @@
"from numpy import *\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import random\n",
"\n",
"# Load dataset\n",
"names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']\n",
@@ -328,7 +329,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"metadata": {
"lines_to_next_cell": 2
},
@@ -364,7 +365,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"metadata": {
"lines_to_next_cell": 2
},
@@ -384,7 +385,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"metadata": {
"lines_to_end_of_cell_marker": 2,
"scrolled": true
@@ -414,10 +415,12 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"def randChosenCent(dataSet,k):\n",
" \"\"\"初始化聚类中心:通过在区间范围随机产生的值作为新的中心点\"\"\"\n",
"\n",
@@ -425,15 +428,21 @@
" m=shape(dataSet)[0]\n",
" # 初始化列表\n",
" centroidsIndex=[]\n",
" \n",
" #生成类似于样本索引的列表\n",
" dataIndex=list(range(m))\n",
" for i in range(k):\n",
" #生成随机数\n",
" randIndex=random.randint(0,len(dataIndex))\n",
" #将随机产生的样本的索引放入centroidsIndex\n",
" centroidsIndex.append(dataIndex[randIndex])\n",
" #删除已经被抽中的样本\n",
" del dataIndex[randIndex]\n",
" if False:\n",
" for i in range(k):\n",
" #生成随机数\n",
" randIndex=random.randint(0,len(dataIndex))\n",
" #将随机产生的样本的索引放入centroidsIndex\n",
" centroidsIndex.append(dataIndex[randIndex])\n",
" #删除已经被抽中的样本\n",
" del dataIndex[randIndex]\n",
" else:\n",
" random.shuffle(dataIndex)\n",
" centroidsIndex = dataIndex[:k]\n",
" \n",
" #根据索引获取样本\n",
" centroids = dataSet.iloc[centroidsIndex]\n",
" return mat(centroids)"
@@ -441,7 +450,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@@ -506,32 +515,28 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"最初的中心= [[6.4 2.7]\n",
" [5. 3.4]\n",
" [6.8 2.8]]\n",
"the SSE of 1th iteration is 52.450000\n",
"the SSE of 2th iteration is 38.174960\n",
"the SSE of 3th iteration is 38.055060\n",
"the SSE of 4th iteration is 37.980634\n",
"the SSE of 5th iteration is 37.859100\n",
"the SSE of 6th iteration is 37.783402\n",
"the SSE of 7th iteration is 37.694864\n",
"the SSE of 8th iteration is 37.636365\n",
"the SSE of 9th iteration is 37.535779\n",
"the SSE of 10th iteration is 37.454640\n",
"the SSE of 11th iteration is 37.355678\n",
"the SSE of 12th iteration is 37.290519\n",
"the SSE of 13th iteration is 37.229337\n",
"the SSE of 14th iteration is 37.201302\n",
"the SSE of 15th iteration is 37.155048\n",
"the SSE of 16th iteration is 37.141172\n"
"最初的中心= [[6.2 2.2]\n",
" [6.3 2.5]\n",
" [7.7 3.8]]\n",
"the SSE of 1th iteration is 189.420000\n",
"the SSE of 2th iteration is 70.447978\n",
"the SSE of 3th iteration is 56.041643\n",
"the SSE of 4th iteration is 49.785857\n",
"the SSE of 5th iteration is 45.985699\n",
"the SSE of 6th iteration is 43.078623\n",
"the SSE of 7th iteration is 40.594295\n",
"the SSE of 8th iteration is 37.791783\n",
"the SSE of 9th iteration is 37.235470\n",
"the SSE of 10th iteration is 37.201302\n",
"the SSE of 11th iteration is 37.155048\n",
"the SSE of 12th iteration is 37.141172\n"
]
}
],
@@ -543,7 +548,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
@@ -648,7 +653,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -701,7 +706,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -753,7 +758,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 29,
"metadata": {},
"outputs": [
{


+ 91
- 98
6_pytorch/1_NN/deep-nn.ipynb
File diff suppressed because it is too large
View File


+ 140
- 102
6_pytorch/1_NN/nn-sequential-module.ipynb
File diff suppressed because it is too large
View File


+ 20
- 14
6_pytorch/1_NN/optimizer/adagrad.ipynb View File

@@ -15,6 +15,8 @@
"$$\n",
"\n",
"这里的 $\\epsilon$ 是为了数值稳定性而加上的,因为有可能 s 的值为 0,那么 0 出现在分母就会出现无穷大的情况,通常 $\\epsilon$ 取 $10^{-10}$,这样不同的参数由于梯度不同,他们对应的 s 大小也就不同,所以上面的公式得到的学习率也就不同,这也就实现了自适应的学习率。\n",
"\n",
"FIXME: need improve. ref: https://ruder.io/optimizing-gradient-descent/index.html#adagrad\n",
"\n"
]
},
@@ -32,9 +34,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"def sgd_adagrad(parameters, sqrs, lr):\n",
@@ -48,9 +48,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
@@ -70,8 +68,8 @@
" x = torch.from_numpy(x)\n",
" return x\n",
"\n",
"train_set = MNIST('./data', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('./data', train=False, transform=data_tf, download=True)\n",
"train_set = MNIST('../../../data/mnist', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('../../../data/mnist', train=False, transform=data_tf, download=True)\n",
"\n",
"# 定义 loss 函数\n",
"criterion = nn.CrossEntropyLoss()"
@@ -83,15 +81,23 @@
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:31: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n",
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:33: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch: 0, Train Loss: 0.406752\n",
"epoch: 1, Train Loss: 0.248588\n",
"epoch: 2, Train Loss: 0.211789\n",
"epoch: 3, Train Loss: 0.188928\n",
"epoch: 4, Train Loss: 0.172839\n",
"使用时间: 54.70610 s\n"
"epoch: 0, Train Loss: 0.404529\n",
"epoch: 1, Train Loss: 0.243532\n",
"epoch: 2, Train Loss: 0.201834\n",
"epoch: 3, Train Loss: 0.176955\n",
"epoch: 4, Train Loss: 0.159980\n",
"使用时间: 31.24649 s\n"
]
}
],


+ 58
- 64
6_pytorch/1_NN/param_initialize.ipynb View File

@@ -26,9 +26,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
@@ -39,9 +37,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# 定义一个 Sequential 模型\n",
@@ -57,9 +53,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# 访问第一层的参数\n",
@@ -77,15 +71,14 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
" 0.1236 -0.1731 -0.0479 ... 0.0031 0.0784 0.1239\n",
" 0.0713 0.1615 0.0500 ... -0.1757 -0.1274 -0.1625\n",
" 0.0638 -0.1543 -0.0362 ... 0.0316 -0.1774 -0.1242\n",
" ... ⋱ ... \n",
" 0.1551 0.1772 0.1537 ... 0.0730 0.0950 0.0627\n",
" 0.0495 0.0896 0.0243 ... -0.1302 -0.0256 -0.0326\n",
"-0.1193 -0.0989 -0.1795 ... 0.0939 0.0774 -0.0751\n",
"[torch.FloatTensor of size 40x30]\n",
"\n"
"tensor([[ 0.0276, -0.1197, -0.0397, ..., 0.0759, -0.1630, 0.1599],\n",
" [ 0.1419, 0.0903, -0.1630, ..., -0.0615, 0.1502, 0.0596],\n",
" [-0.0451, 0.1103, 0.1070, ..., -0.1506, -0.1346, 0.1284],\n",
" ...,\n",
" [-0.0975, -0.1264, 0.0738, ..., -0.1058, -0.1396, 0.1800],\n",
" [-0.1352, 0.0287, 0.0779, ..., 0.1773, -0.1585, 0.1046],\n",
" [-0.1194, 0.1526, -0.0018, ..., 0.0946, -0.1453, -0.1512]],\n",
" requires_grad=True)\n"
]
}
],
@@ -103,9 +96,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# 定义一个 Tensor 直接对其进行替换\n",
@@ -122,15 +113,14 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
" 4.5768 3.6175 3.3098 ... 4.7374 4.0164 3.3037\n",
" 4.1809 3.5624 3.1452 ... 3.0305 4.4444 4.1058\n",
" 3.5277 4.3712 3.7859 ... 3.5760 4.8559 4.3252\n",
" ... ⋱ ... \n",
" 4.8983 3.9855 3.2842 ... 4.7683 4.7590 3.3498\n",
" 4.9168 4.5723 3.5870 ... 3.2032 3.9842 3.2484\n",
" 4.2532 4.6352 4.4857 ... 3.7543 3.9885 4.4211\n",
"[torch.DoubleTensor of size 40x30]\n",
"\n"
"tensor([[3.0403, 4.7550, 4.9311, ..., 3.0626, 4.3593, 3.9823],\n",
" [4.4812, 4.5463, 4.4052, ..., 3.7669, 3.4201, 4.6582],\n",
" [3.7711, 3.3997, 4.1416, ..., 3.4086, 3.1681, 4.0410],\n",
" ...,\n",
" [4.4137, 4.1779, 4.8741, ..., 3.4678, 3.4457, 4.7489],\n",
" [3.8246, 4.2699, 4.9944, ..., 4.8576, 3.8945, 4.5525],\n",
" [3.4959, 3.6991, 4.4047, ..., 4.7308, 3.5796, 3.2013]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
],
@@ -344,10 +334,8 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from torch.nn import init"
@@ -355,7 +343,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -363,15 +351,14 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
" 0.8453 0.2891 -0.5276 ... -0.1530 -0.4474 -0.5470\n",
"-0.1983 -0.4530 -0.1950 ... 0.4107 -0.4889 0.3654\n",
" 0.9149 -0.5641 -0.6594 ... 0.0734 0.1354 -0.4152\n",
" ... ⋱ ... \n",
"-0.4718 -0.5125 -0.5572 ... 0.0824 -0.6551 0.0840\n",
"-0.2374 -0.0036 0.6497 ... 0.7856 -0.1367 -0.8795\n",
" 0.0774 0.2609 -0.2358 ... -0.8196 0.1696 0.5976\n",
"[torch.DoubleTensor of size 40x30]\n",
"\n"
"tensor([[3.0403, 4.7550, 4.9311, ..., 3.0626, 4.3593, 3.9823],\n",
" [4.4812, 4.5463, 4.4052, ..., 3.7669, 3.4201, 4.6582],\n",
" [3.7711, 3.3997, 4.1416, ..., 3.4086, 3.1681, 4.0410],\n",
" ...,\n",
" [4.4137, 4.1779, 4.8741, ..., 3.4678, 3.4457, 4.7489],\n",
" [3.8246, 4.2699, 4.9944, ..., 4.8576, 3.8945, 4.5525],\n",
" [3.4959, 3.6991, 4.4047, ..., 4.7308, 3.5796, 3.2013]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
],
@@ -381,24 +368,32 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:1: UserWarning: nn.init.xavier_uniform is now deprecated in favor of nn.init.xavier_uniform_.\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
"Parameter containing:\n",
"-0.2114 0.2704 -0.2186 ... 0.1727 0.2158 0.0775\n",
"-0.0736 -0.0565 0.0844 ... 0.1793 0.2520 -0.0047\n",
" 0.1331 -0.1843 0.2426 ... -0.2199 -0.0689 0.1756\n",
" ... ⋱ ... \n",
" 0.2751 -0.1404 0.1225 ... 0.1926 0.0175 -0.2099\n",
" 0.0970 -0.0733 -0.2461 ... 0.0605 0.1915 -0.1220\n",
" 0.0199 0.1283 -0.1384 ... -0.0344 -0.0560 0.2285\n",
"[torch.DoubleTensor of size 40x30]"
"tensor([[-0.0889, 0.2279, 0.1816, ..., 0.1091, 0.0207, -0.2063],\n",
" [ 0.0394, 0.1860, 0.1261, ..., 0.2250, -0.2881, 0.0727],\n",
" [-0.2252, -0.0639, 0.2077, ..., 0.0328, -0.0075, 0.0339],\n",
" ...,\n",
" [-0.0932, 0.2806, -0.2377, ..., -0.2087, 0.0325, 0.0504],\n",
" [-0.2305, 0.2866, -0.1872, ..., 0.2127, 0.1487, 0.0645],\n",
" [-0.0072, 0.2771, 0.0928, ..., -0.0234, -0.1238, 0.1197]],\n",
" dtype=torch.float64, requires_grad=True)"
]
},
"execution_count": 15,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -409,7 +404,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -417,15 +412,14 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"-0.2114 0.2704 -0.2186 ... 0.1727 0.2158 0.0775\n",
"-0.0736 -0.0565 0.0844 ... 0.1793 0.2520 -0.0047\n",
" 0.1331 -0.1843 0.2426 ... -0.2199 -0.0689 0.1756\n",
" ... ⋱ ... \n",
" 0.2751 -0.1404 0.1225 ... 0.1926 0.0175 -0.2099\n",
" 0.0970 -0.0733 -0.2461 ... 0.0605 0.1915 -0.1220\n",
" 0.0199 0.1283 -0.1384 ... -0.0344 -0.0560 0.2285\n",
"[torch.DoubleTensor of size 40x30]\n",
"\n"
"tensor([[-0.0889, 0.2279, 0.1816, ..., 0.1091, 0.0207, -0.2063],\n",
" [ 0.0394, 0.1860, 0.1261, ..., 0.2250, -0.2881, 0.0727],\n",
" [-0.2252, -0.0639, 0.2077, ..., 0.0328, -0.0075, 0.0339],\n",
" ...,\n",
" [-0.0932, 0.2806, -0.2377, ..., -0.2087, 0.0325, 0.0504],\n",
" [-0.2305, 0.2866, -0.1872, ..., 0.2127, 0.1487, 0.0645],\n",
" [-0.0072, 0.2771, 0.0928, ..., -0.0234, -0.1238, 0.1197]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
],


BIN
6_pytorch/2_CNN/CNN_Introduction.pptx View File


+ 6
- 3
6_pytorch/2_CNN/lr-decay.ipynb View File

@@ -5,7 +5,7 @@
"metadata": {},
"source": [
"# 学习率衰减\n",
"对于基于一阶梯度进行优化的方法而言,开始的时候更新的幅度是比较大的,也就是说开始的学习率可以设置大一点,但是当训练集的 loss 下降到一定程度之后,使用这个太大的学习率就会导致 loss 一直来回震荡,比如\n",
"对于基于一阶梯度进行优化的方法而言,开始的时候更新的幅度是比较大的,也就是说开始的学习率可以设置大一点,但是当训练集的 loss 下降到一定程度之后,继续使用这个太大的学习率就会导致 loss 一直来回震荡,比如\n",
"\n",
"![](https://ws4.sinaimg.cn/large/006tNc79ly1fmrvdlncomj30bf0aywet.jpg)"
]
@@ -265,9 +265,12 @@
"if torch.cuda.is_available():\n",
" net = net.cuda()\n",
"prev_time = datetime.now()\n",
"for epoch in range(30):\n",
"for epoch in range(100):\n",
" if epoch == 20:\n",
" set_learning_rate(optimizer, 0.01) # 80 次修改学习率为 0.01\n",
" set_learning_rate(optimizer, 0.01) # 20 次修改学习率为 0.01\n",
" elif epoch == 60:\n",
" set_learning_rate(optimizer, 0.005) # 60 次修改学习率为 0.01\n",
"\n",
" train_loss = 0\n",
" net = net.train()\n",
" for im, label in train_data:\n",


+ 11
- 15
6_pytorch/2_CNN/resnet.ipynb View File

@@ -31,13 +31,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T12:56:06.772059Z",
"start_time": "2017-12-22T12:56:06.766027Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -54,13 +53,12 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T12:47:49.222432Z",
"start_time": "2017-12-22T12:47:49.217940Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -70,13 +68,12 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T13:14:02.429145Z",
"start_time": "2017-12-22T13:14:02.383322Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -114,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T13:14:05.793185Z",
@@ -142,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T13:14:11.929120Z",
@@ -177,13 +174,12 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T13:27:46.099404Z",
"start_time": "2017-12-22T13:27:45.986235Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -248,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T13:28:00.597030Z",


+ 41
- 35
6_pytorch/2_CNN/vgg.ipynb View File

@@ -35,13 +35,12 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T09:01:51.296457Z",
"start_time": "2017-12-22T09:01:50.883050Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -64,19 +63,18 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T09:01:51.312500Z",
"start_time": "2017-12-22T09:01:51.298777Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
"def vgg_block(num_convs, in_channels, out_channels):\n",
" net = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), nn.ReLU(True)] # 定义第一层\n",
" \n",
"\n",
" for i in range(num_convs-1): # 定义后面的很多层\n",
" net.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1))\n",
" net.append(nn.ReLU(True))\n",
@@ -94,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T08:20:40.819497Z",
@@ -107,13 +105,13 @@
"output_type": "stream",
"text": [
"Sequential(\n",
" (0): Conv2d (64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): Conv2d (128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): Conv2d (128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (4): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (5): ReLU(inplace)\n",
" (6): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
")\n"
]
}
@@ -125,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T07:52:04.632406Z",
@@ -159,13 +157,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T09:01:54.497712Z",
"start_time": "2017-12-22T09:01:54.489255Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -187,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T09:01:55.149378Z",
@@ -201,42 +198,52 @@
"text": [
"Sequential(\n",
" (0): Sequential(\n",
" (0): Conv2d (3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (1): Sequential(\n",
" (0): Conv2d (64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (2): Sequential(\n",
" (0): Conv2d (128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): Conv2d (256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (4): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (5): ReLU(inplace)\n",
" (6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (3): Sequential(\n",
" (0): Conv2d (256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (5): ReLU(inplace)\n",
" (6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (4): Sequential(\n",
" (0): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (0): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (1): ReLU(inplace)\n",
" (2): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (3): ReLU(inplace)\n",
" (4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1))\n",
" (4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (5): ReLU(inplace)\n",
" (6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
")\n"
]
}
],
"source": [
"vgg_net = vgg_stack((1, 1, 2, 2, 2), ((3, 64), (64, 128), (128, 256), (256, 512), (512, 512)))\n",
"vgg_net = vgg_stack((2, 2, 3, 3, 3), ((3, 64), (64, 128), (128, 256), (256, 512), (512, 512)))\n",
"print(vgg_net)"
]
},
@@ -249,7 +256,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T08:52:44.049650Z",
@@ -280,13 +287,12 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-22T09:01:57.323034Z",
"start_time": "2017-12-22T09:01:57.306864Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [


+ 48
- 0
demo_code/3_CNN_VGG16.py View File

@@ -0,0 +1,48 @@

import sys
sys.path.append('..')

import numpy as np
import torch
from torch import nn
from torch.autograd import Variable
from torchvision.datasets import CIFAR10


def vgg_block(num_convs, in_channels, out_channels):
net = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), \
nn.ReLU(True)] # 定义第一层

for i in range(num_convs-1): # 定义后面的很多层
net.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1))
net.append(nn.ReLU(True))
net.append(nn.MaxPool2d(2, 2)) # 定义池化层
return nn.Sequential(*net)
def vgg_stack(num_convs, channels):
net = []
for n, c in zip(num_convs, channels):
in_c = c[0]
out_c = c[1]
net.append(vgg_block(n, in_c, out_c))
return nn.Sequential(*net)
vgg_net = vgg_stack((2, 2, 3, 3, 3), \
((3, 64), (64, 128), (128, 256), (256, 512), (512, 512)))


class vgg(nn.Module):
def __init__(self):
super(vgg, self).__init__()
self.feature = vgg_net
self.fc = nn.Sequential(
nn.Linear(512, 100),
nn.ReLU(True),
nn.Linear(100, 10)
)
def forward(self, x):
x = self.feature(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
return x

+ 1
- 1
tips/构建深度神经网络的一些实战建议.md View File

@@ -8,7 +8,7 @@

更多详细的深度学习技巧等可以参考:
* [Machine Learning Yearning 中文版 - 《机器学习训练秘籍》](https://github.com/deeplearning-ai/machine-learning-yearning-cn) ([在线阅读](https://deeplearning-ai.github.io/machine-learning-yearning-cn/))
* [33个神经网络「炼丹」技巧](https://www.toutiao.com/a6761273383452672524)


## 常见的一些tips


Loading…
Cancel
Save