• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Aug 22nd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. {
2.  "cells": [
3.   {
4.    "cell_type": "markdown",
6.    "source": [
7.     "# NumPy Exercises \n",
8.     "\n",
9.     "Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks, and then you'll be asked some more complicated questions."
10.    ]
11.   },
12.   {
13.    "cell_type": "markdown",
15.    "source": [
16.     "#### Import NumPy as np"
17.    ]
18.   },
19.   {
20.    "cell_type": "code",
21.    "execution_count": 2,
23.    "outputs": [],
24.    "source": [
25.     "import numpy as np"
26.    ]
27.   },
28.   {
29.    "cell_type": "markdown",
31.    "source": [
32.     "#### Create an array of 10 zeros "
33.    ]
34.   },
35.   {
36.    "cell_type": "code",
37.    "execution_count": 3,
39.     "collapsed": false,
40.     "jupyter": {
41.      "outputs_hidden": false
42.     }
43.    },
44.    "outputs": [
45.     {
46.      "data": {
47.       "text/plain": [
48.        "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
49.       ]
50.      },
51.      "execution_count": 3,
53.      "output_type": "execute_result"
54.     }
55.    ],
56.    "source": [
57.     "np.zeros(10)"
58.    ]
59.   },
60.   {
61.    "cell_type": "markdown",
63.    "source": [
64.     "#### Create an array of 10 ones"
65.    ]
66.   },
67.   {
68.    "cell_type": "code",
69.    "execution_count": 4,
71.    "outputs": [
72.     {
73.      "data": {
74.       "text/plain": [
75.        "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
76.       ]
77.      },
78.      "execution_count": 4,
80.      "output_type": "execute_result"
81.     }
82.    ],
83.    "source": [
84.     "np.ones(10)"
85.    ]
86.   },
87.   {
88.    "cell_type": "markdown",
90.    "source": [
91.     "#### Create an array of 10 fives"
92.    ]
93.   },
94.   {
95.    "cell_type": "code",
96.    "execution_count": 5,
98.    "outputs": [
99.     {
100.      "data": {
101.       "text/plain": [
102.        "array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])"
103.       ]
104.      },
105.      "execution_count": 5,
107.      "output_type": "execute_result"
108.     }
109.    ],
110.    "source": [
111.     "np.ones(10)*5"
112.    ]
113.   },
114.   {
115.    "cell_type": "markdown",
117.    "source": [
118.     "#### Create an array of the integers from 10 to 50"
119.    ]
120.   },
121.   {
122.    "cell_type": "code",
123.    "execution_count": 6,
125.    "outputs": [
126.     {
127.      "data": {
128.       "text/plain": [
129.        "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n",
130.        "       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n",
131.        "       44, 45, 46, 47, 48, 49, 50])"
132.       ]
133.      },
134.      "execution_count": 6,
136.      "output_type": "execute_result"
137.     }
138.    ],
139.    "source": [
140.     "np.arange(10,51)"
141.    ]
142.   },
143.   {
144.    "cell_type": "markdown",
146.    "source": [
147.     "#### Create an array of all the even integers from 10 to 50"
148.    ]
149.   },
150.   {
151.    "cell_type": "code",
152.    "execution_count": 7,
154.     "collapsed": false,
155.     "jupyter": {
156.      "outputs_hidden": false
157.     }
158.    },
159.    "outputs": [
160.     {
161.      "data": {
162.       "text/plain": [
163.        "array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,\n",
164.        "       44, 46, 48, 50])"
165.       ]
166.      },
167.      "execution_count": 7,
169.      "output_type": "execute_result"
170.     }
171.    ],
172.    "source": [
173.     "np.arange(10,51,2)"
174.    ]
175.   },
176.   {
177.    "cell_type": "markdown",
179.    "source": [
180.     "#### Create a 3x3 matrix with values ranging from 0 to 8"
181.    ]
182.   },
183.   {
184.    "cell_type": "code",
185.    "execution_count": 9,
187.    "outputs": [],
188.    "source": [
189.     "a = np.arange(9)"
190.    ]
191.   },
192.   {
193.    "cell_type": "code",
194.    "execution_count": 10,
196.    "outputs": [
197.     {
198.      "data": {
199.       "text/plain": [
200.        "array([0, 1, 2, 3, 4, 5, 6, 7, 8])"
201.       ]
202.      },
203.      "execution_count": 10,
205.      "output_type": "execute_result"
206.     }
207.    ],
208.    "source": [
209.     "a"
210.    ]
211.   },
212.   {
213.    "cell_type": "code",
214.    "execution_count": 11,
216.    "outputs": [
217.     {
218.      "data": {
219.       "text/plain": [
220.        "array([[0, 1, 2],\n",
221.        "       [3, 4, 5],\n",
222.        "       [6, 7, 8]])"
223.       ]
224.      },
225.      "execution_count": 11,
227.      "output_type": "execute_result"
228.     }
229.    ],
230.    "source": [
231.     "a.reshape(3,3)"
232.    ]
233.   },
234.   {
235.    "cell_type": "markdown",
237.    "source": [
238.     "#### Create a 3x3 identity matrix"
239.    ]
240.   },
241.   {
242.    "cell_type": "code",
243.    "execution_count": 12,
245.    "outputs": [
246.     {
247.      "data": {
248.       "text/plain": [
249.        "array([[1., 0., 0.],\n",
250.        "       [0., 1., 0.],\n",
251.        "       [0., 0., 1.]])"
252.       ]
253.      },
254.      "execution_count": 12,
256.      "output_type": "execute_result"
257.     }
258.    ],
259.    "source": [
260.     "np.eye(3)"
261.    ]
262.   },
263.   {
264.    "cell_type": "markdown",
266.    "source": [
267.     "#### Use NumPy to generate a random number between 0 and 1"
268.    ]
269.   },
270.   {
271.    "cell_type": "code",
272.    "execution_count": 13,
274.    "outputs": [
275.     {
276.      "data": {
277.       "text/plain": [
278.        "array([0.61895209])"
279.       ]
280.      },
281.      "execution_count": 13,
283.      "output_type": "execute_result"
284.     }
285.    ],
286.    "source": [
287.     "np.random.rand(1)"
288.    ]
289.   },
290.   {
291.    "cell_type": "markdown",
293.    "source": [
294.     "#### Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution"
295.    ]
296.   },
297.   {
298.    "cell_type": "code",
299.    "execution_count": 14,
301.    "outputs": [
302.     {
303.      "data": {
304.       "text/plain": [
305.        "array([-0.5992264 , -0.07965042,  1.72113865,  0.06624785,  0.32592046,\n",
306.        "       -0.02850312,  0.36569141, -0.61519631,  0.79396004, -0.10803859,\n",
307.        "       -0.61303596, -0.72655377,  1.3243589 , -0.87795404, -0.23940706,\n",
308.        "        0.17175587,  1.15218163, -1.74887861, -1.18665146, -0.50752569,\n",
309.        "       -2.11234357,  1.20751702,  0.01478667,  1.0441076 , -1.11223452])"
310.       ]
311.      },
312.      "execution_count": 14,
314.      "output_type": "execute_result"
315.     }
316.    ],
317.    "source": [
318.     "np.random.randn(25)"
319.    ]
320.   },
321.   {
322.    "cell_type": "markdown",
324.    "source": [
325.     "#### Create the following matrix:"
326.    ]
327.   },
328.   {
329.    "cell_type": "code",
330.    "execution_count": 20,
332.    "outputs": [
333.     {
334.      "data": {
335.       "text/plain": [
336.        "array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],\n",
337.        "       [0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],\n",
338.        "       [0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],\n",
339.        "       [0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],\n",
340.        "       [0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],\n",
341.        "       [0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],\n",
342.        "       [0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],\n",
343.        "       [0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],\n",
344.        "       [0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],\n",
345.        "       [0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.  ]])"
346.       ]
347.      },
348.      "execution_count": 20,
350.      "output_type": "execute_result"
351.     }
352.    ],
353.    "source": [
354.     "np.arange(1,101).reshape(10,10)/100"
355.    ]
356.   },
357.   {
358.    "cell_type": "markdown",
360.    "source": [
361.     "#### Create an array of 20 linearly spaced points between 0 and 1:"
362.    ]
363.   },
364.   {
365.    "cell_type": "code",
366.    "execution_count": 24,
368.    "outputs": [
369.     {
370.      "data": {
371.       "text/plain": [
372.        "array([0.        , 0.05263158, 0.10526316, 0.15789474, 0.21052632,\n",
373.        "       0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,\n",
374.        "       0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,\n",
375.        "       0.78947368, 0.84210526, 0.89473684, 0.94736842, 1.        ])"
376.       ]
377.      },
378.      "execution_count": 24,
380.      "output_type": "execute_result"
381.     }
382.    ],
383.    "source": [
384.     "np.linspace(0,1,20)"
385.    ]
386.   },
387.   {
388.    "cell_type": "markdown",
390.    "source": [
391.     "## Numpy Indexing and Selection\n",
392.     "\n",
393.     "Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:"
394.    ]
395.   },
396.   {
397.    "cell_type": "code",
398.    "execution_count": 25,
400.     "collapsed": false,
401.     "jupyter": {
402.      "outputs_hidden": false
403.     }
404.    },
405.    "outputs": [
406.     {
407.      "data": {
408.       "text/plain": [
409.        "array([[ 1,  2,  3,  4,  5],\n",
410.        "       [ 6,  7,  8,  9, 10],\n",
411.        "       [11, 12, 13, 14, 15],\n",
412.        "       [16, 17, 18, 19, 20],\n",
413.        "       [21, 22, 23, 24, 25]])"
414.       ]
415.      },
416.      "execution_count": 25,
418.      "output_type": "execute_result"
419.     }
420.    ],
421.    "source": [
422.     "mat = np.arange(1,26).reshape(5,5)\n",
423.     "mat"
424.    ]
425.   },
426.   {
427.    "cell_type": "code",
428.    "execution_count": 26,
430.    "outputs": [],
431.    "source": [
432.     "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n",
433.     "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n",
434.     "# BE ABLE TO SEE THE OUTPUT ANY MORE"
435.    ]
436.   },
437.   {
438.    "cell_type": "code",
439.    "execution_count": 27,
441.    "outputs": [
442.     {
443.      "data": {
444.       "text/plain": [
445.        "array([[12, 13, 14, 15],\n",
446.        "       [17, 18, 19, 20],\n",
447.        "       [22, 23, 24, 25]])"
448.       ]
449.      },
450.      "execution_count": 27,
452.      "output_type": "execute_result"
453.     }
454.    ],
455.    "source": [
456.     "mat[2:,1:]"
457.    ]
458.   },
459.   {
460.    "cell_type": "code",
461.    "execution_count": 28,
463.    "outputs": [],
464.    "source": [
465.     "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n",
466.     "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n",
467.     "# BE ABLE TO SEE THE OUTPUT ANY MORE"
468.    ]
469.   },
470.   {
471.    "cell_type": "code",
472.    "execution_count": 29,
474.    "outputs": [
475.     {
476.      "data": {
477.       "text/plain": [
478.        "20"
479.       ]
480.      },
481.      "execution_count": 29,
483.      "output_type": "execute_result"
484.     }
485.    ],
486.    "source": [
487.     "mat[3,4]"
488.    ]
489.   },
490.   {
491.    "cell_type": "code",
492.    "execution_count": 30,
494.    "outputs": [],
495.    "source": [
496.     "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n",
497.     "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n",
498.     "# BE ABLE TO SEE THE OUTPUT ANY MORE"
499.    ]
500.   },
501.   {
502.    "cell_type": "code",
503.    "execution_count": 35,
505.    "outputs": [
506.     {
507.      "data": {
508.       "text/plain": [
509.        "array([[ 2],\n",
510.        "       [ 7],\n",
511.        "       [12]])"
512.       ]
513.      },
514.      "execution_count": 35,
516.      "output_type": "execute_result"
517.     }
518.    ],
519.    "source": [
520.     "mat[:3,1:2]"
521.    ]
522.   },
523.   {
524.    "cell_type": "code",
525.    "execution_count": 36,
527.    "outputs": [],
528.    "source": [
529.     "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n",
530.     "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n",
531.     "# BE ABLE TO SEE THE OUTPUT ANY MORE"
532.    ]
533.   },
534.   {
535.    "cell_type": "code",
536.    "execution_count": 37,
538.    "outputs": [
539.     {
540.      "data": {
541.       "text/plain": [
542.        "array([21, 22, 23, 24, 25])"
543.       ]
544.      },
545.      "execution_count": 37,
547.      "output_type": "execute_result"
548.     }
549.    ],
550.    "source": [
551.     "mat[4]"
552.    ]
553.   },
554.   {
555.    "cell_type": "code",
556.    "execution_count": 38,
558.    "outputs": [],
559.    "source": [
560.     "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n",
561.     "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n",
562.     "# BE ABLE TO SEE THE OUTPUT ANY MORE"
563.    ]
564.   },
565.   {
566.    "cell_type": "code",
567.    "execution_count": 40,
569.    "outputs": [
570.     {
571.      "data": {
572.       "text/plain": [
573.        "array([[16, 17, 18, 19, 20],\n",
574.        "       [21, 22, 23, 24, 25]])"
575.       ]
576.      },
577.      "execution_count": 40,
579.      "output_type": "execute_result"
580.     }
581.    ],
582.    "source": [
583.     "mat[3:]"
584.    ]
585.   },
586.   {
587.    "cell_type": "markdown",
589.    "source": [
590.     "#### Get the sum of all the values in mat"
591.    ]
592.   },
593.   {
594.    "cell_type": "code",
595.    "execution_count": 41,
597.    "outputs": [
598.     {
599.      "data": {
600.       "text/plain": [
601.        "325"
602.       ]
603.      },
604.      "execution_count": 41,
606.      "output_type": "execute_result"
607.     }
608.    ],
609.    "source": [
610.     "mat.sum()"
611.    ]
612.   },
613.   {
614.    "cell_type": "markdown",
616.    "source": [
617.     "#### Get the standard deviation of the values in mat"
618.    ]
619.   },
620.   {
621.    "cell_type": "code",
622.    "execution_count": 42,
624.    "outputs": [
625.     {
626.      "data": {
627.       "text/plain": [
628.        "7.211102550927978"
629.       ]
630.      },
631.      "execution_count": 42,
633.      "output_type": "execute_result"
634.     }
635.    ],
636.    "source": [
637.     "mat.std()"
638.    ]
639.   },
640.   {
641.    "cell_type": "markdown",
643.    "source": [
644.     "#### Get the sum of all the columns in mat"
645.    ]
646.   },
647.   {
648.    "cell_type": "code",
649.    "execution_count": 44,
651.    "outputs": [
652.     {
653.      "data": {
654.       "text/plain": [
655.        "array([55, 60, 65, 70, 75])"
656.       ]
657.      },
658.      "execution_count": 44,
660.      "output_type": "execute_result"
661.     }
662.    ],
663.    "source": [
664.     "mat.sum(0)"
665.    ]
666.   }
667.  ],
669.   "kernelspec": {
670.    "display_name": "Python",
671.    "language": "python",
672.    "name": "conda-env-python-py"
673.   },
674.   "language_info": {
675.    "codemirror_mode": {
676.     "name": "ipython",
677.     "version": 3
678.    },
679.    "file_extension": ".py",
680.    "mimetype": "text/x-python",
681.    "name": "python",
682.    "nbconvert_exporter": "python",
683.    "pygments_lexer": "ipython3",
684.    "version": "3.6.7"
685.   }
686.  },
687.  "nbformat": 4,
688.  "nbformat_minor": 4
689. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top