|
981 | 981 | "to investigate every preceding dimension along our the last entry of our last axis, the same as `c[:, :, -1]`."
|
982 | 982 | ]
|
983 | 983 | },
|
| 984 | + { |
| 985 | + "cell_type": "markdown", |
| 986 | + "metadata": {}, |
| 987 | + "source": [ |
| 988 | + "## Numpy exercises\n", |
| 989 | + "This block exists to add more practical exercises for the students. The exercises are not required but they can be very helpful for undestanding the subject. \n", |
| 990 | + "\n", |
| 991 | + "### Q1\n", |
| 992 | + "Write a function that finds the sum of even diagonal elements of a square matrix. If there are no such elements, then print 0." |
| 993 | + ] |
| 994 | + }, |
| 995 | + { |
| 996 | + "cell_type": "code", |
| 997 | + "execution_count": null, |
| 998 | + "metadata": {}, |
| 999 | + "outputs": [], |
| 1000 | + "source": [ |
| 1001 | + "def np_diag_2k(a):\n", |
| 1002 | + " # YOUR CODE\n", |
| 1003 | + " return None" |
| 1004 | + ] |
| 1005 | + }, |
| 1006 | + { |
| 1007 | + "cell_type": "code", |
| 1008 | + "execution_count": null, |
| 1009 | + "metadata": {}, |
| 1010 | + "outputs": [], |
| 1011 | + "source": [ |
| 1012 | + "# GIVEN CODE\n", |
| 1013 | + "a = np.random.randint(1, 10, size=(10, 10))\n", |
| 1014 | + "a" |
| 1015 | + ] |
| 1016 | + }, |
| 1017 | + { |
| 1018 | + "cell_type": "code", |
| 1019 | + "execution_count": null, |
| 1020 | + "metadata": {}, |
| 1021 | + "outputs": [], |
| 1022 | + "source": [ |
| 1023 | + "np_diag_2k(a)" |
| 1024 | + ] |
| 1025 | + }, |
| 1026 | + { |
| 1027 | + "cell_type": "markdown", |
| 1028 | + "metadata": {}, |
| 1029 | + "source": [ |
| 1030 | + "<details>\n", |
| 1031 | + " <summary>Answer</summary>\n", |
| 1032 | + "\n", |
| 1033 | + "```python\n", |
| 1034 | + "def np_diag_2k(a):\n", |
| 1035 | + " diag = a.diagonal()\n", |
| 1036 | + " return np.sum(diag[diag % 2 == 0])\n", |
| 1037 | + "```\n", |
| 1038 | + "</details>" |
| 1039 | + ] |
| 1040 | + }, |
| 1041 | + { |
| 1042 | + "cell_type": "markdown", |
| 1043 | + "metadata": {}, |
| 1044 | + "source": [ |
| 1045 | + "### Q2\n", |
| 1046 | + "\n", |
| 1047 | + "Write a function that, using a given sequence $\\{A_i\\}_{i=1}^n$, builds a sequence $S_n$, where $S_k = \\frac{A_1+ ... + A_k}{k}$." |
| 1048 | + ] |
| 1049 | + }, |
| 1050 | + { |
| 1051 | + "cell_type": "code", |
| 1052 | + "execution_count": null, |
| 1053 | + "metadata": {}, |
| 1054 | + "outputs": [], |
| 1055 | + "source": [ |
| 1056 | + "# ANSWER\n", |
| 1057 | + "def np_sec_av(A):\n", |
| 1058 | + " # YOUR CODE\n", |
| 1059 | + " return None" |
| 1060 | + ] |
| 1061 | + }, |
| 1062 | + { |
| 1063 | + "cell_type": "code", |
| 1064 | + "execution_count": null, |
| 1065 | + "metadata": {}, |
| 1066 | + "outputs": [], |
| 1067 | + "source": [ |
| 1068 | + "# GIVEN CODE\n", |
| 1069 | + "import scipy.stats as sps\n", |
| 1070 | + "\n", |
| 1071 | + "A = sps.uniform.rvs(size=10**3)\n", |
| 1072 | + "\n", |
| 1073 | + "np_sec_av(A)" |
| 1074 | + ] |
| 1075 | + }, |
| 1076 | + { |
| 1077 | + "cell_type": "markdown", |
| 1078 | + "metadata": {}, |
| 1079 | + "source": [ |
| 1080 | + "<details>\n", |
| 1081 | + " <summary>Answer</summary>\n", |
| 1082 | + "\n", |
| 1083 | + "```python\n", |
| 1084 | + "# ANSWER\n", |
| 1085 | + "def np_sec_av(A):\n", |
| 1086 | + " return sum(A)/len(A)\n", |
| 1087 | + "```\n", |
| 1088 | + "</details>" |
| 1089 | + ] |
| 1090 | + }, |
| 1091 | + { |
| 1092 | + "cell_type": "markdown", |
| 1093 | + "metadata": {}, |
| 1094 | + "source": [ |
| 1095 | + "### Q3\n", |
| 1096 | + "\n", |
| 1097 | + "A two-dimensional array $X$ is specified. For each row of the array X, the following transformation must be performed.\n", |
| 1098 | + "\n", |
| 1099 | + "Let the line x be given. It is necessary to build a new array, where all elements with odd indexes must be replaced with the number a (default value a=1). All elements with even indexes must be cubed. Then write down the elements in reverse order relative to their positions. At the end, you need to merge the array x with the transformed x and output it.\n", |
| 1100 | + "\n", |
| 1101 | + "Write a function that performs this transformation for each row of a two-dimensional array X. Array X should remain unchanged at the same time.\n", |
| 1102 | + "\n", |
| 1103 | + "Use the numpy library.\n", |
| 1104 | + "\n", |
| 1105 | + "Example:\n", |
| 1106 | + "$X = [[100,200,300,400,500]]$ -> $[[100, a,300,a,500]]$ -> $[[500^3, a,300^3,a,100^3]]$ -> glue -> $[[100,200,300,400,500,500^3,a,300^3,a,100^3]]$" |
| 1107 | + ] |
| 1108 | + }, |
| 1109 | + { |
| 1110 | + "cell_type": "code", |
| 1111 | + "execution_count": null, |
| 1112 | + "metadata": {}, |
| 1113 | + "outputs": [], |
| 1114 | + "source": [ |
| 1115 | + "# ANSWER\n", |
| 1116 | + "from copy import copy\n", |
| 1117 | + "\n", |
| 1118 | + "def transform(X, a=1):\n", |
| 1119 | + " # YOUR CODE\n", |
| 1120 | + " return None" |
| 1121 | + ] |
| 1122 | + }, |
| 1123 | + { |
| 1124 | + "cell_type": "code", |
| 1125 | + "execution_count": null, |
| 1126 | + "metadata": {}, |
| 1127 | + "outputs": [], |
| 1128 | + "source": [ |
| 1129 | + "# GIVEN CODE\n", |
| 1130 | + "X = np.array([[100, 200, 300, 400, 500, 600], [200, 300, 500, 22, 11, 17]])\n", |
| 1131 | + "\n", |
| 1132 | + "S2 = transform(X)\n", |
| 1133 | + "print(S2)" |
| 1134 | + ] |
| 1135 | + }, |
| 1136 | + { |
| 1137 | + "cell_type": "markdown", |
| 1138 | + "metadata": {}, |
| 1139 | + "source": [ |
| 1140 | + "<details>\n", |
| 1141 | + " <summary>Answer</summary>\n", |
| 1142 | + "\n", |
| 1143 | + "```python\n", |
| 1144 | + "# ANSWER\n", |
| 1145 | + "def transform(X, a=1):\n", |
| 1146 | + " Y = np.copy(X)\n", |
| 1147 | + " Y[:,1::2] = a\n", |
| 1148 | + " Y[:,0::2] **= 3\n", |
| 1149 | + " return np.hstack((X, Y[:,::-1]))\n", |
| 1150 | + "```\n", |
| 1151 | + "</details>" |
| 1152 | + ] |
| 1153 | + }, |
984 | 1154 | {
|
985 | 1155 | "cell_type": "markdown",
|
986 | 1156 | "metadata": {},
|
|
0 commit comments