|
681 | 681 | },
|
682 | 682 | {
|
683 | 683 | "cell_type": "code",
|
684 | | - "execution_count": 1, |
| 684 | + "execution_count": 2, |
685 | 685 | "metadata": {},
|
686 | 686 | "outputs": [],
|
687 | 687 | "source": [
|
688 | 688 | "import numpy as np"
|
689 | 689 | ]
|
690 | 690 | },
|
| 691 | + { |
| 692 | + "cell_type": "markdown", |
| 693 | + "metadata": {}, |
| 694 | + "source": [ |
| 695 | + "## Looping Over Arrays" |
| 696 | + ] |
| 697 | + }, |
| 698 | + { |
| 699 | + "cell_type": "markdown", |
| 700 | + "metadata": {}, |
| 701 | + "source": [ |
| 702 | + "You can directly loop over rows of an array easily, without using indices:" |
| 703 | + ] |
| 704 | + }, |
| 705 | + { |
| 706 | + "cell_type": "code", |
| 707 | + "execution_count": 37, |
| 708 | + "metadata": {}, |
| 709 | + "outputs": [ |
| 710 | + { |
| 711 | + "data": { |
| 712 | + "text/plain": [ |
| 713 | + "array([[1, 2, 3],\n", |
| 714 | + " [4, 5, 6],\n", |
| 715 | + " [7, 8, 9]])" |
| 716 | + ] |
| 717 | + }, |
| 718 | + "execution_count": 37, |
| 719 | + "metadata": {}, |
| 720 | + "output_type": "execute_result" |
| 721 | + } |
| 722 | + ], |
| 723 | + "source": [ |
| 724 | + "my_array_2d = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)\n", |
| 725 | + "my_array_2d" |
| 726 | + ] |
| 727 | + }, |
| 728 | + { |
| 729 | + "cell_type": "code", |
| 730 | + "execution_count": 38, |
| 731 | + "metadata": {}, |
| 732 | + "outputs": [ |
| 733 | + { |
| 734 | + "name": "stdout", |
| 735 | + "output_type": "stream", |
| 736 | + "text": [ |
| 737 | + "[1 2 3]\n", |
| 738 | + "[4 5 6]\n", |
| 739 | + "[7 8 9]\n" |
| 740 | + ] |
| 741 | + } |
| 742 | + ], |
| 743 | + "source": [ |
| 744 | + "for row in my_array_2d:\n", |
| 745 | + " print(row)" |
| 746 | + ] |
| 747 | + }, |
| 748 | + { |
| 749 | + "cell_type": "markdown", |
| 750 | + "metadata": {}, |
| 751 | + "source": [ |
| 752 | + "To loop over columns, just take the transpose using `.T` and loop over the rows of the transpose." |
| 753 | + ] |
| 754 | + }, |
| 755 | + { |
| 756 | + "cell_type": "code", |
| 757 | + "execution_count": 39, |
| 758 | + "metadata": { |
| 759 | + "scrolled": true |
| 760 | + }, |
| 761 | + "outputs": [ |
| 762 | + { |
| 763 | + "name": "stdout", |
| 764 | + "output_type": "stream", |
| 765 | + "text": [ |
| 766 | + "[1 4 7]\n", |
| 767 | + "[2 5 8]\n", |
| 768 | + "[3 6 9]\n" |
| 769 | + ] |
| 770 | + } |
| 771 | + ], |
| 772 | + "source": [ |
| 773 | + "for col in my_array_2d.T:\n", |
| 774 | + " print(col)" |
| 775 | + ] |
| 776 | + }, |
| 777 | + { |
| 778 | + "cell_type": "markdown", |
| 779 | + "metadata": {}, |
| 780 | + "source": [ |
| 781 | + "### np.ndenumerate" |
| 782 | + ] |
| 783 | + }, |
| 784 | + { |
| 785 | + "cell_type": "markdown", |
| 786 | + "metadata": {}, |
| 787 | + "source": [ |
| 788 | + "`np.ndenumerate` allows looping over the array and enumerating indices in a concise, clean manner." |
| 789 | + ] |
| 790 | + }, |
| 791 | + { |
| 792 | + "cell_type": "code", |
| 793 | + "execution_count": 44, |
| 794 | + "metadata": {}, |
| 795 | + "outputs": [ |
| 796 | + { |
| 797 | + "name": "stdout", |
| 798 | + "output_type": "stream", |
| 799 | + "text": [ |
| 800 | + "Index: (0, 0), Value: 1\n", |
| 801 | + "Index: (0, 1), Value: 2\n", |
| 802 | + "Index: (0, 2), Value: 3\n", |
| 803 | + "Index: (1, 0), Value: 4\n", |
| 804 | + "Index: (1, 1), Value: 5\n", |
| 805 | + "Index: (1, 2), Value: 6\n", |
| 806 | + "Index: (2, 0), Value: 7\n", |
| 807 | + "Index: (2, 1), Value: 8\n", |
| 808 | + "Index: (2, 2), Value: 9\n" |
| 809 | + ] |
| 810 | + } |
| 811 | + ], |
| 812 | + "source": [ |
| 813 | + "for index, value in np.ndenumerate(my_array_2d):\n", |
| 814 | + " print('Index: {}, Value: {}'.format(index, value))" |
| 815 | + ] |
| 816 | + }, |
691 | 817 | {
|
692 | 818 | "cell_type": "markdown",
|
693 | 819 | "metadata": {},
|
|
842 | 968 | "cell_type": "markdown",
|
843 | 969 | "metadata": {},
|
844 | 970 | "source": [
|
845 | | - "## Array Rotation" |
| 971 | + "## Array Rolling with np.roll" |
846 | 972 | ]
|
847 | 973 | },
|
848 | 974 | {
|
|
862 | 988 | "\n",
|
863 | 989 | "E.g.:\n",
|
864 | 990 | "\n",
|
865 | | - "Rotate the array `right` by `2` positions: Expected Result = ` 4 | 5 | 1 | 2 | 3 `." |
| 991 | + "Rotate the array `right` by `2` positions: Expected Result = ` 4 | 5 | 1 | 2 | 3 `.\n", |
| 992 | + "\n", |
| 993 | + "For a 1-d list, it can be done manually in a one-liner easily, but in an n-dim array, rolling along the mth dim can be easily done by `np.roll`" |
866 | 994 | ]
|
867 | 995 | },
|
868 | 996 | {
|
|
914 | 1042 | },
|
915 | 1043 | {
|
916 | 1044 | "cell_type": "code",
|
917 | | - "execution_count": 8, |
| 1045 | + "execution_count": 5, |
918 | 1046 | "metadata": {},
|
919 | 1047 | "outputs": [],
|
920 | 1048 | "source": [
|
921 | 1049 | "# multi-dimenisional cases\n",
|
922 | | - "my_array_nd = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)" |
| 1050 | + "my_array_2d = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)" |
923 | 1051 | ]
|
924 | 1052 | },
|
925 | 1053 | {
|
926 | 1054 | "cell_type": "code",
|
927 | | - "execution_count": 14, |
| 1055 | + "execution_count": 6, |
928 | 1056 | "metadata": {},
|
929 | 1057 | "outputs": [
|
930 | 1058 | {
|
|
939 | 1067 | }
|
940 | 1068 | ],
|
941 | 1069 | "source": [
|
942 | | - "print('original 2-d array: \\n', my_array_nd)" |
| 1070 | + "print('original 2-d array: \\n', my_array_2d)" |
943 | 1071 | ]
|
944 | 1072 | },
|
945 | 1073 | {
|
946 | 1074 | "cell_type": "code",
|
947 | | - "execution_count": 16, |
| 1075 | + "execution_count": 7, |
948 | 1076 | "metadata": {},
|
949 | 1077 | "outputs": [
|
950 | 1078 | {
|
|
959 | 1087 | }
|
960 | 1088 | ],
|
961 | 1089 | "source": [
|
962 | | - "my_array_nd_rolled = np.roll(my_array_nd, 1, axis=0)\n", |
963 | | - "print('rolled 2-d array: \\n', my_array_nd_rolled)" |
| 1090 | + "my_array_2d_rolled = np.roll(my_array_2d, 1, axis=0)\n", |
| 1091 | + "print('rolled 2-d array: \\n', my_array_2d_rolled)" |
| 1092 | + ] |
| 1093 | + }, |
| 1094 | + { |
| 1095 | + "cell_type": "markdown", |
| 1096 | + "metadata": {}, |
| 1097 | + "source": [ |
| 1098 | + "## Array Rotation" |
| 1099 | + ] |
| 1100 | + }, |
| 1101 | + { |
| 1102 | + "cell_type": "code", |
| 1103 | + "execution_count": 19, |
| 1104 | + "metadata": {}, |
| 1105 | + "outputs": [ |
| 1106 | + { |
| 1107 | + "data": { |
| 1108 | + "text/plain": [ |
| 1109 | + "array([[1, 2, 3],\n", |
| 1110 | + " [4, 5, 6],\n", |
| 1111 | + " [7, 8, 9]])" |
| 1112 | + ] |
| 1113 | + }, |
| 1114 | + "execution_count": 19, |
| 1115 | + "metadata": {}, |
| 1116 | + "output_type": "execute_result" |
| 1117 | + } |
| 1118 | + ], |
| 1119 | + "source": [ |
| 1120 | + "my_array_2d" |
964 | 1121 | ]
|
965 | 1122 | },
|
966 | 1123 | {
|
967 | 1124 | "cell_type": "code",
|
968 | | - "execution_count": null, |
| 1125 | + "execution_count": 26, |
969 | 1126 | "metadata": {},
|
970 | | - "outputs": [], |
971 | | - "source": [] |
| 1127 | + "outputs": [ |
| 1128 | + { |
| 1129 | + "data": { |
| 1130 | + "text/plain": [ |
| 1131 | + "array([[7, 4, 1],\n", |
| 1132 | + " [8, 5, 2],\n", |
| 1133 | + " [9, 6, 3]])" |
| 1134 | + ] |
| 1135 | + }, |
| 1136 | + "execution_count": 26, |
| 1137 | + "metadata": {}, |
| 1138 | + "output_type": "execute_result" |
| 1139 | + } |
| 1140 | + ], |
| 1141 | + "source": [ |
| 1142 | + "np.rot90(my_array_2d, k=-1)" |
| 1143 | + ] |
972 | 1144 | },
|
973 | 1145 | {
|
974 | 1146 | "cell_type": "code",
|
|
994 | 1166 | "name": "python",
|
995 | 1167 | "nbconvert_exporter": "python",
|
996 | 1168 | "pygments_lexer": "ipython3",
|
997 | | - "version": "3.6.5" |
| 1169 | + "version": "3.6.4" |
| 1170 | + }, |
| 1171 | + "toc": { |
| 1172 | + "nav_menu": {}, |
| 1173 | + "number_sections": true, |
| 1174 | + "sideBar": true, |
| 1175 | + "skip_h1_title": false, |
| 1176 | + "toc_cell": false, |
| 1177 | + "toc_position": {}, |
| 1178 | + "toc_section_display": "block", |
| 1179 | + "toc_window_display": false |
| 1180 | + }, |
| 1181 | + "varInspector": { |
| 1182 | + "cols": { |
| 1183 | + "lenName": 16, |
| 1184 | + "lenType": 16, |
| 1185 | + "lenVar": 40 |
| 1186 | + }, |
| 1187 | + "kernels_config": { |
| 1188 | + "python": { |
| 1189 | + "delete_cmd_postfix": "", |
| 1190 | + "delete_cmd_prefix": "del ", |
| 1191 | + "library": "var_list.py", |
| 1192 | + "varRefreshCmd": "print(var_dic_list())" |
| 1193 | + }, |
| 1194 | + "r": { |
| 1195 | + "delete_cmd_postfix": ") ", |
| 1196 | + "delete_cmd_prefix": "rm(", |
| 1197 | + "library": "var_list.r", |
| 1198 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 1199 | + } |
| 1200 | + }, |
| 1201 | + "types_to_exclude": [ |
| 1202 | + "module", |
| 1203 | + "function", |
| 1204 | + "builtin_function_or_method", |
| 1205 | + "instance", |
| 1206 | + "_Feature" |
| 1207 | + ], |
| 1208 | + "window_display": false |
998 | 1209 | }
|
999 | 1210 | },
|
1000 | 1211 | "nbformat": 4,
|
|
0 commit comments