├── .gitattributes ├── 10_Random_sampling.ipynb ├── 10_Random_sampling_Solutions.ipynb ├── 11_Set_routines.ipynb ├── 11_Set_routines_Solutions.ipynb ├── 12_Sorting_searching_and_counting.ipynb ├── 12_Sorting_searching_and_counting_Solutions.ipynb ├── 13_Statistics.ipynb ├── 13_Statistics_solutions.ipynb ├── 1_Array_creation_routines.ipynb ├── 1_Array_creation_routines_Solution.ipynb ├── 2_Array_manipulation_routines.ipynb ├── 2_Array_manipulation_routines_Solutions.ipynb ├── 3_String_operations.ipynb ├── 3_String_operations_solutions.ipynb ├── 4_Numpy-specific_help_functions.ipynb ├── 4_Numpy-specific_help_functions_Solutions.ipynb ├── 5_Input_and_Output.ipynb ├── 5_Input_and_Output_Solutions.ipynb ├── 6_Linear_algebra.ipynb ├── 6_Linear_algebra_Solutions.ipynb ├── 7_Discrete_Fourier_Transform.ipynb ├── 7_Discrete_Fourier_Transform_solutions.ipynb ├── 8_Logic_functions.ipynb ├── 8_Logic_functions_Solutions.ipynb ├── 9_Mathematical_functions.ipynb ├── 9_Mathematical_functions_solutions.ipynb ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * linguist-language=Python 2 | -------------------------------------------------------------------------------- /10_Random_sampling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Random Sampling" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 5, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "__author__ = 'kyubyong. longinglove@nate.com'" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## Simple random data" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 49, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "array([[ 0.13879034, 0.71300174],\n", 79 | " [ 0.08121322, 0.00393554],\n", 80 | " [ 0.02349471, 0.56677474]])" 81 | ] 82 | }, 83 | "execution_count": 49, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "Q2. Create an array of shape (1000, 1000) and populate it with random samples from a standard normal distribution. And verify that the mean and standard deviation is close enough to 0 and 1 repectively." 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 42, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "-0.00110028519551\n", 109 | "0.999683483393\n" 110 | ] 111 | } 112 | ], 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Q3. Create an array of shape (3, 2) and populate it with random integers ranging from 0 to 3 (inclusive) from a discrete uniform distribution." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 44, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "array([[1, 3],\n", 133 | " [3, 0],\n", 134 | " [0, 0]])" 135 | ] 136 | }, 137 | "execution_count": 44, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Q4. Extract 1 elements from x randomly such that each of them would be associated with probabilities .3, .5, .2. Then print the result 10 times." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 3, 154 | "metadata": { 155 | "collapsed": false 156 | }, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "5 out of 10\n", 163 | "2 out of 10\n", 164 | "3 out of 10\n", 165 | "5 out of 10\n", 166 | "2 out of 10\n", 167 | "5 out of 10\n", 168 | "2 out of 10\n", 169 | "2 out of 10\n", 170 | "2 out of 10\n", 171 | "5 out of 10\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']\n" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 66, 189 | "metadata": { 190 | "collapsed": false 191 | }, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "array([5, 4, 0])" 197 | ] 198 | }, 199 | "execution_count": 66, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "## Permutations" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Q6. Shuffle numbers between 0 and 9 (inclusive)." 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 86, 223 | "metadata": { 224 | "collapsed": false 225 | }, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "[2 3 8 4 5 1 0 6 9 7]\n" 232 | ] 233 | } 234 | ], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 88, 240 | "metadata": { 241 | "collapsed": false 242 | }, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "[5 2 7 4 1 0 6 8 9 3]\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "# Or\n" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "## Random generator" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time." 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 91, 273 | "metadata": { 274 | "collapsed": true 275 | }, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 2", 283 | "language": "python", 284 | "name": "python2" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 2 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython2", 296 | "version": "2.7.10" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 0 301 | } 302 | -------------------------------------------------------------------------------- /10_Random_sampling_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Random Sampling" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 5, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "__author__ = 'kyubyong. longinglove@nate.com'" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## Simple random data" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 49, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "array([[ 0.13879034, 0.71300174],\n", 79 | " [ 0.08121322, 0.00393554],\n", 80 | " [ 0.02349471, 0.56677474]])" 81 | ] 82 | }, 83 | "execution_count": 49, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "np.random.rand(3, 2) \n", 90 | "# Or np.random.random((3,2))" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Q2. Create an array of shape (1000, 1000) and populate it with random samples from a standard normal distribution. And verify that the mean and standard deviation is close enough to 0 and 1 repectively." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 42, 103 | "metadata": { 104 | "collapsed": false 105 | }, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "-0.00110028519551\n", 112 | "0.999683483393\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "out1 = np.random.randn(1000, 1000)\n", 118 | "out2 = np.random.standard_normal((1000, 1000))\n", 119 | "out3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\n", 120 | "assert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\n", 121 | "assert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\n", 122 | "assert np.allclose(np.std(out1), np.std(out2), atol=0.1)\n", 123 | "assert np.allclose(np.std(out1), np.std(out3), atol=0.1)\n", 124 | "print np.mean(out3)\n", 125 | "print np.std(out1)\n" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Q3. Create an array of shape (3, 2) and populate it with random integers ranging from 0 to 3 (inclusive) from a discrete uniform distribution." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 44, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "array([[1, 3],\n", 146 | " [3, 0],\n", 147 | " [0, 0]])" 148 | ] 149 | }, 150 | "execution_count": 44, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "np.random.randint(0, 4, (3, 2))" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "Q4. Extract 1 elements from x randomly such that each of them would be associated with probabilities .3, .5, .2. Then print the result 10 times." 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 58, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 60, 180 | "metadata": { 181 | "collapsed": false 182 | }, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "2 out of 10\n", 189 | "5 out of 10\n", 190 | "3 out of 10\n", 191 | "5 out of 10\n", 192 | "5 out of 10\n", 193 | "5 out of 10\n", 194 | "2 out of 10\n", 195 | "2 out of 10\n", 196 | "5 out of 10\n", 197 | "5 out of 10\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "for _ in range(10):\n", 203 | " print np.random.choice(x, p=[.3, .5, .2])" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 66, 216 | "metadata": { 217 | "collapsed": false 218 | }, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "array([5, 4, 0])" 224 | ] 225 | }, 226 | "execution_count": 66, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "np.random.choice(10, 3, replace=False)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "## Permutations" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "Q6. Shuffle numbers between 0 and 9 (inclusive)." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 86, 252 | "metadata": { 253 | "collapsed": false 254 | }, 255 | "outputs": [ 256 | { 257 | "name": "stdout", 258 | "output_type": "stream", 259 | "text": [ 260 | "[2 3 8 4 5 1 0 6 9 7]\n" 261 | ] 262 | } 263 | ], 264 | "source": [ 265 | "x = np.arange(10)\n", 266 | "np.random.shuffle(x)\n", 267 | "print x" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 88, 273 | "metadata": { 274 | "collapsed": false 275 | }, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "[5 2 7 4 1 0 6 8 9 3]\n" 282 | ] 283 | } 284 | ], 285 | "source": [ 286 | "# Or\n", 287 | "print np.random.permutation(10)" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "## Random generator" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time." 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 91, 307 | "metadata": { 308 | "collapsed": true 309 | }, 310 | "outputs": [], 311 | "source": [ 312 | "np.random.seed(10)" 313 | ] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 2", 319 | "language": "python", 320 | "name": "python2" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 2 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython2", 332 | "version": "2.7.10" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 0 337 | } 338 | -------------------------------------------------------------------------------- /11_Set_routines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Set routines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "'1.11.2'" 30 | ] 31 | }, 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "np.__version__" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 6, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "author = 'kyubyong. longinglove@nate.com'" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Making proper sets" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "Q1. Get unique elements and reconstruction indices from x. And reconstruct x." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 15, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "unique elements = [1 2 3 4 6]\n", 74 | "reconstruction indices = [0 1 4 3 1 2 1]\n", 75 | "reconstructed = [1 2 6 4 2 3 2]\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "x = np.array([1, 2, 6, 4, 2, 3, 2])\n", 81 | "\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Boolean operations" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "Q2. Create a boolean array of the same shape as x. If each element of x is present in y, the result will be True, otherwise False." 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 19, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "[ True True False False True]\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "x = np.array([0, 1, 2, 5, 0])\n", 113 | "y = np.array([0, 1])\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Q3. Find the unique intersection of x and y." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 20, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "[0 1]\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "x = np.array([0, 1, 2, 5, 0])\n", 138 | "y = np.array([0, 1, 4])\n" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "Q4. Find the unique elements of x that are not present in y." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 21, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "[2 5]\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "x = np.array([0, 1, 2, 5, 0])\n", 163 | "y = np.array([0, 1, 4])\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "Q5. Find the xor elements of x and y." 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 40, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "[2 4 5]\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "x = np.array([0, 1, 2, 5, 0])\n", 188 | "y = np.array([0, 1, 4])\n" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "Q6. Find the union of x and y." 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 42, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "[0 1 2 4 5]\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "x = np.array([0, 1, 2, 5, 0])\n", 213 | "y = np.array([0, 1, 4])\n" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": { 220 | "collapsed": true 221 | }, 222 | "outputs": [], 223 | "source": [] 224 | } 225 | ], 226 | "metadata": { 227 | "kernelspec": { 228 | "display_name": "Python 3", 229 | "language": "python", 230 | "name": "python3" 231 | }, 232 | "language_info": { 233 | "codemirror_mode": { 234 | "name": "ipython", 235 | "version": 3 236 | }, 237 | "file_extension": ".py", 238 | "mimetype": "text/x-python", 239 | "name": "python", 240 | "nbconvert_exporter": "python", 241 | "pygments_lexer": "ipython3", 242 | "version": "3.5.2" 243 | } 244 | }, 245 | "nbformat": 4, 246 | "nbformat_minor": 1 247 | } 248 | -------------------------------------------------------------------------------- /11_Set_routines_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Set routines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 5, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 6, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "author = 'kyubyong. longinglove@nate.com'" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## Making proper sets" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Q1. Get unique elements and reconstruction indices from x. And reconstruct x." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 15, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "unique elements = [1 2 3 4 6]\n", 80 | "reconstruction indices = [0 1 4 3 1 2 1]\n", 81 | "reconstructed = [1 2 6 4 2 3 2]\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "x = np.array([1, 2, 6, 4, 2, 3, 2])\n", 87 | "out, indices = np.unique(x, return_inverse=True)\n", 88 | "print \"unique elements =\", out\n", 89 | "print \"reconstruction indices =\", indices\n", 90 | "print \"reconstructed =\", out[indices]\n" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## Boolean operations" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Q2. Create a boolean array of the same shape as x. If each element of x is present in y, the result will be True, otherwise False." 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 19, 110 | "metadata": { 111 | "collapsed": false 112 | }, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "[ True True False False True]\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "x = np.array([0, 1, 2, 5, 0])\n", 124 | "y = np.array([0, 1])\n", 125 | "print np.in1d(x, y)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Q3. Find the unique intersection of x and y." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 20, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "[0 1]\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "x = np.array([0, 1, 2, 5, 0])\n", 152 | "y = np.array([0, 1, 4])\n", 153 | "print np.intersect1d(x, y)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Q4. Find the unique elements of x that are not present in y." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 21, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "[2 5]\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "x = np.array([0, 1, 2, 5, 0])\n", 180 | "y = np.array([0, 1, 4])\n", 181 | "print np.setdiff1d(x, y)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "Q5. Find the xor elements of x and y." 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 40, 194 | "metadata": { 195 | "collapsed": false 196 | }, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "[2 4 5]\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "x = np.array([0, 1, 2, 5, 0])\n", 208 | "y = np.array([0, 1, 4])\n", 209 | "out1 = np.setxor1d(x, y)\n", 210 | "out2 = np.sort(np.concatenate((np.setdiff1d(x, y), np.setdiff1d(y, x))))\n", 211 | "assert np.allclose(out1, out2)\n", 212 | "print out1\n" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "Q6. Find the union of x and y." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 42, 225 | "metadata": { 226 | "collapsed": false 227 | }, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "[0 1 2 4 5]\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "x = np.array([0, 1, 2, 5, 0])\n", 239 | "y = np.array([0, 1, 4])\n", 240 | "out1 = np.union1d(x, y)\n", 241 | "out2 = np.sort(np.unique(np.concatenate((x, y))))\n", 242 | "assert np.allclose(out1, out2)\n", 243 | "print np.union1d(x, y)" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": { 250 | "collapsed": true 251 | }, 252 | "outputs": [], 253 | "source": [] 254 | } 255 | ], 256 | "metadata": { 257 | "kernelspec": { 258 | "display_name": "Python 2", 259 | "language": "python", 260 | "name": "python2" 261 | }, 262 | "language_info": { 263 | "codemirror_mode": { 264 | "name": "ipython", 265 | "version": 2 266 | }, 267 | "file_extension": ".py", 268 | "mimetype": "text/x-python", 269 | "name": "python", 270 | "nbconvert_exporter": "python", 271 | "pygments_lexer": "ipython2", 272 | "version": "2.7.10" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 0 277 | } 278 | -------------------------------------------------------------------------------- /12_Sorting_searching_and_counting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Soring, searching, and counting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": { 47 | "collapsed": true 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "author = 'kyubyong. longinglove@nate.com'" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## Sorting" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Q1. Sort x along the second axis." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 11, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "[[1 4]\n", 80 | " [1 3]]\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "x = np.array([[1,4],[3,1]])\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "Q2. Sort pairs of surnames and first names and return their indices. (first by surname, then by name)." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 13, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "[1 2 0]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "surnames = ('Hertz', 'Galilei', 'Hertz')\n", 112 | "first_names = ('Heinrich', 'Galileo', 'Gustav')\n" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Q3. Get the indices that would sort x along the second axis." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 17, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "[[0 1]\n", 134 | " [1 0]]\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "x = np.array([[1,4],[3,1]])\n" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value." 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 48, 152 | "metadata": { 153 | "collapsed": false 154 | }, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "x = [5 1 6 3 9 8 2 7 4 0]\n", 161 | "\n", 162 | "Check the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n", 163 | "[2 0 4 3 1 5 8 7 6 9]\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "x = np.random.permutation(10)\n", 169 | "print \"x =\", x\n" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Q5. Create the indices of an array such that its third element would be the same as the element of sorted x, and it divide other elements by their value." 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 56, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "x = [2 8 3 7 5 6 4 0 9 1]\n", 191 | "partitioned = [0 1 2 3 4 5 8 6 9 7]\n", 192 | "indices = [0 1 2 3 4 5 8 6 9 7]\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "x = np.random.permutation(10)\n", 198 | "print \"x =\", x\n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "## Searching" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "Q6. Get the maximum and minimum values and their indices of x along the second axis." 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 78, 218 | "metadata": { 219 | "collapsed": false 220 | }, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "x = [[0 5 9 8 2]\n", 227 | " [3 7 4 1 6]]\n", 228 | "maximum values = [9 7]\n", 229 | "max indices = [2 1]\n", 230 | "minimum values = [0 1]\n", 231 | "min indices = [0 3]\n" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "x = np.random.permutation(10).reshape(2, 5)\n", 237 | "print \"x =\", x\n" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs." 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 79, 250 | "metadata": { 251 | "collapsed": false 252 | }, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "maximum values ignoring NaNs = [ 4. 3.]\n", 259 | "max indices = [1 0]\n", 260 | "minimum values ignoring NaNs = [ 4. 2.]\n", 261 | "min indices = [1 1]\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "x = np.array([[np.nan, 4], [3, 2]])\n" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "Q8. Get the values and indices of the elements that are bigger than 2 in x.\n" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 113, 279 | "metadata": { 280 | "collapsed": false 281 | }, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "Values bigger than 2 = [3 3 5]\n", 288 | "Their indices are (array([0, 1, 1], dtype=int64), array([2, 1, 2], dtype=int64))\n" 289 | ] 290 | } 291 | ], 292 | "source": [ 293 | "x = np.array([[1, 2, 3], [1, 3, 5]])\n" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "Q9. Get the indices of the elements that are bigger than 2 in the flattend x." 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 100, 306 | "metadata": { 307 | "collapsed": false 308 | }, 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | "[0 1 2 3 4 5]\n" 315 | ] 316 | } 317 | ], 318 | "source": [ 319 | "x = np.array([[1, 2, 3], [1, 3, 5]])\n" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "Q10. Check the elements of x and return 0 if it is less than 0, otherwise the element itself." 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 105, 332 | "metadata": { 333 | "collapsed": false 334 | }, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "[[0 0 0]\n", 341 | " [0 0 0]\n", 342 | " [1 2 3]]\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "x = np.arange(-5, 4).reshape(3, 3)\n" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": {}, 353 | "source": [ 354 | "Q11. Get the indices where elements of y should be inserted to x to maintain order." 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 109, 360 | "metadata": { 361 | "collapsed": false 362 | }, 363 | "outputs": [ 364 | { 365 | "data": { 366 | "text/plain": [ 367 | "array([0, 2, 1, 3], dtype=int64)" 368 | ] 369 | }, 370 | "execution_count": 109, 371 | "metadata": {}, 372 | "output_type": "execute_result" 373 | } 374 | ], 375 | "source": [ 376 | "x = [1, 3, 5, 7, 9]\n", 377 | "y = [0, 4, 2, 6]\n" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "## Counting" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "Q12. Get the number of nonzero elements in x." 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 120, 397 | "metadata": { 398 | "collapsed": false 399 | }, 400 | "outputs": [ 401 | { 402 | "name": "stdout", 403 | "output_type": "stream", 404 | "text": [ 405 | "5\n" 406 | ] 407 | } 408 | ], 409 | "source": [ 410 | "x = [[0,1,7,0,0],[3,0,0,2,19]]\n" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": { 417 | "collapsed": true 418 | }, 419 | "outputs": [], 420 | "source": [] 421 | } 422 | ], 423 | "metadata": { 424 | "kernelspec": { 425 | "display_name": "Python 2", 426 | "language": "python", 427 | "name": "python2" 428 | }, 429 | "language_info": { 430 | "codemirror_mode": { 431 | "name": "ipython", 432 | "version": 2 433 | }, 434 | "file_extension": ".py", 435 | "mimetype": "text/x-python", 436 | "name": "python", 437 | "nbconvert_exporter": "python", 438 | "pygments_lexer": "ipython2", 439 | "version": "2.7.10" 440 | } 441 | }, 442 | "nbformat": 4, 443 | "nbformat_minor": 0 444 | } 445 | -------------------------------------------------------------------------------- /12_Sorting_searching_and_counting_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Soring, searching, and counting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "'1.11.2'" 28 | ] 29 | }, 30 | "execution_count": 2, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "np.__version__" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "author = 'kyubyong. longinglove@nate.com'" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Sorting" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "Q1. Sort x along the second axis." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 11, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "[[1 4]\n", 74 | " [1 3]]\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "x = np.array([[1,4],[3,1]])\n", 80 | "out = np.sort(x, axis=1)\n", 81 | "x.sort(axis=1)\n", 82 | "assert np.array_equal(out, x)\n", 83 | "print out" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Q2. Sort pairs of surnames and first names and return their indices. (first by surname, then by name)." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 13, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "[1 2 0]\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "surnames = ('Hertz', 'Galilei', 'Hertz')\n", 108 | "first_names = ('Heinrich', 'Galileo', 'Gustav')\n", 109 | "print np.lexsort((first_names, surnames))" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Q3. Get the indices that would sort x along the second axis." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 17, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "[[0 1]\n", 129 | " [1 0]]\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "x = np.array([[1,4],[3,1]])\n", 135 | "out = np.argsort(x, axis=1)\n", 136 | "print out" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 48, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "x = [5 1 6 3 9 8 2 7 4 0]\n", 156 | "\n", 157 | "Check the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n", 158 | "[2 0 4 3 1 5 8 7 6 9]\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "x = np.random.permutation(10)\n", 164 | "print \"x =\", x\n", 165 | "print \"\\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\\n\", \n", 166 | "out = np.partition(x, 5)\n", 167 | "x.partition(5) # in-place equivalent\n", 168 | "assert np.array_equal(x, out)\n", 169 | "print out\n" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Q5. Create the indices of an array such that its third element would be the same as the element of sorted x, and it divide other elements by their value." 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 56, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "x = [2 8 3 7 5 6 4 0 9 1]\n", 189 | "partitioned = [0 1 2 3 4 5 8 6 9 7]\n", 190 | "indices = [0 1 2 3 4 5 8 6 9 7]\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "x = np.random.permutation(10)\n", 196 | "print \"x =\", x\n", 197 | "partitioned = np.partition(x, 3)\n", 198 | "indices = np.argpartition(x, 3)\n", 199 | "print \"partitioned =\", partitioned\n", 200 | "print \"indices =\", partitioned\n", 201 | "assert np.array_equiv(x[indices], partitioned)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "## Searching" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "Q6. Get the maximum and minimum values and their indices of x along the second axis." 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 78, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "x = [[0 5 9 8 2]\n", 228 | " [3 7 4 1 6]]\n", 229 | "maximum values = [9 7]\n", 230 | "max indices = [2 1]\n", 231 | "minimum values = [0 1]\n", 232 | "min indices = [0 3]\n" 233 | ] 234 | } 235 | ], 236 | "source": [ 237 | "x = np.random.permutation(10).reshape(2, 5)\n", 238 | "print \"x =\", x\n", 239 | "print \"maximum values =\", np.max(x, 1)\n", 240 | "print \"max indices =\", np.argmax(x, 1)\n", 241 | "print \"minimum values =\", np.min(x, 1)\n", 242 | "print \"min indices =\", np.argmin(x, 1)\n" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs." 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 79, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "maximum values ignoring NaNs = [ 4. 3.]\n", 262 | "max indices = [1 0]\n", 263 | "minimum values ignoring NaNs = [ 4. 2.]\n", 264 | "min indices = [1 1]\n" 265 | ] 266 | } 267 | ], 268 | "source": [ 269 | "x = np.array([[np.nan, 4], [3, 2]])\n", 270 | "print \"maximum values ignoring NaNs =\", np.nanmax(x, 1)\n", 271 | "print \"max indices =\", np.nanargmax(x, 1)\n", 272 | "print \"minimum values ignoring NaNs =\", np.nanmin(x, 1)\n", 273 | "print \"min indices =\", np.nanargmin(x, 1)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "Q8. Get the values and indices of the elements that are bigger than 2 in x.\n" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 113, 286 | "metadata": {}, 287 | "outputs": [ 288 | { 289 | "name": "stdout", 290 | "output_type": "stream", 291 | "text": [ 292 | "Values bigger than 2 = [3 3 5]\n", 293 | "Their indices are (array([0, 1, 1], dtype=int64), array([2, 1, 2], dtype=int64))\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "x = np.array([[1, 2, 3], [1, 3, 5]])\n", 299 | "print \"Values bigger than 2 =\", x[x>2]\n", 300 | "print \"Their indices are \", np.nonzero(x > 2)\n", 301 | "assert np.array_equiv(x[x>2], x[np.nonzero(x > 2)])\n", 302 | "assert np.array_equiv(x[x>2], np.extract(x > 2, x))" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "Q9. Get the indices of the elements that are bigger than 2 in the flattend x." 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 4, 315 | "metadata": { 316 | "scrolled": true 317 | }, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "[2 4 5]\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "x = np.array([[1, 2, 3], [1, 3, 5]])\n", 329 | "print np.flatnonzero(x>2)\n", 330 | "assert np.array_equiv(np.flatnonzero(x), x.ravel().nonzero())" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "Q10. Check the elements of x and return 0 if it is less than 0, otherwise the element itself." 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 105, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "name": "stdout", 347 | "output_type": "stream", 348 | "text": [ 349 | "[[0 0 0]\n", 350 | " [0 0 0]\n", 351 | " [1 2 3]]\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "x = np.arange(-5, 4).reshape(3, 3)\n", 357 | "print np.where(x <0, 0, x)" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Q11. Get the indices where elements of y should be inserted to x to maintain order." 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 109, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "array([0, 2, 1, 3], dtype=int64)" 376 | ] 377 | }, 378 | "execution_count": 109, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "x = [1, 3, 5, 7, 9]\n", 385 | "y = [0, 4, 2, 6]\n", 386 | "np.searchsorted(x, y)" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": {}, 392 | "source": [ 393 | "## Counting" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": {}, 399 | "source": [ 400 | "Q12. Get the number of nonzero elements in x." 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 120, 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "name": "stdout", 410 | "output_type": "stream", 411 | "text": [ 412 | "5\n" 413 | ] 414 | } 415 | ], 416 | "source": [ 417 | "x = [[0,1,7,0,0],[3,0,0,2,19]]\n", 418 | "print np.count_nonzero(x)\n", 419 | "assert np.count_nonzero(x) == len(x[x!=0])" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": { 426 | "collapsed": true 427 | }, 428 | "outputs": [], 429 | "source": [] 430 | } 431 | ], 432 | "metadata": { 433 | "kernelspec": { 434 | "display_name": "Python 3", 435 | "language": "python", 436 | "name": "python3" 437 | }, 438 | "language_info": { 439 | "codemirror_mode": { 440 | "name": "ipython", 441 | "version": 3 442 | }, 443 | "file_extension": ".py", 444 | "mimetype": "text/x-python", 445 | "name": "python", 446 | "nbconvert_exporter": "python", 447 | "pygments_lexer": "ipython3", 448 | "version": "3.5.2" 449 | } 450 | }, 451 | "nbformat": 4, 452 | "nbformat_minor": 1 453 | } 454 | -------------------------------------------------------------------------------- /1_Array_creation_routines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Array creation routines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Ones and zeros" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "Create a new array of 2*2 integers, without initializing entries." 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 27, 38 | "metadata": { 39 | "collapsed": false 40 | }, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "array([[0, 0],\n", 46 | " [0, 0]])" 47 | ] 48 | }, 49 | "execution_count": 27, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Let X = np.array([1,2,3], [4,5,6], np.int32). \n", 61 | "Create a new array with the same shape and type as X." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 32, 67 | "metadata": { 68 | "collapsed": false 69 | }, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "array([[1, 2, 3],\n", 75 | " [4, 5, 6]])" 76 | ] 77 | }, 78 | "execution_count": 32, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "X = np.array([[1,2,3], [4,5,6]], np.int32)\n" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Create a 3-D array with ones on the diagonal and zeros elsewhere." 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 33, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "array([[ 1., 0., 0.],\n", 105 | " [ 0., 1., 0.],\n", 106 | " [ 0., 0., 1.]])" 107 | ] 108 | }, 109 | "execution_count": 33, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 35, 119 | "metadata": { 120 | "collapsed": false 121 | }, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "array([[ 1., 0., 0.],\n", 127 | " [ 0., 1., 0.],\n", 128 | " [ 0., 0., 1.]])" 129 | ] 130 | }, 131 | "execution_count": 35, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "Create a new array of 3*2 float numbers, filled with ones." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 36, 148 | "metadata": { 149 | "collapsed": false 150 | }, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "array([[ 1., 1.],\n", 156 | " [ 1., 1.],\n", 157 | " [ 1., 1.]])" 158 | ] 159 | }, 160 | "execution_count": 36, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 59, 177 | "metadata": { 178 | "collapsed": false 179 | }, 180 | "outputs": [ 181 | { 182 | "data": { 183 | "text/plain": [ 184 | "array([1, 1, 1, 1], dtype=int64)" 185 | ] 186 | }, 187 | "execution_count": 59, 188 | "metadata": {}, 189 | "output_type": "execute_result" 190 | } 191 | ], 192 | "source": [ 193 | "x = np.arange(4, dtype=np.int64)\n" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Create a new array of 3*2 float numbers, filled with zeros." 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 45, 206 | "metadata": { 207 | "collapsed": false 208 | }, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "array([[ 0., 0.],\n", 214 | " [ 0., 0.],\n", 215 | " [ 0., 0.]])" 216 | ] 217 | }, 218 | "execution_count": 45, 219 | "metadata": {}, 220 | "output_type": "execute_result" 221 | } 222 | ], 223 | "source": [] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X." 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 58, 235 | "metadata": { 236 | "collapsed": false 237 | }, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "array([0, 0, 0, 0], dtype=int64)" 243 | ] 244 | }, 245 | "execution_count": 58, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "x = np.arange(4, dtype=np.int64)\n" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "Create a new array of 2*5 uints, filled with 6." 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 49, 264 | "metadata": { 265 | "collapsed": false 266 | }, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "array([[6, 6, 6, 6, 6],\n", 272 | " [6, 6, 6, 6, 6]], dtype=uint32)" 273 | ] 274 | }, 275 | "execution_count": 49, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X." 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 79, 292 | "metadata": { 293 | "collapsed": false 294 | }, 295 | "outputs": [ 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "array([6, 6, 6, 6], dtype=int64)" 300 | ] 301 | }, 302 | "execution_count": 79, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "x = np.arange(4, dtype=np.int64)\n" 309 | ] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "## From existing data" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Create an array of [1, 2, 3]." 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 53, 328 | "metadata": { 329 | "collapsed": false 330 | }, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "array([1, 2, 3])" 336 | ] 337 | }, 338 | "execution_count": 53, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "Let x = [1, 2]. Convert it into an array." 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 60, 355 | "metadata": { 356 | "collapsed": false 357 | }, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "array([1, 2])" 363 | ] 364 | }, 365 | "execution_count": 60, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "x = [1,2]\n" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix." 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 62, 384 | "metadata": { 385 | "collapsed": false 386 | }, 387 | "outputs": [ 388 | { 389 | "data": { 390 | "text/plain": [ 391 | "matrix([[1, 2],\n", 392 | " [3, 4]])" 393 | ] 394 | }, 395 | "execution_count": 62, 396 | "metadata": {}, 397 | "output_type": "execute_result" 398 | } 399 | ], 400 | "source": [ 401 | "X = np.array([[1, 2], [3, 4]])\n" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "Let x = [1, 2]. Conver it into an array of `float`." 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 63, 414 | "metadata": { 415 | "collapsed": false 416 | }, 417 | "outputs": [ 418 | { 419 | "data": { 420 | "text/plain": [ 421 | "array([ 1., 2.])" 422 | ] 423 | }, 424 | "execution_count": 63, 425 | "metadata": {}, 426 | "output_type": "execute_result" 427 | } 428 | ], 429 | "source": [ 430 | "x = [1, 2]\n" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": {}, 436 | "source": [ 437 | "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30." 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 67, 443 | "metadata": { 444 | "collapsed": false 445 | }, 446 | "outputs": [ 447 | { 448 | "data": { 449 | "text/plain": [ 450 | "30" 451 | ] 452 | }, 453 | "execution_count": 67, 454 | "metadata": {}, 455 | "output_type": "execute_result" 456 | } 457 | ], 458 | "source": [ 459 | "x = np.array([30])\n" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x." 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 76, 472 | "metadata": { 473 | "collapsed": false 474 | }, 475 | "outputs": [ 476 | { 477 | "name": "stdout", 478 | "output_type": "stream", 479 | "text": [ 480 | "70140352 [1 2 3]\n", 481 | "70140752 [1 2 3]\n" 482 | ] 483 | } 484 | ], 485 | "source": [ 486 | "x = np.array([1, 2, 3])\n" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "## Numerical ranges" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "Create an array of 2, 4, 6, 8, ..., 100." 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 85, 506 | "metadata": { 507 | "collapsed": false 508 | }, 509 | "outputs": [ 510 | { 511 | "data": { 512 | "text/plain": [ 513 | "array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,\n", 514 | " 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,\n", 515 | " 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,\n", 516 | " 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])" 517 | ] 518 | }, 519 | "execution_count": 85, 520 | "metadata": {}, 521 | "output_type": "execute_result" 522 | } 523 | ], 524 | "source": [] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive." 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 86, 536 | "metadata": { 537 | "collapsed": false 538 | }, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "array([ 3. , 3.14285714, 3.28571429, 3.42857143,\n", 544 | " 3.57142857, 3.71428571, 3.85714286, 4. ,\n", 545 | " 4.14285714, 4.28571429, 4.42857143, 4.57142857,\n", 546 | " 4.71428571, 4.85714286, 5. , 5.14285714,\n", 547 | " 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n", 548 | " 5.85714286, 6. , 6.14285714, 6.28571429,\n", 549 | " 6.42857143, 6.57142857, 6.71428571, 6.85714286,\n", 550 | " 7. , 7.14285714, 7.28571429, 7.42857143,\n", 551 | " 7.57142857, 7.71428571, 7.85714286, 8. ,\n", 552 | " 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n", 553 | " 8.71428571, 8.85714286, 9. , 9.14285714,\n", 554 | " 9.28571429, 9.42857143, 9.57142857, 9.71428571,\n", 555 | " 9.85714286, 10. ])" 556 | ] 557 | }, 558 | "execution_count": 86, 559 | "metadata": {}, 560 | "output_type": "execute_result" 561 | } 562 | ], 563 | "source": [] 564 | }, 565 | { 566 | "cell_type": "markdown", 567 | "metadata": {}, 568 | "source": [ 569 | "Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive." 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "execution_count": 88, 575 | "metadata": { 576 | "collapsed": false 577 | }, 578 | "outputs": [ 579 | { 580 | "data": { 581 | "text/plain": [ 582 | "array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,\n", 583 | " 2.63026799e+03, 3.63078055e+03, 5.01187234e+03,\n", 584 | " 6.91830971e+03, 9.54992586e+03, 1.31825674e+04,\n", 585 | " 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n", 586 | " 4.78630092e+04, 6.60693448e+04, 9.12010839e+04,\n", 587 | " 1.25892541e+05, 1.73780083e+05, 2.39883292e+05,\n", 588 | " 3.31131121e+05, 4.57088190e+05, 6.30957344e+05,\n", 589 | " 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n", 590 | " 2.29086765e+06, 3.16227766e+06, 4.36515832e+06,\n", 591 | " 6.02559586e+06, 8.31763771e+06, 1.14815362e+07,\n", 592 | " 1.58489319e+07, 2.18776162e+07, 3.01995172e+07,\n", 593 | " 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n", 594 | " 1.09647820e+08, 1.51356125e+08, 2.08929613e+08,\n", 595 | " 2.88403150e+08, 3.98107171e+08, 5.49540874e+08,\n", 596 | " 7.58577575e+08, 1.04712855e+09, 1.44543977e+09,\n", 597 | " 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n", 598 | " 5.24807460e+09, 7.24435960e+09])" 599 | ] 600 | }, 601 | "execution_count": 88, 602 | "metadata": {}, 603 | "output_type": "execute_result" 604 | } 605 | ], 606 | "source": [] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "## Building matrices" 613 | ] 614 | }, 615 | { 616 | "cell_type": "markdown", 617 | "metadata": {}, 618 | "source": [ 619 | "Let X = np.array([[ 0, 1, 2, 3],\n", 620 | " [ 4, 5, 6, 7],\n", 621 | " [ 8, 9, 10, 11]]).\n", 622 | " Get the diagonal of X, that is, [0, 5, 10]." 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 93, 628 | "metadata": { 629 | "collapsed": false 630 | }, 631 | "outputs": [ 632 | { 633 | "data": { 634 | "text/plain": [ 635 | "array([ 0, 5, 10])" 636 | ] 637 | }, 638 | "execution_count": 93, 639 | "metadata": {}, 640 | "output_type": "execute_result" 641 | } 642 | ], 643 | "source": [ 644 | "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n" 645 | ] 646 | }, 647 | { 648 | "cell_type": "markdown", 649 | "metadata": {}, 650 | "source": [ 651 | "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere." 652 | ] 653 | }, 654 | { 655 | "cell_type": "code", 656 | "execution_count": 95, 657 | "metadata": { 658 | "collapsed": false 659 | }, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": [ 664 | "array([[1, 0, 0, 0],\n", 665 | " [0, 2, 0, 0],\n", 666 | " [0, 0, 3, 0],\n", 667 | " [0, 0, 0, 4]])" 668 | ] 669 | }, 670 | "execution_count": 95, 671 | "metadata": {}, 672 | "output_type": "execute_result" 673 | } 674 | ], 675 | "source": [] 676 | }, 677 | { 678 | "cell_type": "markdown", 679 | "metadata": {}, 680 | "source": [ 681 | "Create an array which looks like below.\n", 682 | "array([[ 0., 0., 0., 0., 0.],\n", 683 | " [ 1., 0., 0., 0., 0.],\n", 684 | " [ 1., 1., 0., 0., 0.]])" 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 97, 690 | "metadata": { 691 | "collapsed": false 692 | }, 693 | "outputs": [ 694 | { 695 | "data": { 696 | "text/plain": [ 697 | "array([[ 0., 0., 0., 0., 0.],\n", 698 | " [ 1., 0., 0., 0., 0.],\n", 699 | " [ 1., 1., 0., 0., 0.]])" 700 | ] 701 | }, 702 | "execution_count": 97, 703 | "metadata": {}, 704 | "output_type": "execute_result" 705 | } 706 | ], 707 | "source": [] 708 | }, 709 | { 710 | "cell_type": "markdown", 711 | "metadata": {}, 712 | "source": [ 713 | "Create an array which looks like below.\n", 714 | "array([[ 0, 0, 0],\n", 715 | " [ 4, 0, 0],\n", 716 | " [ 7, 8, 0],\n", 717 | " [10, 11, 12]])" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 101, 723 | "metadata": { 724 | "collapsed": false 725 | }, 726 | "outputs": [ 727 | { 728 | "data": { 729 | "text/plain": [ 730 | "array([[ 0, 0, 0],\n", 731 | " [ 4, 0, 0],\n", 732 | " [ 7, 8, 0],\n", 733 | " [10, 11, 12]])" 734 | ] 735 | }, 736 | "execution_count": 101, 737 | "metadata": {}, 738 | "output_type": "execute_result" 739 | } 740 | ], 741 | "source": [] 742 | }, 743 | { 744 | "cell_type": "markdown", 745 | "metadata": {}, 746 | "source": [ 747 | "Create an array which looks like below. array([[ 1, 2, 3],\n", 748 | " [ 4, 5, 6],\n", 749 | " [ 0, 8, 9],\n", 750 | " [ 0, 0, 12]])" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 102, 756 | "metadata": { 757 | "collapsed": false 758 | }, 759 | "outputs": [ 760 | { 761 | "data": { 762 | "text/plain": [ 763 | "array([[ 1, 2, 3],\n", 764 | " [ 4, 5, 6],\n", 765 | " [ 0, 8, 9],\n", 766 | " [ 0, 0, 12]])" 767 | ] 768 | }, 769 | "execution_count": 102, 770 | "metadata": {}, 771 | "output_type": "execute_result" 772 | } 773 | ], 774 | "source": [] 775 | } 776 | ], 777 | "metadata": { 778 | "kernelspec": { 779 | "display_name": "Python 2", 780 | "language": "python", 781 | "name": "python2" 782 | }, 783 | "language_info": { 784 | "codemirror_mode": { 785 | "name": "ipython", 786 | "version": 2 787 | }, 788 | "file_extension": ".py", 789 | "mimetype": "text/x-python", 790 | "name": "python", 791 | "nbconvert_exporter": "python", 792 | "pygments_lexer": "ipython2", 793 | "version": "2.7.6" 794 | } 795 | }, 796 | "nbformat": 4, 797 | "nbformat_minor": 0 798 | } 799 | -------------------------------------------------------------------------------- /2_Array_manipulation_routines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Array manipulation routines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": { 46 | "collapsed": true 47 | }, 48 | "source": [ 49 | "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 5, 55 | "metadata": { 56 | "collapsed": false 57 | }, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 64 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 65 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 66 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 67 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 68 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 69 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 70 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 71 | " 1. 1. 1. 1. 1. 1.]\n", 72 | " [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 73 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 74 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 75 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 76 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 77 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 78 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 79 | " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", 80 | " 1. 1. 1. 1. 1. 1.]]\n" 81 | ] 82 | } 83 | ], 84 | "source": [] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6]." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 22, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "[1 4 2 5 3 6]\n" 105 | ] 106 | } 107 | ], 108 | "source": [] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element." 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 23, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "5\n" 129 | ] 130 | } 131 | ], 132 | "source": [] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).\n" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 36, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "(4L, 3L, 5L)\n" 153 | ] 154 | } 155 | ], 156 | "source": [] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3)." 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 38, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "(4L, 3L)\n" 177 | ] 178 | } 179 | ], 180 | "source": [] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4)." 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 42, 192 | "metadata": { 193 | "collapsed": false 194 | }, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "(3L, 1L, 4L)\n" 201 | ] 202 | } 203 | ], 204 | "source": [] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4)." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 43, 216 | "metadata": { 217 | "collapsed": false 218 | }, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "(3L, 4L)\n" 225 | ] 226 | } 227 | ], 228 | "source": [] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "Q7. Lex x be an array
\n", 235 | "[[ 1 2 3]
\n", 236 | "[ 4 5 6].

\n", 237 | "and y be an array
\n", 238 | "[[ 7 8 9]
\n", 239 | "[10 11 12]].
\n", 240 | "Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].\n" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 31, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "[[ 1 2 3 7 8 9]\n", 255 | " [ 4 5 6 10 11 12]]\n" 256 | ] 257 | } 258 | ], 259 | "source": [] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "Q8. Lex x be an array
\n", 266 | "[[ 1 2 3]
\n", 267 | "[ 4 5 6].

\n", 268 | "and y be an array
\n", 269 | "[[ 7 8 9]
\n", 270 | "[10 11 12]].
\n", 271 | "Concatenate x and y so that a new array looks like
[[ 1 2 3]
\n", 272 | " [ 4 5 6]
\n", 273 | " [ 7 8 9]
\n", 274 | " [10 11 12]]\n" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 38, 280 | "metadata": { 281 | "collapsed": false 282 | }, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "[[ 1 2 3]\n", 289 | " [ 4 5 6]\n", 290 | " [ 7 8 9]\n", 291 | " [10 11 12]]\n" 292 | ] 293 | } 294 | ], 295 | "source": [] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]]." 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 54, 307 | "metadata": { 308 | "collapsed": false 309 | }, 310 | "outputs": [ 311 | { 312 | "name": "stdout", 313 | "output_type": "stream", 314 | "text": [ 315 | "[[1 4]\n", 316 | " [2 5]\n", 317 | " [3 6]]\n" 318 | ] 319 | } 320 | ], 321 | "source": [] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]]." 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 34, 333 | "metadata": { 334 | "collapsed": false 335 | }, 336 | "outputs": [ 337 | { 338 | "name": "stdout", 339 | "output_type": "stream", 340 | "text": [ 341 | "[[[1 4]]\n", 342 | "\n", 343 | " [[2 5]]\n", 344 | "\n", 345 | " [[3 6]]]\n" 346 | ] 347 | } 348 | ], 349 | "source": [] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order." 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 62, 361 | "metadata": { 362 | "collapsed": false 363 | }, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]\n" 370 | ] 371 | } 372 | ], 373 | "source": [] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "Q11. Let x be an array
\n", 380 | "[[[ 0., 1., 2., 3.],
\n", 381 | " [ 4., 5., 6., 7.]],
\n", 382 | " \n", 383 | " [[ 8., 9., 10., 11.],
\n", 384 | " [ 12., 13., 14., 15.]]].
\n", 385 | "Split it into two such that the first array looks like
\n", 386 | "[[[ 0., 1., 2.],
\n", 387 | " [ 4., 5., 6.]],
\n", 388 | " \n", 389 | " [[ 8., 9., 10.],
\n", 390 | " [ 12., 13., 14.]]].
\n", 391 | " \n", 392 | "and the second one look like:
\n", 393 | " \n", 394 | "[[[ 3.],
\n", 395 | " [ 7.]],
\n", 396 | " \n", 397 | " [[ 11.],
\n", 398 | " [ 15.]]].
" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 72, 404 | "metadata": { 405 | "collapsed": false 406 | }, 407 | "outputs": [ 408 | { 409 | "name": "stdout", 410 | "output_type": "stream", 411 | "text": [ 412 | "[array([[[ 0, 1, 2],\n", 413 | " [ 4, 5, 6]],\n", 414 | "\n", 415 | " [[ 8, 9, 10],\n", 416 | " [12, 13, 14]]]), array([[[ 3],\n", 417 | " [ 7]],\n", 418 | "\n", 419 | " [[11],\n", 420 | " [15]]])]\n" 421 | ] 422 | } 423 | ], 424 | "source": [] 425 | }, 426 | { 427 | "cell_type": "markdown", 428 | "metadata": {}, 429 | "source": [ 430 | "Q12. Let x be an array
\n", 431 | "[[ 0., 1., 2., 3.],
\n", 432 | " [ 4., 5., 6., 7.],
\n", 433 | " [ 8., 9., 10., 11.],
\n", 434 | " [ 12., 13., 14., 15.]].
\n", 435 | "Split it into two arrays along the second axis." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 74, 441 | "metadata": { 442 | "collapsed": false 443 | }, 444 | "outputs": [ 445 | { 446 | "name": "stdout", 447 | "output_type": "stream", 448 | "text": [ 449 | "[array([[ 0, 1],\n", 450 | " [ 4, 5],\n", 451 | " [ 8, 9],\n", 452 | " [12, 13]]), array([[ 2, 3],\n", 453 | " [ 6, 7],\n", 454 | " [10, 11],\n", 455 | " [14, 15]])]\n" 456 | ] 457 | } 458 | ], 459 | "source": [] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": {}, 464 | "source": [ 465 | "Q13. Let x be an array
\n", 466 | "[[ 0., 1., 2., 3.],
\n", 467 | " [ 4., 5., 6., 7.],
\n", 468 | " [ 8., 9., 10., 11.],
\n", 469 | " [ 12., 13., 14., 15.]].
\n", 470 | "Split it into two arrays along the first axis." 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 75, 476 | "metadata": { 477 | "collapsed": false 478 | }, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "[array([[0, 1, 2, 3],\n", 485 | " [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],\n", 486 | " [12, 13, 14, 15]])]\n" 487 | ] 488 | } 489 | ], 490 | "source": [] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "metadata": {}, 495 | "source": [ 496 | "Q14. Let x be an array [0, 1, 2]. Convert it to
\n", 497 | "[[0, 1, 2, 0, 1, 2],
\n", 498 | " [0, 1, 2, 0, 1, 2]]." 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 93, 504 | "metadata": { 505 | "collapsed": false 506 | }, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "[[0 1 2 0 1 2]\n", 513 | " [0 1 2 0 1 2]]\n" 514 | ] 515 | } 516 | ], 517 | "source": [] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "Q15. Let x be an array [0, 1, 2]. Convert it to
\n", 524 | "[0, 0, 1, 1, 2, 2]." 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 83, 530 | "metadata": { 531 | "collapsed": false 532 | }, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "[0 0 1 1 2 2]\n" 539 | ] 540 | } 541 | ], 542 | "source": [] 543 | }, 544 | { 545 | "cell_type": "markdown", 546 | "metadata": {}, 547 | "source": [ 548 | "Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
\n", 549 | "remove the leading the trailing zeros." 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 105, 555 | "metadata": { 556 | "collapsed": false 557 | }, 558 | "outputs": [ 559 | { 560 | "name": "stdout", 561 | "output_type": "stream", 562 | "text": [ 563 | "[1 2 3 0 2 1]\n" 564 | ] 565 | } 566 | ], 567 | "source": [] 568 | }, 569 | { 570 | "cell_type": "markdown", 571 | "metadata": {}, 572 | "source": [ 573 | "Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.\n" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": 107, 579 | "metadata": { 580 | "collapsed": false 581 | }, 582 | "outputs": [ 583 | { 584 | "name": "stdout", 585 | "output_type": "stream", 586 | "text": [ 587 | "[1 2 3 4 5] [2 3 1 1 2]\n" 588 | ] 589 | } 590 | ], 591 | "source": [] 592 | }, 593 | { 594 | "cell_type": "markdown", 595 | "metadata": {}, 596 | "source": [ 597 | "Q18. Lex x be an array
\n", 598 | "[[ 1 2]
\n", 599 | " [ 3 4].
\n", 600 | "Flip x along the second axis." 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 120, 606 | "metadata": { 607 | "collapsed": false 608 | }, 609 | "outputs": [ 610 | { 611 | "name": "stdout", 612 | "output_type": "stream", 613 | "text": [ 614 | "[[2 1]\n", 615 | " [4 3]]\n" 616 | ] 617 | } 618 | ], 619 | "source": [] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "Q19. Lex x be an array
\n", 626 | "[[ 1 2]
\n", 627 | " [ 3 4].
\n", 628 | "Flip x along the first axis." 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 121, 634 | "metadata": { 635 | "collapsed": false 636 | }, 637 | "outputs": [ 638 | { 639 | "name": "stdout", 640 | "output_type": "stream", 641 | "text": [ 642 | "[[3 4]\n", 643 | " [1 2]]\n" 644 | ] 645 | } 646 | ], 647 | "source": [] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": {}, 652 | "source": [ 653 | "Q20. Lex x be an array
\n", 654 | "[[ 1 2]
\n", 655 | " [ 3 4].
\n", 656 | "Rotate x 90 degrees counter-clockwise." 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 122, 662 | "metadata": { 663 | "collapsed": false 664 | }, 665 | "outputs": [ 666 | { 667 | "name": "stdout", 668 | "output_type": "stream", 669 | "text": [ 670 | "[[2 4]\n", 671 | " [1 3]]\n" 672 | ] 673 | } 674 | ], 675 | "source": [] 676 | }, 677 | { 678 | "cell_type": "markdown", 679 | "metadata": {}, 680 | "source": [ 681 | "Q21 Lex x be an array
\n", 682 | "[[ 1 2 3 4]
\n", 683 | " [ 5 6 7 8].
\n", 684 | "Shift elements one step to right along the second axis." 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 126, 690 | "metadata": { 691 | "collapsed": false 692 | }, 693 | "outputs": [ 694 | { 695 | "name": "stdout", 696 | "output_type": "stream", 697 | "text": [ 698 | "[[4 1 2 3]\n", 699 | " [8 5 6 7]]\n" 700 | ] 701 | } 702 | ], 703 | "source": [] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": null, 708 | "metadata": { 709 | "collapsed": true 710 | }, 711 | "outputs": [], 712 | "source": [] 713 | } 714 | ], 715 | "metadata": { 716 | "kernelspec": { 717 | "display_name": "Python 2", 718 | "language": "python", 719 | "name": "python2" 720 | }, 721 | "language_info": { 722 | "codemirror_mode": { 723 | "name": "ipython", 724 | "version": 2 725 | }, 726 | "file_extension": ".py", 727 | "mimetype": "text/x-python", 728 | "name": "python", 729 | "nbconvert_exporter": "python", 730 | "pygments_lexer": "ipython2", 731 | "version": "2.7.10" 732 | } 733 | }, 734 | "nbformat": 4, 735 | "nbformat_minor": 0 736 | } 737 | -------------------------------------------------------------------------------- /3_String_operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## String operations" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "from __future__ import print_function\n", 19 | "import numpy as np" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\"" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "'1.11.3'" 44 | ] 45 | }, 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "np.__version__" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "Q1. Concatenate x1 and x2." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": { 66 | "collapsed": false 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "['Hello world' 'Say something']\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "x1 = np.array(['Hello', 'Say'], dtype=np.str)\n", 79 | "x2 = np.array([' world', ' something'], dtype=np.str)\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Q2. Repeat x three time element-wise." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "['Hello Hello Hello ' 'Say Say Say ']\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "x = np.array(['Hello ', 'Say '], dtype=np.str)\n" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "Q3-1. Capitalize the first letter of x element-wise.
\n", 113 | "Q3-2. Lowercase x element-wise.
\n", 114 | "Q3-3. Uppercase x element-wise.
\n", 115 | "Q3-4. Swapcase x element-wise.
\n", 116 | "Q3-5. Title-case x element-wise.
" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "capitalized = ['Hello world' 'Say something']\n", 131 | "lowered = ['hello world' 'say something']\n", 132 | "uppered = ['HELLO WORLD' 'SAY SOMETHING']\n", 133 | "swapcased = ['HEllO WOrlD' 'sAY SoMETHING']\n", 134 | "titlecased = ['Hello World' 'Say Something']\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)\n", 140 | "capitalized = ...\n", 141 | "lowered = ...\n", 142 | "uppered = ...\n", 143 | "swapcased = ...\n", 144 | "titlecased = ...\n", 145 | "print(\"capitalized =\", capitalized)\n", 146 | "print(\"lowered =\", lowered)\n", 147 | "print(\"uppered =\", uppered)\n", 148 | "print(\"swapcased =\", swapcased)\n", 149 | "print(\"titlecased =\", titlecased)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Q4. Make the length of each element 20 and the string centered / left-justified / right-justified with paddings of `_`." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 7, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "centered = ['____hello world_____' '___say something____']\n", 171 | "left = ['hello world_________' 'say something_______']\n", 172 | "right = ['_________hello world' '_______say something']\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n", 178 | "centered = ...\n", 179 | "left = ...\n", 180 | "right = ...\n", 181 | "\n", 182 | "print(\"centered =\", centered)\n", 183 | "print(\"left =\", left)\n", 184 | "print(\"right =\", right)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Q5. Encode x in cp500 and decode it again." 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 8, 197 | "metadata": { 198 | "collapsed": false 199 | }, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "encoded = [b'\\x88\\x85\\x93\\x93\\x96@\\xa6\\x96\\x99\\x93\\x84'\n", 206 | " b'\\xa2\\x81\\xa8@\\xa2\\x96\\x94\\x85\\xa3\\x88\\x89\\x95\\x87']\n", 207 | "decoded = ['hello world' 'say something']\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n", 213 | "encoded = ...\n", 214 | "decoded = ...\n", 215 | "print(\"encoded =\", encoded)\n", 216 | "print(\"decoded =\", decoded)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "Q6. Insert a space between characters of x." 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 9, 229 | "metadata": { 230 | "collapsed": false 231 | }, 232 | "outputs": [ 233 | { 234 | "name": "stdout", 235 | "output_type": "stream", 236 | "text": [ 237 | "['h e l l o w o r l d' 's a y s o m e t h i n g']\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "Q7-1. Remove the leading and trailing whitespaces of x element-wise.
\n", 250 | "Q7-2. Remove the leading whitespaces of x element-wise.
\n", 251 | "Q7-3. Remove the trailing whitespaces of x element-wise." 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 10, 257 | "metadata": { 258 | "collapsed": false 259 | }, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "stripped = ['hello world' 'say something']\n", 266 | "lstripped = ['hello world ' 'say something\\n']\n", 267 | "rstripped = [' hello world' '\\tsay something']\n" 268 | ] 269 | } 270 | ], 271 | "source": [ 272 | "x = np.array([' hello world ', '\\tsay something\\n'], dtype=np.str)\n", 273 | "stripped = ...\n", 274 | "lstripped = ...\n", 275 | "rstripped = ...\n", 276 | "print(\"stripped =\", stripped)\n", 277 | "print(\"lstripped =\", lstripped)\n", 278 | "print(\"rstripped =\", rstripped)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "Q8. Split the element of x with spaces." 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 11, 291 | "metadata": { 292 | "collapsed": false 293 | }, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "[['Hello', 'my', 'name', 'is', 'John']]\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "x = np.array(['Hello my name is John'], dtype=np.str)\n" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "Q9. Split the element of x to multiple lines." 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 12, 317 | "metadata": { 318 | "collapsed": false 319 | }, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "[['Hello', 'my name is John']]\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "x = np.array(['Hello\\nmy name is John'], dtype=np.str)\n" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "Q10. Make x a numeric string of 4 digits with zeros on its left." 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 13, 343 | "metadata": { 344 | "collapsed": false 345 | }, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "['0034']\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "x = np.array(['34'], dtype=np.str)\n" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": {}, 362 | "source": [ 363 | "Q11. Replace \"John\" with \"Jim\" in x." 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 14, 369 | "metadata": { 370 | "collapsed": false 371 | }, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "['Hello nmy name is Jim']\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "x = np.array(['Hello nmy name is John'], dtype=np.str)\n" 383 | ] 384 | }, 385 | { 386 | "cell_type": "markdown", 387 | "metadata": {}, 388 | "source": [ 389 | "## Comparison" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "Q12. Return x1 == x2, element-wise." 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 15, 402 | "metadata": { 403 | "collapsed": false 404 | }, 405 | "outputs": [ 406 | { 407 | "name": "stdout", 408 | "output_type": "stream", 409 | "text": [ 410 | "[ True True True True False]\n" 411 | ] 412 | } 413 | ], 414 | "source": [ 415 | "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n", 416 | "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "Q13. Return x1 != x2, element-wise." 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 16, 429 | "metadata": { 430 | "collapsed": false 431 | }, 432 | "outputs": [ 433 | { 434 | "name": "stdout", 435 | "output_type": "stream", 436 | "text": [ 437 | "[False False False False True]\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n", 443 | "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "## String information" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "Q14. Count the number of \"l\" in x, element-wise." 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 17, 463 | "metadata": { 464 | "collapsed": false 465 | }, 466 | "outputs": [ 467 | { 468 | "name": "stdout", 469 | "output_type": "stream", 470 | "text": [ 471 | "[2 0 0 0 1]\n" 472 | ] 473 | } 474 | ], 475 | "source": [ 476 | "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "Q15. Count the lowest index of \"l\" in x, element-wise." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 18, 489 | "metadata": { 490 | "collapsed": false 491 | }, 492 | "outputs": [ 493 | { 494 | "name": "stdout", 495 | "output_type": "stream", 496 | "text": [ 497 | "[ 2 -1 -1 -1 2]\n" 498 | ] 499 | } 500 | ], 501 | "source": [ 502 | "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n", 503 | "\n", 504 | "\n" 505 | ] 506 | }, 507 | { 508 | "cell_type": "markdown", 509 | "metadata": {}, 510 | "source": [ 511 | "Q16-1. Check if each element of x is composed of digits only.
\n", 512 | "Q16-2. Check if each element of x is composed of lower case letters only.
\n", 513 | "Q16-3. Check if each element of x is composed of upper case letters only." 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 19, 519 | "metadata": { 520 | "collapsed": false 521 | }, 522 | "outputs": [ 523 | { 524 | "name": "stdout", 525 | "output_type": "stream", 526 | "text": [ 527 | "Digits only = [False False False True False False]\n", 528 | "Lower cases only = [False False True False True True]\n", 529 | "Upper cases only = [False True False False False False]\n" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=np.str)\n", 535 | "out1 = ...\n", 536 | "out2 = ...\n", 537 | "out3 = ...\n", 538 | "print(\"Digits only =\", out1)\n", 539 | "print(\"Lower cases only =\", out2)\n", 540 | "print(\"Upper cases only =\", out3)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "Q17. Check if each element of x starts with \"hi\"." 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 20, 553 | "metadata": { 554 | "collapsed": false 555 | }, 556 | "outputs": [ 557 | { 558 | "name": "stdout", 559 | "output_type": "stream", 560 | "text": [ 561 | "[False True True True]\n" 562 | ] 563 | } 564 | ], 565 | "source": [ 566 | "x = np.array(['he', 'his', 'him', 'his'], dtype=np.str)\n" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": null, 572 | "metadata": { 573 | "collapsed": true 574 | }, 575 | "outputs": [], 576 | "source": [] 577 | } 578 | ], 579 | "metadata": { 580 | "anaconda-cloud": {}, 581 | "kernelspec": { 582 | "display_name": "Python [conda root]", 583 | "language": "python", 584 | "name": "conda-root-py" 585 | }, 586 | "language_info": { 587 | "codemirror_mode": { 588 | "name": "ipython", 589 | "version": 3 590 | }, 591 | "file_extension": ".py", 592 | "mimetype": "text/x-python", 593 | "name": "python", 594 | "nbconvert_exporter": "python", 595 | "pygments_lexer": "ipython3", 596 | "version": "3.5.2" 597 | } 598 | }, 599 | "nbformat": 4, 600 | "nbformat_minor": 1 601 | } 602 | -------------------------------------------------------------------------------- /3_String_operations_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## String operations" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "from __future__ import print_function\n", 19 | "import numpy as np" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\"" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "'1.11.3'" 44 | ] 45 | }, 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "np.__version__" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "Q1. Concatenate x1 and x2." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": { 66 | "collapsed": false 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "['Hello world' 'Say something']\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "x1 = np.array(['Hello', 'Say'], dtype=np.str)\n", 79 | "x2 = np.array([' world', ' something'], dtype=np.str)\n", 80 | "out = np.char.add(x1, x2)\n", 81 | "print(out)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "Q2. Repeat x three time element-wise." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "['Hello Hello Hello ' 'Say Say Say ']\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "x = np.array(['Hello ', 'Say '], dtype=np.str)\n", 108 | "out = np.char.multiply(x, 3)\n", 109 | "print(out)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Q3-1. Capitalize the first letter of x element-wise.
\n", 117 | "Q3-2. Lowercase x element-wise.
\n", 118 | "Q3-3. Uppercase x element-wise.
\n", 119 | "Q3-4. Swapcase x element-wise.
\n", 120 | "Q3-5. Title-case x element-wise.
" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "capitalized = ['Hello world' 'Say something']\n", 135 | "lowered = ['hello world' 'say something']\n", 136 | "uppered = ['HELLO WORLD' 'SAY SOMETHING']\n", 137 | "swapcased = ['HEllO WOrlD' 'sAY SoMETHING']\n", 138 | "titlecased = ['Hello World' 'Say Something']\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)\n", 144 | "capitalized = np.char.capitalize(x)\n", 145 | "lowered = np.char.lower(x)\n", 146 | "uppered = np.char.upper(x)\n", 147 | "swapcased = np.char.swapcase(x)\n", 148 | "titlecased = np.char.title(x)\n", 149 | "print(\"capitalized =\", capitalized)\n", 150 | "print(\"lowered =\", lowered)\n", 151 | "print(\"uppered =\", uppered)\n", 152 | "print(\"swapcased =\", swapcased)\n", 153 | "print(\"titlecased =\", titlecased)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Q4. Make the length of each element 20 and the string centered / left-justified / right-justified with paddings of `_`." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 7, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "centered = ['____hello world_____' '___say something____']\n", 175 | "left = ['hello world_________' 'say something_______']\n", 176 | "right = ['_________hello world' '_______say something']\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n", 182 | "centered = np.char.center(x, 20, fillchar='_')\n", 183 | "left = np.char.ljust(x, 20, fillchar='_')\n", 184 | "right = np.char.rjust(x, 20, fillchar='_')\n", 185 | "\n", 186 | "print(\"centered =\", centered)\n", 187 | "print(\"left =\", left)\n", 188 | "print(\"right =\", right)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "Q5. Encode x in cp500 and decode it again." 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 8, 201 | "metadata": { 202 | "collapsed": false 203 | }, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "encoded = [b'\\x88\\x85\\x93\\x93\\x96@\\xa6\\x96\\x99\\x93\\x84'\n", 210 | " b'\\xa2\\x81\\xa8@\\xa2\\x96\\x94\\x85\\xa3\\x88\\x89\\x95\\x87']\n", 211 | "decoded = ['hello world' 'say something']\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n", 217 | "encoded = np.char.encode(x, 'cp500')\n", 218 | "decoded = np.char.decode(encoded,'cp500')\n", 219 | "print(\"encoded =\", encoded)\n", 220 | "print(\"decoded =\", decoded)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "Q6. Insert a space between characters of x." 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "metadata": { 234 | "collapsed": false 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "['h e l l o w o r l d' 's a y s o m e t h i n g']\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "x = np.array(['hello world', 'say something'], dtype=np.str)\n", 247 | "out = np.char.join(\" \", x)\n", 248 | "print(out)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "Q7-1. Remove the leading and trailing whitespaces of x element-wise.
\n", 256 | "Q7-2. Remove the leading whitespaces of x element-wise.
\n", 257 | "Q7-3. Remove the trailing whitespaces of x element-wise." 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 10, 263 | "metadata": { 264 | "collapsed": false 265 | }, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "stripped = ['hello world' 'say something']\n", 272 | "lstripped = ['hello world ' 'say something\\n']\n", 273 | "rstripped = [' hello world' '\\tsay something']\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "x = np.array([' hello world ', '\\tsay something\\n'], dtype=np.str)\n", 279 | "stripped = np.char.strip(x)\n", 280 | "lstripped = np.char.lstrip(x)\n", 281 | "rstripped = np.char.rstrip(x)\n", 282 | "print(\"stripped =\", stripped)\n", 283 | "print(\"lstripped =\", lstripped)\n", 284 | "print(\"rstripped =\", rstripped)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "Q8. Split the element of x with spaces." 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 11, 297 | "metadata": { 298 | "collapsed": false 299 | }, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "[['Hello', 'my', 'name', 'is', 'John']]\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "x = np.array(['Hello my name is John'], dtype=np.str)\n", 311 | "out = np.char.split(x)\n", 312 | "print(out)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "Q9. Split the element of x to multiple lines." 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 12, 325 | "metadata": { 326 | "collapsed": false 327 | }, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "[['Hello', 'my name is John']]\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "x = np.array(['Hello\\nmy name is John'], dtype=np.str)\n", 339 | "out = np.char.splitlines(x)\n", 340 | "print(out)" 341 | ] 342 | }, 343 | { 344 | "cell_type": "markdown", 345 | "metadata": {}, 346 | "source": [ 347 | "Q10. Make x a numeric string of 4 digits with zeros on its left." 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 13, 353 | "metadata": { 354 | "collapsed": false 355 | }, 356 | "outputs": [ 357 | { 358 | "name": "stdout", 359 | "output_type": "stream", 360 | "text": [ 361 | "['0034']\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "x = np.array(['34'], dtype=np.str)\n", 367 | "out = np.char.zfill(x, 4)\n", 368 | "print(out)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "Q11. Replace \"John\" with \"Jim\" in x." 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 14, 381 | "metadata": { 382 | "collapsed": false 383 | }, 384 | "outputs": [ 385 | { 386 | "name": "stdout", 387 | "output_type": "stream", 388 | "text": [ 389 | "['Hello nmy name is Jim']\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "x = np.array(['Hello nmy name is John'], dtype=np.str)\n", 395 | "out = np.char.replace(x, \"John\", \"Jim\")\n", 396 | "print(out)" 397 | ] 398 | }, 399 | { 400 | "cell_type": "markdown", 401 | "metadata": {}, 402 | "source": [ 403 | "## Comparison" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": {}, 409 | "source": [ 410 | "Q12. Return x1 == x2, element-wise." 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 15, 416 | "metadata": { 417 | "collapsed": false 418 | }, 419 | "outputs": [ 420 | { 421 | "name": "stdout", 422 | "output_type": "stream", 423 | "text": [ 424 | "[ True True True True False]\n" 425 | ] 426 | } 427 | ], 428 | "source": [ 429 | "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n", 430 | "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n", 431 | "out = np.char.equal(x1, x2)\n", 432 | "print(out)" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "Q13. Return x1 != x2, element-wise." 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 16, 445 | "metadata": { 446 | "collapsed": false 447 | }, 448 | "outputs": [ 449 | { 450 | "name": "stdout", 451 | "output_type": "stream", 452 | "text": [ 453 | "[False False False False True]\n" 454 | ] 455 | } 456 | ], 457 | "source": [ 458 | "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n", 459 | "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n", 460 | "out = np.char.not_equal(x1, x2)\n", 461 | "print(out)" 462 | ] 463 | }, 464 | { 465 | "cell_type": "markdown", 466 | "metadata": {}, 467 | "source": [ 468 | "## String information" 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "metadata": {}, 474 | "source": [ 475 | "Q14. Count the number of \"l\" in x, element-wise." 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 17, 481 | "metadata": { 482 | "collapsed": false 483 | }, 484 | "outputs": [ 485 | { 486 | "name": "stdout", 487 | "output_type": "stream", 488 | "text": [ 489 | "[2 0 0 0 1]\n" 490 | ] 491 | } 492 | ], 493 | "source": [ 494 | "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n", 495 | "out = np.char.count(x, \"l\")\n", 496 | "print(out)" 497 | ] 498 | }, 499 | { 500 | "cell_type": "markdown", 501 | "metadata": {}, 502 | "source": [ 503 | "Q15. Count the lowest index of \"l\" in x, element-wise." 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 18, 509 | "metadata": { 510 | "collapsed": false 511 | }, 512 | "outputs": [ 513 | { 514 | "name": "stdout", 515 | "output_type": "stream", 516 | "text": [ 517 | "[ 2 -1 -1 -1 2]\n" 518 | ] 519 | } 520 | ], 521 | "source": [ 522 | "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n", 523 | "out = np.char.find(x, \"l\")\n", 524 | "print(out)\n", 525 | "\n", 526 | "# compare\n", 527 | "# print(np.char.index(x, \"l\"))\n", 528 | "# => This raises an error!" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": {}, 534 | "source": [ 535 | "Q16-1. Check if each element of x is composed of digits only.
\n", 536 | "Q16-2. Check if each element of x is composed of lower case letters only.
\n", 537 | "Q16-3. Check if each element of x is composed of upper case letters only." 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 19, 543 | "metadata": { 544 | "collapsed": false 545 | }, 546 | "outputs": [ 547 | { 548 | "name": "stdout", 549 | "output_type": "stream", 550 | "text": [ 551 | "Digits only = [False False False True False False]\n", 552 | "Lower cases only = [False False True False True True]\n", 553 | "Upper cases only = [False True False False False False]\n" 554 | ] 555 | } 556 | ], 557 | "source": [ 558 | "x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=np.str)\n", 559 | "out1 = np.char.isdigit(x)\n", 560 | "out2 = np.char.islower(x)\n", 561 | "out3 = np.char.isupper(x)\n", 562 | "print(\"Digits only =\", out1)\n", 563 | "print(\"Lower cases only =\", out2)\n", 564 | "print(\"Upper cases only =\", out3)" 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "metadata": {}, 570 | "source": [ 571 | "Q17. Check if each element of x starts with \"hi\"." 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 20, 577 | "metadata": { 578 | "collapsed": false 579 | }, 580 | "outputs": [ 581 | { 582 | "name": "stdout", 583 | "output_type": "stream", 584 | "text": [ 585 | "[False True True True]\n" 586 | ] 587 | } 588 | ], 589 | "source": [ 590 | "x = np.array(['he', 'his', 'him', 'his'], dtype=np.str)\n", 591 | "out = np.char.startswith(x, \"hi\")\n", 592 | "print(out)" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "metadata": { 599 | "collapsed": true 600 | }, 601 | "outputs": [], 602 | "source": [] 603 | } 604 | ], 605 | "metadata": { 606 | "anaconda-cloud": {}, 607 | "kernelspec": { 608 | "display_name": "Python [conda root]", 609 | "language": "python", 610 | "name": "conda-root-py" 611 | }, 612 | "language_info": { 613 | "codemirror_mode": { 614 | "name": "ipython", 615 | "version": 3 616 | }, 617 | "file_extension": ".py", 618 | "mimetype": "text/x-python", 619 | "name": "python", 620 | "nbconvert_exporter": "python", 621 | "pygments_lexer": "ipython3", 622 | "version": "3.5.2" 623 | } 624 | }, 625 | "nbformat": 4, 626 | "nbformat_minor": 1 627 | } 628 | -------------------------------------------------------------------------------- /4_Numpy-specific_help_functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "'1.11.2'" 25 | ] 26 | }, 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "np.__version__" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "Q1. Search for docstrings of the numpy functions on linear algebra." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "Search results for 'linear algebra'\n", 55 | "-----------------------------------\n", 56 | "numpy.linalg.solve\n", 57 | " Solve a linear matrix equation, or system of linear scalar equations.\n", 58 | "numpy.poly\n", 59 | " Find the coefficients of a polynomial with the given sequence of roots.\n", 60 | "numpy.restoredot\n", 61 | " Restore `dot`, `vdot`, and `innerproduct` to the default non-BLAS\n", 62 | "numpy.linalg.eig\n", 63 | " Compute the eigenvalues and right eigenvectors of a square array.\n", 64 | "numpy.linalg.cond\n", 65 | " Compute the condition number of a matrix.\n", 66 | "numpy.linalg.eigh\n", 67 | " Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.\n", 68 | "numpy.linalg.pinv\n", 69 | " Compute the (Moore-Penrose) pseudo-inverse of a matrix.\n", 70 | "numpy.linalg.LinAlgError\n", 71 | " Generic Python-exception-derived object raised by linalg functions.\n" 72 | ] 73 | } 74 | ], 75 | "source": [] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "Q2. Get help information for numpy dot function." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 9, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "dot(a, b, out=None)\n", 96 | "\n", 97 | "Dot product of two arrays.\n", 98 | "\n", 99 | "For 2-D arrays it is equivalent to matrix multiplication, and for 1-D\n", 100 | "arrays to inner product of vectors (without complex conjugation). For\n", 101 | "N dimensions it is a sum product over the last axis of `a` and\n", 102 | "the second-to-last of `b`::\n", 103 | "\n", 104 | " dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])\n", 105 | "\n", 106 | "Parameters\n", 107 | "----------\n", 108 | "a : array_like\n", 109 | " First argument.\n", 110 | "b : array_like\n", 111 | " Second argument.\n", 112 | "out : ndarray, optional\n", 113 | " Output argument. This must have the exact kind that would be returned\n", 114 | " if it was not used. In particular, it must have the right type, must be\n", 115 | " C-contiguous, and its dtype must be the dtype that would be returned\n", 116 | " for `dot(a,b)`. This is a performance feature. Therefore, if these\n", 117 | " conditions are not met, an exception is raised, instead of attempting\n", 118 | " to be flexible.\n", 119 | "\n", 120 | "Returns\n", 121 | "-------\n", 122 | "output : ndarray\n", 123 | " Returns the dot product of `a` and `b`. If `a` and `b` are both\n", 124 | " scalars or both 1-D arrays then a scalar is returned; otherwise\n", 125 | " an array is returned.\n", 126 | " If `out` is given, then it is returned.\n", 127 | "\n", 128 | "Raises\n", 129 | "------\n", 130 | "ValueError\n", 131 | " If the last dimension of `a` is not the same size as\n", 132 | " the second-to-last dimension of `b`.\n", 133 | "\n", 134 | "See Also\n", 135 | "--------\n", 136 | "vdot : Complex-conjugating dot product.\n", 137 | "tensordot : Sum products over arbitrary axes.\n", 138 | "einsum : Einstein summation convention.\n", 139 | "matmul : '@' operator as method with out parameter.\n", 140 | "\n", 141 | "Examples\n", 142 | "--------\n", 143 | ">>> np.dot(3, 4)\n", 144 | "12\n", 145 | "\n", 146 | "Neither argument is complex-conjugated:\n", 147 | "\n", 148 | ">>> np.dot([2j, 3j], [2j, 3j])\n", 149 | "(-13+0j)\n", 150 | "\n", 151 | "For 2-D arrays it is the matrix product:\n", 152 | "\n", 153 | ">>> a = [[1, 0], [0, 1]]\n", 154 | ">>> b = [[4, 1], [2, 2]]\n", 155 | ">>> np.dot(a, b)\n", 156 | "array([[4, 1],\n", 157 | " [2, 2]])\n", 158 | "\n", 159 | ">>> a = np.arange(3*4*5*6).reshape((3,4,5,6))\n", 160 | ">>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))\n", 161 | ">>> np.dot(a, b)[2,3,2,1,2,2]\n", 162 | "499128\n", 163 | ">>> sum(a[2,3,2,:] * b[1,2,:,2])\n", 164 | "499128\n" 165 | ] 166 | } 167 | ], 168 | "source": [] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": { 174 | "collapsed": true 175 | }, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Python 2", 183 | "language": "python", 184 | "name": "python2" 185 | }, 186 | "language_info": { 187 | "codemirror_mode": { 188 | "name": "ipython", 189 | "version": 2 190 | }, 191 | "file_extension": ".py", 192 | "mimetype": "text/x-python", 193 | "name": "python", 194 | "nbconvert_exporter": "python", 195 | "pygments_lexer": "ipython2", 196 | "version": "2.7.10" 197 | } 198 | }, 199 | "nbformat": 4, 200 | "nbformat_minor": 0 201 | } 202 | -------------------------------------------------------------------------------- /4_Numpy-specific_help_functions_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "'1.11.2'" 25 | ] 26 | }, 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "np.__version__" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "Q1. Search for docstrings of the numpy functions on linear algebra." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "Search results for 'linear algebra'\n", 55 | "-----------------------------------\n", 56 | "numpy.linalg.solve\n", 57 | " Solve a linear matrix equation, or system of linear scalar equations.\n", 58 | "numpy.poly\n", 59 | " Find the coefficients of a polynomial with the given sequence of roots.\n", 60 | "numpy.restoredot\n", 61 | " Restore `dot`, `vdot`, and `innerproduct` to the default non-BLAS\n", 62 | "numpy.linalg.eig\n", 63 | " Compute the eigenvalues and right eigenvectors of a square array.\n", 64 | "numpy.linalg.cond\n", 65 | " Compute the condition number of a matrix.\n", 66 | "numpy.linalg.eigh\n", 67 | " Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.\n", 68 | "numpy.linalg.pinv\n", 69 | " Compute the (Moore-Penrose) pseudo-inverse of a matrix.\n", 70 | "numpy.linalg.LinAlgError\n", 71 | " Generic Python-exception-derived object raised by linalg functions.\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "np.lookfor('linear algebra')" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "Q2. Get help information for numpy dot function." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 9, 89 | "metadata": { 90 | "collapsed": false 91 | }, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "dot(a, b, out=None)\n", 98 | "\n", 99 | "Dot product of two arrays.\n", 100 | "\n", 101 | "For 2-D arrays it is equivalent to matrix multiplication, and for 1-D\n", 102 | "arrays to inner product of vectors (without complex conjugation). For\n", 103 | "N dimensions it is a sum product over the last axis of `a` and\n", 104 | "the second-to-last of `b`::\n", 105 | "\n", 106 | " dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])\n", 107 | "\n", 108 | "Parameters\n", 109 | "----------\n", 110 | "a : array_like\n", 111 | " First argument.\n", 112 | "b : array_like\n", 113 | " Second argument.\n", 114 | "out : ndarray, optional\n", 115 | " Output argument. This must have the exact kind that would be returned\n", 116 | " if it was not used. In particular, it must have the right type, must be\n", 117 | " C-contiguous, and its dtype must be the dtype that would be returned\n", 118 | " for `dot(a,b)`. This is a performance feature. Therefore, if these\n", 119 | " conditions are not met, an exception is raised, instead of attempting\n", 120 | " to be flexible.\n", 121 | "\n", 122 | "Returns\n", 123 | "-------\n", 124 | "output : ndarray\n", 125 | " Returns the dot product of `a` and `b`. If `a` and `b` are both\n", 126 | " scalars or both 1-D arrays then a scalar is returned; otherwise\n", 127 | " an array is returned.\n", 128 | " If `out` is given, then it is returned.\n", 129 | "\n", 130 | "Raises\n", 131 | "------\n", 132 | "ValueError\n", 133 | " If the last dimension of `a` is not the same size as\n", 134 | " the second-to-last dimension of `b`.\n", 135 | "\n", 136 | "See Also\n", 137 | "--------\n", 138 | "vdot : Complex-conjugating dot product.\n", 139 | "tensordot : Sum products over arbitrary axes.\n", 140 | "einsum : Einstein summation convention.\n", 141 | "matmul : '@' operator as method with out parameter.\n", 142 | "\n", 143 | "Examples\n", 144 | "--------\n", 145 | ">>> np.dot(3, 4)\n", 146 | "12\n", 147 | "\n", 148 | "Neither argument is complex-conjugated:\n", 149 | "\n", 150 | ">>> np.dot([2j, 3j], [2j, 3j])\n", 151 | "(-13+0j)\n", 152 | "\n", 153 | "For 2-D arrays it is the matrix product:\n", 154 | "\n", 155 | ">>> a = [[1, 0], [0, 1]]\n", 156 | ">>> b = [[4, 1], [2, 2]]\n", 157 | ">>> np.dot(a, b)\n", 158 | "array([[4, 1],\n", 159 | " [2, 2]])\n", 160 | "\n", 161 | ">>> a = np.arange(3*4*5*6).reshape((3,4,5,6))\n", 162 | ">>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))\n", 163 | ">>> np.dot(a, b)[2,3,2,1,2,2]\n", 164 | "499128\n", 165 | ">>> sum(a[2,3,2,:] * b[1,2,:,2])\n", 166 | "499128\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "np.info(np.dot)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": { 178 | "collapsed": true 179 | }, 180 | "outputs": [], 181 | "source": [] 182 | } 183 | ], 184 | "metadata": { 185 | "kernelspec": { 186 | "display_name": "Python 2", 187 | "language": "python", 188 | "name": "python2" 189 | }, 190 | "language_info": { 191 | "codemirror_mode": { 192 | "name": "ipython", 193 | "version": 2 194 | }, 195 | "file_extension": ".py", 196 | "mimetype": "text/x-python", 197 | "name": "python", 198 | "nbconvert_exporter": "python", 199 | "pygments_lexer": "ipython2", 200 | "version": "2.7.10" 201 | } 202 | }, 203 | "nbformat": 4, 204 | "nbformat_minor": 0 205 | } 206 | -------------------------------------------------------------------------------- /5_Input_and_Output.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Input and Output" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "from __future__ import print_function\n", 19 | "import numpy as np" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\"" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "'1.12.0'" 44 | ] 45 | }, 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "np.__version__" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "2017-04-01\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "from datetime import date\n", 72 | "print(date.today())" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## NumPy binary files (NPY, NPZ)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Q1. Save x into `temp.npy` and load it." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "True\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "x = np.arange(10)\n", 106 | "...\n", 107 | "\n", 108 | "# Check if there exists the 'temp.npy' file.\n", 109 | "import os\n", 110 | "if os.path.exists('temp.npy'):\n", 111 | " x2 = ...\n", 112 | " print(np.array_equal(x, x2))\n" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Q2. Save x and y into a single file 'temp.npz' and load it." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 6, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "True\n", 134 | "True\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "x = np.arange(10)\n", 140 | "y = np.arange(11, 20)\n", 141 | "...\n", 142 | "\n", 143 | "with ... as data:\n", 144 | " x2 = data['x']\n", 145 | " y2 = data['y']\n", 146 | " print(np.array_equal(x, x2))\n", 147 | " print(np.array_equal(y, y2))\n" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "## Text files" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Q3. Save x to 'temp.txt' in string format and load it." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 7, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "array([[ 0., 1., 2., 3., 4.],\n", 175 | " [ 5., 6., 7., 8., 9.]])" 176 | ] 177 | }, 178 | "execution_count": 7, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "x = np.arange(10).reshape(2, 5)\n", 185 | "header = 'num1 num2 num3 num4 num5'\n", 186 | "...\n", 187 | "..." 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "Q4. Save `x`, `y`, and `z` to 'temp.txt' in string format line by line, then load it." 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "metadata": { 201 | "collapsed": false 202 | }, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],\n", 208 | " [ 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.],\n", 209 | " [ 22., 23., 24., 25., 26., 27., 28., 29., 30., 31.]])" 210 | ] 211 | }, 212 | "execution_count": 8, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "x = np.arange(10)\n", 219 | "y = np.arange(11, 21)\n", 220 | "z = np.arange(22, 32)\n", 221 | "...\n", 222 | "..." 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "Q5. Convert `x` into bytes, and load it as array." 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 9, 235 | "metadata": { 236 | "collapsed": false 237 | }, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "True\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "x = np.array([1, 2, 3, 4])\n", 249 | "x_bytes = ...\n", 250 | "x2 = ...\n", 251 | "print(np.array_equal(x, x2))\n" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "Q6. Convert `a` into an ndarray and then convert it into a list again." 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 10, 264 | "metadata": { 265 | "collapsed": false 266 | }, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "True\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "a = [[1, 2], [3, 4]]\n", 278 | "x = ...\n", 279 | "a2 = ...\n", 280 | "print(a == a2)" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "## String formatting¶" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "Q7. Convert `x` to a string, and revert it." 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 11, 300 | "metadata": { 301 | "collapsed": false 302 | }, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "[[0 1 2 3 4]\n", 309 | " [5 6 7 8 9]] \n", 310 | " \n" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "x = np.arange(10).reshape(2,5)\n", 316 | "x_str = ...\n", 317 | "print(x_str, \"\\n\", type(x_str))\n", 318 | "x_str = x_str.replace(\"[\", \"\") # [] must be stripped\n", 319 | "x_str = x_str.replace(\"]\", \"\")\n", 320 | "x2 = ...\n", 321 | "assert np.array_equal(x, x2)\n", 322 | "\n" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "## Text formatting options" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "Q8. Print `x` such that all elements are displayed with precision=1, no suppress." 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 12, 342 | "metadata": { 343 | "collapsed": false 344 | }, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "[[ 0.8 0.5 0.9 0. 0.6 0.6 0.5 0.5 0.9 0.9 0.3 0.6 0.9 0.8\n", 351 | " 0.1 0.5 0.3 0.9 0.1 0.8 0.3 0. 0.4 0.5 0.4 0.6 0.2 0.6\n", 352 | " 0.6 0.5 0.7 0.4 0.1 0.6 0.7 0.3 0.1 0.5 0.1 0.5 0. 0.8 1.\n", 353 | " 0.5 0.3 0.9 0.3 0.6 0.9 0. 0.4 0.5 0.9 0.3 0.9 0.2 0.9\n", 354 | " 0.4 0.8 0.5 0.2 0.1 0.2 0.7 0.6 0.3 0.6 0.8 0.7 0.2 0.2\n", 355 | " 0.4 0.6 0.4 1. 0. 0.2 0.4 0.8 0.5 0.4 0.4 0.6 0.2 0.2\n", 356 | " 0.8 0.1 0.9 0.7 0.3 1. 0.6 0.7 0.4 0.1 0.9 0.2 0.4 0.1\n", 357 | " 0.3]\n", 358 | " [ 0.5 0.9 0.2 0.1 0. 0.4 0.5 0.3 0.5 0.8 0.2 0.5 0.9 0.4\n", 359 | " 0.2 0.8 0.3 0.1 0.8 0.5 0.2 0. 0.7 0.7 0.6 0.7 0.1 0.2\n", 360 | " 0.6 0.2 0.4 0.2 0.2 0.1 0.4 0.4 0.9 0.6 0.3 0.4 0.9 0.1\n", 361 | " 0.6 0.7 0.7 0.2 0.5 0.3 0.3 0.8 0. 0.1 0.3 0.9 0.8 0.9\n", 362 | " 0.3 0.5 0.5 0.7 0.5 0.8 0.2 0.9 0.1 1. 0.5 0.3 0.7 0.2\n", 363 | " 0.9 0.3 0.6 0.4 1. 0.2 1. 0.7 0.8 0.6 0.9 0.9 0.4 1. 0.3\n", 364 | " 0.7 0. 0.3 0.2 0.4 0.1 0.6 0.2 0.4 0.7 0.3 0. 0.7 0.1\n", 365 | " 0.5]\n", 366 | " [ 0.7 0.1 0.6 0.2 0.9 0.8 0.5 0.6 0.9 0.8 0.4 0.1 0.1 0.7\n", 367 | " 0.7 0.8 0.1 0.8 0.4 0.2 0.6 0.8 1. 0.8 0.6 0.8 0.3 0.1\n", 368 | " 0.5 0.5 0.4 0.2 0.8 0.4 0.4 0.6 0.9 0.7 0.8 0.5 1. 0.3\n", 369 | " 0.4 0.7 0.5 0.3 1. 0.3 0.1 0.6 0.2 0. 0.2 0.8 0.7 0.7\n", 370 | " 0.6 0.4 0.7 0.7 0.5 0.1 0.9 0. 0.1 0.3 0.3 0.1 0.6 0.5\n", 371 | " 0.4 0.1 0.3 0.2 0.1 0.7 0.3 0.8 0.8 0.9 0.7 0.5 0. 0.9\n", 372 | " 0.5 0.6 1. 0.9 0.8 0. 0.7 0.3 0.4 0.2 0.3 0.4 0.9 0.4 1.\n", 373 | " 0.2]\n", 374 | " [ 1. 0.1 0.8 0. 0.4 0.2 0.2 0.8 0.1 0.2 0.1 0.9 0.9 0.9\n", 375 | " 0.5 0.7 0.8 0.6 0.1 0.2 0.2 0.5 0.4 0.6 1. 0.1 0.8 0.5\n", 376 | " 0.3 0.6 0.6 1. 0.5 0.4 0.3 0.8 0.7 0.4 0.1 1. 0.6 0.3\n", 377 | " 0.7 0.4 0.5 1. 0.6 0.8 0.5 0.6 1. 0.7 0.2 0.6 0.4 0. 0.8\n", 378 | " 0.5 0.5 0.2 0.8 0.2 0.4 0.5 0.2 0. 0.8 0.4 0.4 0.4 0.4\n", 379 | " 0.4 0.8 0.4 0.1 0.9 0.1 0.8 0.2 0.7 1. 0.3 0.1 0.9 0.3\n", 380 | " 0.1 0.1 0.4 0.5 0.5 0.6 0.2 0.2 0.4 0.5 0.6 0.3 0.2 0.6\n", 381 | " 0.7]\n", 382 | " [ 0.1 0.5 0.4 0.1 0.1 0.9 0.5 0.9 0.8 0.8 0.8 0.4 0.4 0.7 0.\n", 383 | " 0.4 0. 0.4 0.7 0.7 0.7 0.8 0.9 0. 0.6 0.8 1. 0.5 0. 0.9\n", 384 | " 0.9 1. 0.6 0.4 0.4 0.8 0.9 0.2 0.1 0.2 0.7 0.8 0.2 0.1\n", 385 | " 0.9 0.9 0.4 0.6 0.7 0.4 0.6 0.2 0.9 0.9 0.3 0.7 0.6 0.2\n", 386 | " 0.3 0.8 0.6 0.6 0.1 0.3 0.2 0.6 0.3 0.7 0.4 0.5 0.3 0.4\n", 387 | " 0.4 0.9 0.6 0.6 0.3 0.1 0.3 0.8 0.3 0.7 0.4 0.5 0.4 0.7\n", 388 | " 0.6 0.1 0.8 0.2 0.6 0.1 1. 0.8 0.2 0.6 0.9 0.2 0.4 0.4]\n", 389 | " [ 0.4 0.5 0.5 0.7 0.3 0.7 0.2 0.3 0.6 0.9 0.7 0.3 0.1 0.4\n", 390 | " 0.6 0.8 0.3 0.2 0.4 0.2 0.8 0.4 0.2 0.9 0.2 0.9 0.8 0.5\n", 391 | " 0.9 0.7 0.2 0.5 0.3 0.7 0.6 0. 0.3 0.5 0.6 0.8 0.5 0.9\n", 392 | " 0.2 0.1 0.3 0.9 0.3 0.3 0.6 0.5 0.3 1. 0.2 0.9 0.7 0.9\n", 393 | " 0.2 0.1 0.1 0.4 0.1 0.7 0.4 0.2 0.3 0.4 0.7 1. 0.9 0.5\n", 394 | " 0.1 0.9 0.5 0.1 0.4 0.9 0.7 0.3 0.5 0.3 0.3 1. 0.7 0.8\n", 395 | " 0.3 0.6 0.3 0.6 0.7 0.7 0. 0.7 0.6 0.1 0.9 1. 0.1 0.1\n", 396 | " 0.2 0.7]\n", 397 | " [ 0.1 0.1 0.4 0.6 1. 0.4 0.1 0.4 0.5 0. 0.3 0.3 0.7 0.4\n", 398 | " 0.7 0.8 0. 0.8 0.1 0.9 0.7 0.9 0.9 0.6 0.2 0.2 0.4 0.5\n", 399 | " 0.3 0.1 0.6 0.8 0.9 0.5 0.4 0.4 0.6 0.3 0.5 0.4 0.7 0.2\n", 400 | " 0.4 0.3 0.6 0.8 0.8 1. 0.5 0.7 0.6 1. 0.5 0.1 0.2 0.1\n", 401 | " 0.4 0.3 0.5 0.3 0.5 0.6 0.2 0.3 0.8 0.5 0.6 0.9 0.8 0.3\n", 402 | " 0.3 0.5 0.2 0.1 0.5 0.4 0.4 0.6 0.2 0.5 0.8 0.2 0.4 0.6\n", 403 | " 0.2 0.3 0.4 0.8 0.3 0.2 0.6 0.5 0.4 0.3 0.4 0.6 0.1 0.6\n", 404 | " 0.9 0.9]\n", 405 | " [ 0.6 0.6 0.1 0.4 0.6 0.8 0. 0.5 0.9 0.4 0.6 0.5 0.5 0.7\n", 406 | " 0.9 0.7 0.1 0.4 0.3 0.5 0.3 0.5 0.3 1. 0.4 0.5 0.1 0.4\n", 407 | " 0.3 0.6 0.6 0.3 0.4 0.8 0.5 0.4 0. 0. 0.8 0.7 0.9 0.5\n", 408 | " 0.2 0.2 0.1 0.3 0.2 0.6 0.7 0.2 0.9 0.2 0.3 0.5 0.4 0.2\n", 409 | " 0.9 0.8 0.1 0.2 0.8 0.3 0.9 0.6 0.3 0. 0.6 0. 0.1 0.7\n", 410 | " 0.7 0.2 0.8 0.4 0.4 0.6 0.2 0.3 0.4 0.5 0.9 0.3 0. 0.6\n", 411 | " 0.4 0.9 0.6 0.3 1. 0.9 0.6 0.9 0. 0.5 0.2 0.4 1. 0.5\n", 412 | " 0.7 0.9]\n", 413 | " [ 0.3 0.2 0.9 0.4 0.8 0.8 1. 0.1 0.8 0.1 0.1 0.7 0.5 0.6\n", 414 | " 0.1 0.5 0.5 0.4 0.2 0.8 0. 0.4 0.2 0.1 0.4 1. 0.1 0. 0.7\n", 415 | " 0.1 0.4 0.3 0.7 0.4 0.5 0.5 0. 0.2 0.2 0.9 0.9 0.8 0.1\n", 416 | " 0.3 0.3 0. 0.7 0. 0.8 0.5 0.5 0.3 0.3 0.1 1. 0. 0.9\n", 417 | " 0.8 0.6 0.9 0.5 1. 0.5 0. 0. 0.1 0.6 0.7 0.1 0.6 0.4\n", 418 | " 0.5 0.2 0.4 0.5 0.8 0.6 0.3 1. 0.9 0.6 0.1 0.7 0. 0.7\n", 419 | " 0.4 0.6 0.3 0.5 0.9 0.4 0.8 0.8 0.2 0.3 0.5 0.1 0.5 0.6\n", 420 | " 0.8]\n", 421 | " [ 0.4 0.6 0.6 1. 0.3 0.6 0.6 0.1 0.1 0.9 0.1 0.9 0.5 1. 0.6\n", 422 | " 0.1 0.8 0.1 0.4 0.1 0.7 0.9 0.7 0.4 0.3 0.5 0.5 0.4 0. 0.7\n", 423 | " 0.7 0.3 0.9 1. 0.7 0. 0.2 1. 0.6 0.3 0.3 0.1 0.7 0.5\n", 424 | " 0.3 0.7 0.1 0.9 0.6 0.9 0.3 0.1 0.9 1. 0.1 1. 0.6 0.7\n", 425 | " 0.5 0.6 1. 0.9 0.2 0.2 0.3 0.4 0.6 0.1 0.4 0. 0.9 0.4\n", 426 | " 0.9 0.7 0.1 1. 0.7 0.1 0.7 0.2 0.9 0.7 0.1 0.1 0.9 0.6\n", 427 | " 0.1 0.1 0.2 0.6 0.8 0.9 0.2 0.1 0.2 0.3 0.5 0.7 0.8 0.8]]\n" 428 | ] 429 | } 430 | ], 431 | "source": [ 432 | "x = np.random.uniform(size=[10,100])\n", 433 | "np.set_printoptions(...)\n", 434 | "print(x)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "## Base-n representations" 442 | ] 443 | }, 444 | { 445 | "cell_type": "markdown", 446 | "metadata": {}, 447 | "source": [ 448 | "Q9. Convert 12 into a binary number in string format." 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 13, 454 | "metadata": { 455 | "collapsed": false 456 | }, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "1100\n" 463 | ] 464 | } 465 | ], 466 | "source": [] 467 | }, 468 | { 469 | "cell_type": "markdown", 470 | "metadata": {}, 471 | "source": [ 472 | "Q10. Convert 12 into a hexadecimal number in string format." 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 14, 478 | "metadata": { 479 | "collapsed": false 480 | }, 481 | "outputs": [ 482 | { 483 | "data": { 484 | "text/plain": [ 485 | "'44C'" 486 | ] 487 | }, 488 | "execution_count": 14, 489 | "metadata": {}, 490 | "output_type": "execute_result" 491 | } 492 | ], 493 | "source": [] 494 | } 495 | ], 496 | "metadata": { 497 | "anaconda-cloud": {}, 498 | "kernelspec": { 499 | "display_name": "Python [conda root]", 500 | "language": "python", 501 | "name": "conda-root-py" 502 | }, 503 | "language_info": { 504 | "codemirror_mode": { 505 | "name": "ipython", 506 | "version": 3 507 | }, 508 | "file_extension": ".py", 509 | "mimetype": "text/x-python", 510 | "name": "python", 511 | "nbconvert_exporter": "python", 512 | "pygments_lexer": "ipython3", 513 | "version": "3.5.2" 514 | } 515 | }, 516 | "nbformat": 4, 517 | "nbformat_minor": 1 518 | } 519 | -------------------------------------------------------------------------------- /5_Input_and_Output_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Input and Output" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "from __future__ import print_function\n", 19 | "import numpy as np" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\"" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 15, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "'1.12.0'" 44 | ] 45 | }, 46 | "execution_count": 15, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "np.__version__" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 16, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "2017-04-01\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "from datetime import date\n", 72 | "print(date.today())" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## NumPy binary files (NPY, NPZ)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Q1. Save x into `temp.npy` and load it." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 4, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "True\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "x = np.arange(10)\n", 106 | "np.save('temp.npy', x) # Actually you can omit the extension. If so, it will be added automatically.\n", 107 | "\n", 108 | "# Check if there exists the 'temp.npy' file.\n", 109 | "import os\n", 110 | "if os.path.exists('temp.npy'):\n", 111 | " x2 = np.load('temp.npy')\n", 112 | " print(np.array_equal(x, x2))\n" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Q2. Save x and y into a single file 'temp.npz' and load it." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 5, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "True\n", 134 | "True\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "x = np.arange(10)\n", 140 | "y = np.arange(11, 20)\n", 141 | "np.savez('temp.npz', x=x, y=y)\n", 142 | "# np.savez_compressed('temp.npz', x=x, y=y) # If you want to save x and y into a single file in compressed .npz format.\n", 143 | "with np.load('temp.npz') as data:\n", 144 | " x2 = data['x']\n", 145 | " y2 = data['y']\n", 146 | " print(np.array_equal(x, x2))\n", 147 | " print(np.array_equal(y, y2))\n" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "## Text files" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Q3. Save x to 'temp.txt' in string format and load it." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 6, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "array([[ 0., 1., 2., 3., 4.],\n", 175 | " [ 5., 6., 7., 8., 9.]])" 176 | ] 177 | }, 178 | "execution_count": 6, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "x = np.arange(10).reshape(2, 5)\n", 185 | "header = 'num1 num2 num3 num4 num5'\n", 186 | "np.savetxt('temp.txt', x, fmt=\"%d\", header=header) \n", 187 | "np.loadtxt('temp.txt')" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "Q4. Save `x`, `y`, and `z` to 'temp.txt' in string format line by line, then load it." 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 7, 200 | "metadata": { 201 | "collapsed": false 202 | }, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],\n", 208 | " [ 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.],\n", 209 | " [ 22., 23., 24., 25., 26., 27., 28., 29., 30., 31.]])" 210 | ] 211 | }, 212 | "execution_count": 7, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "x = np.arange(10)\n", 219 | "y = np.arange(11, 21)\n", 220 | "z = np.arange(22, 32)\n", 221 | "np.savetxt('temp.txt', (x, y, z), fmt='%d')\n", 222 | "np.loadtxt('temp.txt')" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "Q5. Convert `x` into bytes, and load it as array." 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 8, 235 | "metadata": { 236 | "collapsed": false 237 | }, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "True\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "x = np.array([1, 2, 3, 4])\n", 249 | "x_bytes = x.tostring() # Don't be misled by the function name. What it really does is it returns bytes.\n", 250 | "x2 = np.fromstring(x_bytes, dtype=x.dtype) # returns a 1-D array even if x is not.\n", 251 | "print(np.array_equal(x, x2))\n" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "Q6. Convert `a` into an ndarray and then convert it into a list again." 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 9, 264 | "metadata": { 265 | "collapsed": false 266 | }, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "True\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "a = [[1, 2], [3, 4]]\n", 278 | "x = np.array(a)\n", 279 | "a2 = x.tolist()\n", 280 | "print(a == a2)" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "## String formatting¶" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "Q7. Convert `x` to a string, and revert it." 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 10, 300 | "metadata": { 301 | "collapsed": false 302 | }, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "[[0 1 2 3 4]\n", 309 | " [5 6 7 8 9]] \n", 310 | " \n" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "x = np.arange(10).reshape(2,5)\n", 316 | "x_str = np.array_str(x)\n", 317 | "print(x_str, \"\\n\", type(x_str))\n", 318 | "x_str = x_str.replace(\"[\", \"\") # [] must be stripped\n", 319 | "x_str = x_str.replace(\"]\", \"\")\n", 320 | "x2 = np.fromstring(x_str, dtype=x.dtype, sep=\" \").reshape(x.shape)\n", 321 | "assert np.array_equal(x, x2)\n", 322 | "\n" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "## Text formatting options" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "Q8. Print `x` such that all elements are displayed with precision=1, no suppress." 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 11, 342 | "metadata": { 343 | "collapsed": false 344 | }, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "[[ 0.5 0. 0.8 0.2 0.3 0.2 0.2 1. 0.4 0.8 0.6 0.2 0.5 0.1\n", 351 | " 0.4 0.1 0.9 0.6 0.1 0.5 0.8 0.8 0.8 0. 0.6 0.8 0.4 0.3\n", 352 | " 0.8 0.2 0.7 0.7 0.2 1. 0.8 0.1 0.2 0.1 0.3 0.1 0.5 0.9\n", 353 | " 0.6 0.9 0.6 0.5 0.8 0.3 0.3 0.5 0.1 0.6 0.1 0.3 0.6 0.2\n", 354 | " 0.4 0.8 0.6 0.4 0.2 0.6 0. 0.3 0.8 0.5 0.7 0.9 0.8 0.6\n", 355 | " 0.9 0.8 0.4 0.4 0.7 0.8 0. 0.1 0.5 0.4 0.7 1. 0.1 0.2\n", 356 | " 0.6 0.3 0.9 0.1 0.6 0.4 0.3 0.8 0.3 0.6 0.6 0.3 1. 0.2\n", 357 | " 0.9 0.2]\n", 358 | " [ 0.9 0.2 0.4 0.9 0.5 0.6 0.1 0.7 0. 0. 0.1 0.8 0.8 1. 0.2\n", 359 | " 0.8 0.3 0.2 1. 0.6 1. 0.3 0.4 0.4 0.7 0.5 0.4 0.8 0.5\n", 360 | " 0.9 0.3 0.5 0.7 0.4 0.2 0.3 0.9 0. 0.6 0.8 0.3 0.5 0.2\n", 361 | " 0.3 0. 0.6 0.5 0.2 0.5 0.8 0.2 0.8 0. 0.9 0. 0.7 0.1\n", 362 | " 0.4 0.2 0.5 0.6 0.2 0.6 0.1 0.1 0. 0.5 0.9 0.4 0.5 0.8\n", 363 | " 0.5 0.1 0.7 0. 1. 0.5 0.4 0.2 0. 1. 0.4 0.1 0.7 0.7\n", 364 | " 0.4 0.8 0.4 0.6 0.6 0.5 0.8 0.8 0.2 0.2 0.3 0.2 0.5 0.9\n", 365 | " 0.5]\n", 366 | " [ 0.3 0.6 0.4 0.5 0.5 0. 0.7 0.1 0. 0.9 0.5 0.7 0.6 0.3\n", 367 | " 0.9 0.5 0.1 0.4 0.1 0.9 0.8 0.6 0.8 0.8 0.1 0.4 0.9 0.1 1.\n", 368 | " 0.7 0.4 0.3 0.8 0.3 0.8 0.8 0.2 0.7 0.2 0.8 0.3 0.9 0.1\n", 369 | " 0.9 0.2 0.8 0.9 0.6 0.1 0.3 0.4 1. 0.1 0.7 0.3 0.9 0.3\n", 370 | " 0.5 0.9 0. 0.6 0. 0.8 0.1 0.9 0. 0.8 0.6 0.5 0.5 0.2 1.\n", 371 | " 0.4 0. 0.2 0. 0.9 0.9 0.8 0.2 0.7 0.3 0.2 0.1 1. 0.4\n", 372 | " 0.5 0.4 0.8 0.8 0.8 0.7 0.6 0.4 0.7 0.6 0.5 0.8 0.7 0.6]\n", 373 | " [ 0.2 0.6 0.9 0.7 0.1 0.1 1. 0.5 0.8 0.3 1. 0.4 0.1 0.5\n", 374 | " 0.6 0.8 0.8 0.8 0.1 1. 0.8 0. 0.7 0.6 0.8 0.2 0.5 0.9\n", 375 | " 0.4 0.8 0.7 0.2 0.8 0.6 0.9 0.6 0.9 0.8 0.9 1. 0.6 0.6\n", 376 | " 0.7 0.1 0.5 0.3 0. 0.8 0. 0.5 0.8 0.3 0.8 0.7 0.1 0.5\n", 377 | " 0.2 0.1 0.7 0. 0. 0.6 0. 0.8 0.7 0.1 0.4 0.1 0.2 0.1\n", 378 | " 0.9 0.6 0.9 0.3 0.4 0.9 0.2 0.6 0.8 0.9 0.6 0.8 0.5 0.1\n", 379 | " 0.6 1. 0. 0.7 0.7 0.4 0.1 0.9 0.4 0.1 0.7 0.6 0.3 0.9\n", 380 | " 0.3 0.5]\n", 381 | " [ 0.9 0.3 0.1 0.1 0.2 0.4 0.3 0.5 0.2 0. 0.5 0.4 0.5 0.3\n", 382 | " 0.6 1. 0.1 0.7 0.6 0.2 0.3 0.3 0.1 0.5 0.6 0. 0.6 0.7\n", 383 | " 0.6 0.4 0.2 0.6 0.1 0.9 0.9 0.1 0.9 0.1 0.6 0.6 0. 0.1\n", 384 | " 0.6 0.4 0.3 0.1 0.9 0.8 0.1 0.2 0.8 0.4 0.7 0.8 0.6 0.4\n", 385 | " 0.9 0.3 0.6 0.7 0.4 0.8 0.3 0. 0. 0.9 0.3 0.3 0.8 0.5\n", 386 | " 0.8 1. 0.2 0.6 0.6 0.2 0.2 0.2 0.4 0.6 0.6 0.4 0.4 0.8\n", 387 | " 0.2 0.5 0.7 0.7 0.1 0.9 0.5 0.6 0.3 0.3 0.6 0.8 0.6 0.8\n", 388 | " 0.4 0.3]\n", 389 | " [ 0.3 1. 0.6 0.9 0.6 1. 0.7 0.9 0.4 0.3 0.9 0.9 0.3 0.8\n", 390 | " 0.3 0.6 0.7 0.3 0.1 0.1 0.4 0.3 0.6 0.5 0.1 0.6 0.1 0.5\n", 391 | " 0.9 0.5 0.5 0.6 0.4 0.4 0.3 1. 0.6 0.6 0.3 0.1 0.4 0.7\n", 392 | " 0.7 0.1 0.5 0.1 0.3 0.1 0.6 0.7 0. 0.1 0.2 0.4 0.1 0.4\n", 393 | " 0.7 0.3 0.2 0.9 0.5 0. 0.4 0.9 1. 0.4 0. 0.2 0.3 0.9\n", 394 | " 0.3 0. 0.8 0.9 0.8 0.6 0.4 0.5 0. 0.9 0.6 0.6 0.1 0.6\n", 395 | " 0.9 0.1 0.8 0.6 0.6 0.5 0.7 1. 0.5 0.3 0.3 0.4 0.6 0.6 1.\n", 396 | " 0.2]\n", 397 | " [ 0.7 0.7 0.9 0.2 0.6 0.3 0.9 0.2 0.9 0.8 0.5 0.3 0.9 0.5 1.\n", 398 | " 0.6 0.9 0.5 0.5 0.1 0.8 0.3 0.9 0.5 0.7 1. 0.6 0.7 0.1\n", 399 | " 0.7 0.9 0.4 0.8 0.9 0.4 1. 0.1 1. 0.5 0.1 0.4 0.7 1. 0.4\n", 400 | " 0.3 0.2 0.2 0.6 0.6 0.3 0.7 0.5 0.7 0.1 0.3 0.5 1. 0.8\n", 401 | " 0.4 0.8 0.8 0.7 0.1 0.2 0.4 0.3 0.4 0.3 0.5 0.4 0.6 0.3\n", 402 | " 0.1 0.7 0.8 0.6 0.6 0.2 0.7 0.9 0.9 0.7 0.3 0.9 0.4 0.6 0.\n", 403 | " 0.4 0.4 0.2 0.8 0.3 0.1 0.2 0.6 0.5 0.9 0.8 0.9 0.7]\n", 404 | " [ 0.8 0.7 0.7 0.6 0.9 0.1 0.4 0.9 1. 0.3 0. 0.2 0.1 0.5\n", 405 | " 0.8 0.1 0.7 0.7 0.6 1. 0.7 1. 0.4 0.6 0.2 0.4 0.4 0.6 0.\n", 406 | " 0.1 1. 0.5 0.1 0.2 0.8 0.2 0.1 0.4 0.7 0.5 0.4 1. 0.5\n", 407 | " 0.5 0.4 0.8 0.2 0.1 0.7 0.2 0.1 0.4 0.3 0.6 0.9 0.9 0.9\n", 408 | " 0.9 0.1 0.1 0. 1. 0. 0.1 0.4 0.6 1. 0.4 0.9 0.3 0.2\n", 409 | " 0.7 0. 0.3 0.2 0.7 0.4 0.3 0.9 0.3 0. 0.5 0.2 0.3 0.1\n", 410 | " 0.2 0. 0.1 0.6 0.9 0.2 0.5 0.8 0.7 0. 0.4 0.8 0.8 0.5\n", 411 | " 0.2]\n", 412 | " [ 0.2 0.3 0. 0.1 0.8 0.4 0.1 0.2 0. 0.7 0. 1. 0.6 0.7\n", 413 | " 0.3 0.3 0.7 0.9 0.3 0.7 0.1 0.1 0.5 0.6 0.3 0.8 0.7 0.1\n", 414 | " 0.6 0.6 0.3 0.2 0.3 0.3 1. 0.1 0.1 0.2 0.4 0.4 0.6 0.5\n", 415 | " 0.7 0.7 0.2 0. 0.8 0.3 0.9 0.1 0.1 0.4 0.4 0.5 0.3 0.9\n", 416 | " 0.6 0.9 0.3 0.5 0. 0.4 0.8 1. 0.3 0.5 0.7 0.5 0.8 0.7\n", 417 | " 0.6 0.3 0.1 0.2 0.5 1. 0.9 0.5 0.6 0.6 0.2 0.8 0.6 0. 0.5\n", 418 | " 0.6 0.8 0.5 0.8 0.8 0.9 0.7 0.9 0.5 0.2 1. 1. 0.1 0.3\n", 419 | " 0.3]\n", 420 | " [ 0. 0.3 0.4 0.7 0.2 0.9 0.2 0.3 0.6 0.8 0.4 0.7 0.3 0.5\n", 421 | " 0.6 0.3 0.7 0. 0.1 0.1 0.9 0. 0.7 0.7 0.1 0.6 0.6 0. 0.3\n", 422 | " 0.5 0.9 0.3 0.1 0.3 0.1 0.9 0.6 0.3 0.3 0.4 0.4 0.2 0.3\n", 423 | " 0.1 0.5 0.3 0.8 0. 0.8 0.6 0.2 0.7 0.4 0.8 0.2 0.9 1. 1.\n", 424 | " 0.7 0.9 0.1 0.2 0. 0.5 0.8 0.7 0.6 0.7 0.7 0.5 0.9 0.2\n", 425 | " 0.2 0.1 0.2 0.1 0.7 1. 0.6 0.3 0.9 1. 0.3 0.3 0.7 0.9\n", 426 | " 0.5 0.8 0.9 0.7 0.2 0.7 0.3 0.1 0.9 0.2 0.5 0.6 0.3 0.4]]\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "x = np.random.uniform(size=[10,100])\n", 432 | "np.set_printoptions(precision=1, threshold=np.nan, suppress=True)\n", 433 | "print(x)" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": {}, 439 | "source": [ 440 | "## Base-n representations" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "Q9. Convert 12 into a binary number in string format." 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 12, 453 | "metadata": { 454 | "collapsed": false 455 | }, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "1100\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "out1 = np.binary_repr(12)\n", 467 | "out2 = np.base_repr(12, base=2)\n", 468 | "assert out1 == out2 # But out1 is better because it's much faster.\n", 469 | "print(out1)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "Q10. Convert 12 into a hexadecimal number in string format." 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 13, 482 | "metadata": { 483 | "collapsed": false 484 | }, 485 | "outputs": [ 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "'44C'" 490 | ] 491 | }, 492 | "execution_count": 13, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "np.base_repr(1100, base=16)" 499 | ] 500 | } 501 | ], 502 | "metadata": { 503 | "anaconda-cloud": {}, 504 | "kernelspec": { 505 | "display_name": "Python [conda root]", 506 | "language": "python", 507 | "name": "conda-root-py" 508 | }, 509 | "language_info": { 510 | "codemirror_mode": { 511 | "name": "ipython", 512 | "version": 3 513 | }, 514 | "file_extension": ".py", 515 | "mimetype": "text/x-python", 516 | "name": "python", 517 | "nbconvert_exporter": "python", 518 | "pygments_lexer": "ipython3", 519 | "version": "3.5.2" 520 | } 521 | }, 522 | "nbformat": 4, 523 | "nbformat_minor": 1 524 | } 525 | -------------------------------------------------------------------------------- /6_Linear_algebra.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Linear algebra" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Matrix and vector products" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Q1. Predict the results of the following code." 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 53, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "x = [1,2]\n", 66 | "y = [[4, 1], [2, 2]]\n", 67 | "#print np.dot(x, y)\n", 68 | "#print np.dot(y, x)\n", 69 | "#print np.matmul(x, y)\n", 70 | "#print np.inner(x, y)\n", 71 | "#print np.inner(y, x)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "Q2. Predict the results of the following code." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 52, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "x = [[1, 0], [0, 1]]\n", 90 | "y = [[4, 1], [2, 2], [1, 1]]\n", 91 | "#print np.dot(y, x)\n", 92 | "#print np.matmul(y, x)\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "Q3. Predict the results of the following code." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 37, 105 | "metadata": { 106 | "collapsed": false 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "x = np.array([[1, 4], [5, 6]])\n", 111 | "y = np.array([[4, 1], [2, 2]])\n", 112 | "#print np.vdot(x, y)\n", 113 | "#print np.vdot(y, x)\n", 114 | "#print np.dot(x.flatten(), y.flatten())\n", 115 | "#print np.inner(x.flatten(), y.flatten())\n", 116 | "#print (x*y).sum()" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Q4. Predict the results of the following code." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 45, 129 | "metadata": { 130 | "collapsed": false 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "x = np.array(['a', 'b'], dtype=object)\n", 135 | "y = np.array([1, 2])\n", 136 | "#print np.inner(x, y)\n", 137 | "#print np.inner(y, x)\n", 138 | "#print np.outer(x, y)\n", 139 | "#print np.outer(y, x)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "## Decompositions" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "Q5. Get the lower-trianglular `L` in the Cholesky decomposition of x and verify it." 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 97, 159 | "metadata": { 160 | "collapsed": false 161 | }, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "[[ 2. 0. 0.]\n", 168 | " [ 6. 1. 0.]\n", 169 | " [-8. 5. 3.]]\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Q6. Compute the qr factorization of x and verify it." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 107, 187 | "metadata": { 188 | "collapsed": false 189 | }, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "q=\n", 196 | "[[-0.85714287 0.39428571 0.33142856]\n", 197 | " [-0.42857143 -0.90285712 -0.03428571]\n", 198 | " [ 0.2857143 -0.17142858 0.94285715]] \n", 199 | "r=\n", 200 | "[[ -14. -21. 14.]\n", 201 | " [ 0. -175. 70.]\n", 202 | " [ 0. 0. -35.]]\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)\n" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Q7. Factor x by Singular Value Decomposition and verify it." 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 165, 220 | "metadata": { 221 | "collapsed": false 222 | }, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "U=\n", 229 | "[[ 0. 1. 0. 0.]\n", 230 | " [ 1. 0. 0. 0.]\n", 231 | " [ 0. 0. 0. -1.]\n", 232 | " [ 0. 0. 1. 0.]] \n", 233 | "s=\n", 234 | "[ 3. 2.23606801 2. 0. ] \n", 235 | "V=\n", 236 | "[[ 1. 0. 0.]\n", 237 | " [ 0. 1. 0.]\n", 238 | " [ 0. 0. 1.]]\n" 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)\n" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "## Matrix eigenvalues" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 77, 263 | "metadata": { 264 | "collapsed": false 265 | }, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "eigenvalues are\n", 272 | "[ 1. 2. 3.]\n", 273 | "eigenvectors are\n", 274 | "[[ 1. 0. 0.]\n", 275 | " [ 0. 1. 0.]\n", 276 | " [ 0. 0. 1.]]\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "x = np.diag((1, 2, 3))\n" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "Q9. Predict the results of the following code." 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 81, 294 | "metadata": { 295 | "collapsed": false 296 | }, 297 | "outputs": [], 298 | "source": [ 299 | "#print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "## Norms and other numbers" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Q10. Calculate the Frobenius norm and the condition number of x." 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 12, 319 | "metadata": { 320 | "collapsed": false 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "16.8819430161\n", 328 | "4.56177073661e+17\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "x = np.arange(1, 10).reshape((3, 3))\n" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "Q11. Calculate the determinant of x." 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 22, 346 | "metadata": { 347 | "collapsed": false 348 | }, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "-2.0\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "x = np.arange(1, 5).reshape((2, 2))\n" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [ 366 | "Q12. Calculate the rank of x." 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 35, 372 | "metadata": { 373 | "collapsed": false 374 | }, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "4\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "x = np.eye(4)\n" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "Q13. Compute the sign and natural logarithm of the determinant of x." 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 49, 398 | "metadata": { 399 | "collapsed": false 400 | }, 401 | "outputs": [ 402 | { 403 | "name": "stdout", 404 | "output_type": "stream", 405 | "text": [ 406 | "-1.0 0.69314718056\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "x = np.arange(1, 5).reshape((2, 2))\n", 412 | "\n" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "Q14. Return the sum along the diagonal of x." 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 57, 425 | "metadata": { 426 | "collapsed": false 427 | }, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "4.0\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "x = np.eye(4)\n" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "## Solving equations and inverting matrices" 446 | ] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "metadata": { 451 | "collapsed": false 452 | }, 453 | "source": [ 454 | "Q15. Compute the inverse of x." 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 60, 460 | "metadata": { 461 | "collapsed": false 462 | }, 463 | "outputs": [ 464 | { 465 | "name": "stdout", 466 | "output_type": "stream", 467 | "text": [ 468 | "[[-2. 1. ]\n", 469 | " [ 1.5 -0.5]]\n" 470 | ] 471 | } 472 | ], 473 | "source": [ 474 | "x = np.array([[1., 2.], [3., 4.]])\n", 475 | "\n" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": null, 481 | "metadata": { 482 | "collapsed": true 483 | }, 484 | "outputs": [], 485 | "source": [] 486 | } 487 | ], 488 | "metadata": { 489 | "kernelspec": { 490 | "display_name": "Python 2", 491 | "language": "python", 492 | "name": "python2" 493 | }, 494 | "language_info": { 495 | "codemirror_mode": { 496 | "name": "ipython", 497 | "version": 2 498 | }, 499 | "file_extension": ".py", 500 | "mimetype": "text/x-python", 501 | "name": "python", 502 | "nbconvert_exporter": "python", 503 | "pygments_lexer": "ipython2", 504 | "version": "2.7.10" 505 | } 506 | }, 507 | "nbformat": 4, 508 | "nbformat_minor": 0 509 | } 510 | -------------------------------------------------------------------------------- /6_Linear_algebra_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Linear algebra" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Matrix and vector products" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Q1. Predict the results of the following code." 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 61, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "[8 5]\n", 69 | "[6 6]\n", 70 | "[8 5]\n", 71 | "[6 6]\n", 72 | "[6 6]\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "x = [1,2]\n", 78 | "y = [[4, 1], [2, 2]]\n", 79 | "print np.dot(x, y)\n", 80 | "print np.dot(y, x)\n", 81 | "print np.matmul(x, y)\n", 82 | "print np.inner(x, y)\n", 83 | "print np.inner(y, x)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Q2. Predict the results of the following code." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 62, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "[[4 1]\n", 105 | " [2 2]\n", 106 | " [1 1]]\n", 107 | "[[4 1]\n", 108 | " [2 2]\n", 109 | " [1 1]]\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "x = [[1, 0], [0, 1]]\n", 115 | "y = [[4, 1], [2, 2], [1, 1]]\n", 116 | "print np.dot(y, x)\n", 117 | "print np.matmul(y, x)\n" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "Q3. Predict the results of the following code." 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 63, 130 | "metadata": { 131 | "collapsed": false 132 | }, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "30\n", 139 | "30\n", 140 | "30\n", 141 | "30\n", 142 | "30\n" 143 | ] 144 | } 145 | ], 146 | "source": [ 147 | "x = np.array([[1, 4], [5, 6]])\n", 148 | "y = np.array([[4, 1], [2, 2]])\n", 149 | "print np.vdot(x, y)\n", 150 | "print np.vdot(y, x)\n", 151 | "print np.dot(x.flatten(), y.flatten())\n", 152 | "print np.inner(x.flatten(), y.flatten())\n", 153 | "print (x*y).sum()" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Q4. Predict the results of the following code." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 65, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "abb\n", 175 | "abb\n", 176 | "[['a' 'aa']\n", 177 | " ['b' 'bb']]\n", 178 | "[['a' 'b']\n", 179 | " ['aa' 'bb']]\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "x = np.array(['a', 'b'], dtype=object)\n", 185 | "y = np.array([1, 2])\n", 186 | "print np.inner(x, y)\n", 187 | "print np.inner(y, x)\n", 188 | "print np.outer(x, y)\n", 189 | "print np.outer(y, x)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "## Decompositions" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "Q5. Get the lower-trianglular `L` in the Cholesky decomposition of x and verify it." 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 97, 209 | "metadata": { 210 | "collapsed": false 211 | }, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "[[ 2. 0. 0.]\n", 218 | " [ 6. 1. 0.]\n", 219 | " [-8. 5. 3.]]\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)\n", 225 | "L = np.linalg.cholesky(x)\n", 226 | "print L\n", 227 | "assert np.array_equal(np.dot(L, L.T.conjugate()), x)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "Q6. Compute the qr factorization of x and verify it." 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 107, 240 | "metadata": { 241 | "collapsed": false 242 | }, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "q=\n", 249 | "[[-0.85714287 0.39428571 0.33142856]\n", 250 | " [-0.42857143 -0.90285712 -0.03428571]\n", 251 | " [ 0.2857143 -0.17142858 0.94285715]] \n", 252 | "r=\n", 253 | "[[ -14. -21. 14.]\n", 254 | " [ 0. -175. 70.]\n", 255 | " [ 0. 0. -35.]]\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)\n", 261 | "q, r = np.linalg.qr(x)\n", 262 | "print \"q=\\n\", q, \"\\nr=\\n\", r\n", 263 | "assert np.allclose(np.dot(q, r), x)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "Q7. Factor x by Singular Value Decomposition and verify it." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 165, 276 | "metadata": { 277 | "collapsed": false 278 | }, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "U=\n", 285 | "[[ 0. 1. 0. 0.]\n", 286 | " [ 1. 0. 0. 0.]\n", 287 | " [ 0. 0. 0. -1.]\n", 288 | " [ 0. 0. 1. 0.]] \n", 289 | "s=\n", 290 | "[ 3. 2.23606801 2. 0. ] \n", 291 | "V=\n", 292 | "[[ 1. 0. 0.]\n", 293 | " [ 0. 1. 0.]\n", 294 | " [ 0. 0. 1.]]\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)\n", 300 | "U, s, V = np.linalg.svd(x, full_matrices=False)\n", 301 | "print \"U=\\n\", U, \"\\ns=\\n\", s, \"\\nV=\\n\", v\n", 302 | "assert np.allclose(np.dot(U, np.dot(np.diag(s), V)), x)\n" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "## Matrix eigenvalues" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 68, 322 | "metadata": { 323 | "collapsed": false 324 | }, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "eigenvalues are\n", 331 | "[ 1. 2. 3.]\n", 332 | "eigenvectors are\n", 333 | "[[ 1. 0. 0.]\n", 334 | " [ 0. 1. 0.]\n", 335 | " [ 0. 0. 1.]]\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "x = np.diag((1, 2, 3))\n", 341 | "eigenvals = np.linalg.eig(x)[0]\n", 342 | "eigenvals_ = np.linalg.eigvals(x)\n", 343 | "assert np.array_equal(eigenvals, eigenvals_)\n", 344 | "print \"eigenvalues are\\n\", eigenvals\n", 345 | "eigenvecs = np.linalg.eig(x)[1]\n", 346 | "print \"eigenvectors are\\n\", eigenvecs" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "Q9. Predict the results of the following code." 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 69, 359 | "metadata": { 360 | "collapsed": false 361 | }, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "True\n" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "## Norms and other numbers" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "Q10. Calculate the Frobenius norm and the condition number of x." 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 12, 392 | "metadata": { 393 | "collapsed": false 394 | }, 395 | "outputs": [ 396 | { 397 | "name": "stdout", 398 | "output_type": "stream", 399 | "text": [ 400 | "16.8819430161\n", 401 | "4.56177073661e+17\n" 402 | ] 403 | } 404 | ], 405 | "source": [ 406 | "x = np.arange(1, 10).reshape((3, 3))\n", 407 | "print np.linalg.norm(x, 'fro')\n", 408 | "print np.linalg.cond(x, 'fro')" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "Q11. Calculate the determinant of x." 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 22, 421 | "metadata": { 422 | "collapsed": false 423 | }, 424 | "outputs": [ 425 | { 426 | "name": "stdout", 427 | "output_type": "stream", 428 | "text": [ 429 | "-2.0\n" 430 | ] 431 | } 432 | ], 433 | "source": [ 434 | "x = np.arange(1, 5).reshape((2, 2))\n", 435 | "out1 = np.linalg.det(x)\n", 436 | "out2 = x[0, 0] * x[1, 1] - x[0, 1] * x[1, 0]\n", 437 | "assert np.allclose(out1, out2)\n", 438 | "print out1" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "Q12. Calculate the rank of x." 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 35, 451 | "metadata": { 452 | "collapsed": false 453 | }, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "4\n" 460 | ] 461 | } 462 | ], 463 | "source": [ 464 | "x = np.eye(4)\n", 465 | "out1 = np.linalg.matrix_rank(x)\n", 466 | "out2 = np.linalg.svd(x)[1].size\n", 467 | "assert out1 == out2\n", 468 | "print out1\n" 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "metadata": {}, 474 | "source": [ 475 | "Q13. Compute the sign and natural logarithm of the determinant of x." 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 49, 481 | "metadata": { 482 | "collapsed": false 483 | }, 484 | "outputs": [ 485 | { 486 | "name": "stdout", 487 | "output_type": "stream", 488 | "text": [ 489 | "-1.0 0.69314718056\n" 490 | ] 491 | } 492 | ], 493 | "source": [ 494 | "x = np.arange(1, 5).reshape((2, 2))\n", 495 | "sign, logdet = np.linalg.slogdet(x)\n", 496 | "det = np.linalg.det(x)\n", 497 | "assert sign == np.sign(det)\n", 498 | "assert logdet == np.log(np.abs(det))\n", 499 | "print sign, logdet\n" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "Q14. Return the sum along the diagonal of x." 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": 57, 512 | "metadata": { 513 | "collapsed": false 514 | }, 515 | "outputs": [ 516 | { 517 | "name": "stdout", 518 | "output_type": "stream", 519 | "text": [ 520 | "4.0\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "x = np.eye(4)\n", 526 | "out1 = np.trace(x)\n", 527 | "out2 = x.diagonal().sum()\n", 528 | "assert out1 == out2\n", 529 | "print out1" 530 | ] 531 | }, 532 | { 533 | "cell_type": "markdown", 534 | "metadata": {}, 535 | "source": [ 536 | "## Solving equations and inverting matrices" 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "metadata": { 542 | "collapsed": false 543 | }, 544 | "source": [ 545 | "Q15. Compute the inverse of x." 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 60, 551 | "metadata": { 552 | "collapsed": false 553 | }, 554 | "outputs": [ 555 | { 556 | "name": "stdout", 557 | "output_type": "stream", 558 | "text": [ 559 | "[[-2. 1. ]\n", 560 | " [ 1.5 -0.5]]\n" 561 | ] 562 | } 563 | ], 564 | "source": [ 565 | "x = np.array([[1., 2.], [3., 4.]])\n", 566 | "out1 = np.linalg.inv(x)\n", 567 | "assert np.allclose(np.dot(x, out1), np.eye(2))\n", 568 | "print out1\n" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": null, 574 | "metadata": { 575 | "collapsed": true 576 | }, 577 | "outputs": [], 578 | "source": [] 579 | } 580 | ], 581 | "metadata": { 582 | "kernelspec": { 583 | "display_name": "Python 2", 584 | "language": "python", 585 | "name": "python2" 586 | }, 587 | "language_info": { 588 | "codemirror_mode": { 589 | "name": "ipython", 590 | "version": 2 591 | }, 592 | "file_extension": ".py", 593 | "mimetype": "text/x-python", 594 | "name": "python", 595 | "nbconvert_exporter": "python", 596 | "pygments_lexer": "ipython2", 597 | "version": "2.7.10" 598 | } 599 | }, 600 | "nbformat": 4, 601 | "nbformat_minor": 0 602 | } 603 | -------------------------------------------------------------------------------- /8_Logic_functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Logic functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Truth value testing\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "True\n", 69 | "False\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "x = np.array([1,2,3])\n", 75 | "#\n", 76 | "\n", 77 | "x = np.array([1,0,3])\n", 78 | "#" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "True\n", 100 | "False\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "x = np.array([1,0,0])\n", 106 | "#\n", 107 | "\n", 108 | "x = np.array([0,0,0])\n", 109 | "#" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## Array contents\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Q3. Predict the result of the following code." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 8, 129 | "metadata": { 130 | "collapsed": false 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "x = np.array([1, 0, np.nan, np.inf])\n", 135 | "#print np.isfinite(x)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "Q4. Predict the result of the following code." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 10, 148 | "metadata": { 149 | "collapsed": false 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "x = np.array([1, 0, np.nan, np.inf])\n", 154 | "#print np.isinf(x)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Q5. Predict the result of the following code." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 12, 167 | "metadata": { 168 | "collapsed": true 169 | }, 170 | "outputs": [], 171 | "source": [ 172 | "x = np.array([1, 0, np.nan, np.inf])\n", 173 | "#print np.isnan(x)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "## Array type testing" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Q6. Predict the result of the following code." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 15, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n", 199 | "#print np.iscomplex(x)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "Q7. Predict the result of the following code." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 18, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [], 216 | "source": [ 217 | "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n", 218 | "#print np.isreal(x)" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Q8. Predict the result of the following code." 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 21, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "#print np.isscalar(3)\n", 237 | "#print np.isscalar([3])\n", 238 | "#print np.isscalar(True)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "## Logical operations" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "Q9. Predict the result of the following code." 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 31, 258 | "metadata": { 259 | "collapsed": false 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "#print np.logical_and([True, False], [False, False])\n", 264 | "#print np.logical_or([True, False, True], [True, False, False])\n", 265 | "#print np.logical_xor([True, False, True], [True, False, False])\n", 266 | "#print np.logical_not([True, False, 0, 1])\n" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "## Comparison" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "Q10. Predict the result of the following code." 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 42, 286 | "metadata": { 287 | "collapsed": false 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "#print np.allclose([3], [2.999999])\n", 292 | "#print np.array_equal([3], [2.999999])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "Q11. Write numpy comparison functions such that they return the results as you see." 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 51, 305 | "metadata": { 306 | "collapsed": false 307 | }, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | "[ True False]\n", 314 | "[ True True]\n", 315 | "[False False]\n", 316 | "[False True]\n" 317 | ] 318 | } 319 | ], 320 | "source": [ 321 | "x = np.array([4, 5])\n", 322 | "y = np.array([2, 5])\n", 323 | "#\n", 324 | "#\n", 325 | "#\n", 326 | "#" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "Q12. Predict the result of the following code." 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 50, 339 | "metadata": { 340 | "collapsed": false 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "#print np.equal([1, 2], [1, 2.000001])\n", 345 | "#print np.isclose([1, 2], [1, 2.000001])" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "metadata": { 352 | "collapsed": true 353 | }, 354 | "outputs": [], 355 | "source": [] 356 | } 357 | ], 358 | "metadata": { 359 | "kernelspec": { 360 | "display_name": "Python 2", 361 | "language": "python", 362 | "name": "python2" 363 | }, 364 | "language_info": { 365 | "codemirror_mode": { 366 | "name": "ipython", 367 | "version": 2 368 | }, 369 | "file_extension": ".py", 370 | "mimetype": "text/x-python", 371 | "name": "python", 372 | "nbconvert_exporter": "python", 373 | "pygments_lexer": "ipython2", 374 | "version": "2.7.12" 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 2 379 | } 380 | -------------------------------------------------------------------------------- /8_Logic_functions_Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Logic functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "'1.11.2'" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "np.__version__" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Truth value testing\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "True\n", 69 | "False\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "x = np.array([1,2,3])\n", 75 | "print np.all(x)\n", 76 | "\n", 77 | "x = np.array([1,0,3])\n", 78 | "print np.all(x)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "True\n", 100 | "False\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "x = np.array([1,0,0])\n", 106 | "print np.any(x)\n", 107 | "\n", 108 | "x = np.array([0,0,0])\n", 109 | "print np.any(x)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## Array contents\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Q3. Predict the result of the following code." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 8, 129 | "metadata": { 130 | "collapsed": false 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "x = np.array([1, 0, np.nan, np.inf])\n", 135 | "#print np.isfinite(x)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "Q4. Predict the result of the following code." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 10, 148 | "metadata": { 149 | "collapsed": false 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "x = np.array([1, 0, np.nan, np.inf])\n", 154 | "#print np.isinf(x)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Q5. Predict the result of the following code." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 12, 167 | "metadata": { 168 | "collapsed": true 169 | }, 170 | "outputs": [], 171 | "source": [ 172 | "x = np.array([1, 0, np.nan, np.inf])\n", 173 | "#print np.isnan(x)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "## Array type testing" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Q6. Predict the result of the following code." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 15, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n", 199 | "#print np.iscomplex(x)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "Q7. Predict the result of the following code." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 18, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [], 216 | "source": [ 217 | "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n", 218 | "#print np.isreal(x)" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Q8. Predict the result of the following code." 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 21, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "#print np.isscalar(3)\n", 237 | "#print np.isscalar([3])\n", 238 | "#print np.isscalar(True)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "## Logical operations" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "Q9. Predict the result of the following code." 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 31, 258 | "metadata": { 259 | "collapsed": false 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "#print np.logical_and([True, False], [False, False])\n", 264 | "#print np.logical_or([True, False, True], [True, False, False])\n", 265 | "#print np.logical_xor([True, False, True], [True, False, False])\n", 266 | "#print np.logical_not([True, False, 0, 1])\n" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "## Comparison" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "Q10. Predict the result of the following code." 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 42, 286 | "metadata": { 287 | "collapsed": false 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "#print np.allclose([3], [2.999999])\n", 292 | "#print np.array_equal([3], [2.999999])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "Q11. Write numpy comparison functions such that they return the results as you see." 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 51, 305 | "metadata": { 306 | "collapsed": false 307 | }, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | "[ True False]\n", 314 | "[ True True]\n", 315 | "[False False]\n", 316 | "[False True]\n" 317 | ] 318 | } 319 | ], 320 | "source": [ 321 | "x = np.array([4, 5])\n", 322 | "y = np.array([2, 5])\n", 323 | "print np.greater(x, y)\n", 324 | "print np.greater_equal(x, y)\n", 325 | "print np.less(x, y)\n", 326 | "print np.less_equal(x, y)\n" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "Q12. Predict the result of the following code." 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 50, 339 | "metadata": { 340 | "collapsed": false 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "#print np.equal([1, 2], [1, 2.000001])\n", 345 | "#print np.isclose([1, 2], [1, 2.000001])" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "metadata": { 352 | "collapsed": true 353 | }, 354 | "outputs": [], 355 | "source": [] 356 | } 357 | ], 358 | "metadata": { 359 | "kernelspec": { 360 | "display_name": "Python 2", 361 | "language": "python", 362 | "name": "python2" 363 | }, 364 | "language_info": { 365 | "codemirror_mode": { 366 | "name": "ipython", 367 | "version": 2 368 | }, 369 | "file_extension": ".py", 370 | "mimetype": "text/x-python", 371 | "name": "python", 372 | "nbconvert_exporter": "python", 373 | "pygments_lexer": "ipython2", 374 | "version": "2.7.12" 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 2 379 | } 380 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Kyubyong Park 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NumPy Exercises 2 | 3 | In numerical computing in python, NumPy is essential. I'm writing simple (a few lines for each problem) but hopefully helpful exercises based on each of numpy's functions. The outline will be as follows. 4 | 5 | * Array creation routines (DONE) 6 | * Array manipulation routines (DONE) 7 | * String operations (DONE) 8 | * Numpy-specific help functions (DONE) 9 | * Input and output (DONE) 10 | * Linear algebra (DONE) 11 | * Discrete Fourier Transform (DONE) 12 | * Logic functions (DONE) 13 | * Mathematical functions (DONE) 14 | * Random sampling (numpy.random) (DONE) 15 | * Set routines (DONE) 16 | * Sorting, searching, and counting (DONE) 17 | * Statistics (DONE) 18 | 19 | Nov., 2016 20 | Kyubyong 21 | --------------------------------------------------------------------------------