{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 1\n", "Import the NumPy library under the alias `np`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2\n", "Create a NumPy array called `my_array` that contains the following elements: `1`, `3`, and `5`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "my_array = np.array([1, 3, 5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 3\n", "Create a two-dimensional NumPy array with 9 elements. The array should be called `my_matrix` and should have 3 columns and three rows. The matrix can contain whatever values you'd like. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "my_matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 4\n", "Use NumPy's `arange` method to generate the following output:\n", "\n", "`array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20])`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(0, 21)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 5\n", "Use NumPy's `arange` method to generate the following output:\n", "\n", "`array([ 1, 4, 7, 10, 13])`\n", "\n", "Hint: You will need to use `arange`'s third argument." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 7, 10, 13])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,15,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 6\n", "Generate a NumPy array that contains 20 zeros." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0.])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 7\n", "Generate a NumPy array that contains 50 ones." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n", " 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n", " 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 8\n", "Using NumPy, divide the space between 0 and 100 into 1000 even intervals." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.1001001 , 0.2002002 , 0.3003003 ,\n", " 0.4004004 , 0.5005005 , 0.6006006 , 0.7007007 ,\n", " 0.8008008 , 0.9009009 , 1.001001 , 1.1011011 ,\n", " 1.2012012 , 1.3013013 , 1.4014014 , 1.5015015 ,\n", " 1.6016016 , 1.7017017 , 1.8018018 , 1.9019019 ,\n", " 2.002002 , 2.1021021 , 2.2022022 , 2.3023023 ,\n", " 2.4024024 , 2.5025025 , 2.6026026 , 2.7027027 ,\n", " 2.8028028 , 2.9029029 , 3.003003 , 3.1031031 ,\n", " 3.2032032 , 3.3033033 , 3.4034034 , 3.5035035 ,\n", " 3.6036036 , 3.7037037 , 3.8038038 , 3.9039039 ,\n", " 4.004004 , 4.1041041 , 4.2042042 , 4.3043043 ,\n", " 4.4044044 , 4.5045045 , 4.6046046 , 4.7047047 ,\n", " 4.8048048 , 4.9049049 , 5.00500501, 5.10510511,\n", " 5.20520521, 5.30530531, 5.40540541, 5.50550551,\n", " 5.60560561, 5.70570571, 5.80580581, 5.90590591,\n", " 6.00600601, 6.10610611, 6.20620621, 6.30630631,\n", " 6.40640641, 6.50650651, 6.60660661, 6.70670671,\n", " 6.80680681, 6.90690691, 7.00700701, 7.10710711,\n", " 7.20720721, 7.30730731, 7.40740741, 7.50750751,\n", " 7.60760761, 7.70770771, 7.80780781, 7.90790791,\n", " 8.00800801, 8.10810811, 8.20820821, 8.30830831,\n", " 8.40840841, 8.50850851, 8.60860861, 8.70870871,\n", " 8.80880881, 8.90890891, 9.00900901, 9.10910911,\n", " 9.20920921, 9.30930931, 9.40940941, 9.50950951,\n", " 9.60960961, 9.70970971, 9.80980981, 9.90990991,\n", " 10.01001001, 10.11011011, 10.21021021, 10.31031031,\n", " 10.41041041, 10.51051051, 10.61061061, 10.71071071,\n", " 10.81081081, 10.91091091, 11.01101101, 11.11111111,\n", " 11.21121121, 11.31131131, 11.41141141, 11.51151151,\n", " 11.61161161, 11.71171171, 11.81181181, 11.91191191,\n", " 12.01201201, 12.11211211, 12.21221221, 12.31231231,\n", " 12.41241241, 12.51251251, 12.61261261, 12.71271271,\n", " 12.81281281, 12.91291291, 13.01301301, 13.11311311,\n", " 13.21321321, 13.31331331, 13.41341341, 13.51351351,\n", " 13.61361361, 13.71371371, 13.81381381, 13.91391391,\n", " 14.01401401, 14.11411411, 14.21421421, 14.31431431,\n", " 14.41441441, 14.51451451, 14.61461461, 14.71471471,\n", " 14.81481481, 14.91491491, 15.01501502, 15.11511512,\n", " 15.21521522, 15.31531532, 15.41541542, 15.51551552,\n", " 15.61561562, 15.71571572, 15.81581582, 15.91591592,\n", " 16.01601602, 16.11611612, 16.21621622, 16.31631632,\n", " 16.41641642, 16.51651652, 16.61661662, 16.71671672,\n", " 16.81681682, 16.91691692, 17.01701702, 17.11711712,\n", " 17.21721722, 17.31731732, 17.41741742, 17.51751752,\n", " 17.61761762, 17.71771772, 17.81781782, 17.91791792,\n", " 18.01801802, 18.11811812, 18.21821822, 18.31831832,\n", " 18.41841842, 18.51851852, 18.61861862, 18.71871872,\n", " 18.81881882, 18.91891892, 19.01901902, 19.11911912,\n", " 19.21921922, 19.31931932, 19.41941942, 19.51951952,\n", " 19.61961962, 19.71971972, 19.81981982, 19.91991992,\n", " 20.02002002, 20.12012012, 20.22022022, 20.32032032,\n", " 20.42042042, 20.52052052, 20.62062062, 20.72072072,\n", " 20.82082082, 20.92092092, 21.02102102, 21.12112112,\n", " 21.22122122, 21.32132132, 21.42142142, 21.52152152,\n", " 21.62162162, 21.72172172, 21.82182182, 21.92192192,\n", " 22.02202202, 22.12212212, 22.22222222, 22.32232232,\n", " 22.42242242, 22.52252252, 22.62262262, 22.72272272,\n", " 22.82282282, 22.92292292, 23.02302302, 23.12312312,\n", " 23.22322322, 23.32332332, 23.42342342, 23.52352352,\n", " 23.62362362, 23.72372372, 23.82382382, 23.92392392,\n", " 24.02402402, 24.12412412, 24.22422422, 24.32432432,\n", " 24.42442442, 24.52452452, 24.62462462, 24.72472472,\n", " 24.82482482, 24.92492492, 25.02502503, 25.12512513,\n", " 25.22522523, 25.32532533, 25.42542543, 25.52552553,\n", " 25.62562563, 25.72572573, 25.82582583, 25.92592593,\n", " 26.02602603, 26.12612613, 26.22622623, 26.32632633,\n", " 26.42642643, 26.52652653, 26.62662663, 26.72672673,\n", " 26.82682683, 26.92692693, 27.02702703, 27.12712713,\n", " 27.22722723, 27.32732733, 27.42742743, 27.52752753,\n", " 27.62762763, 27.72772773, 27.82782783, 27.92792793,\n", " 28.02802803, 28.12812813, 28.22822823, 28.32832833,\n", " 28.42842843, 28.52852853, 28.62862863, 28.72872873,\n", " 28.82882883, 28.92892893, 29.02902903, 29.12912913,\n", " 29.22922923, 29.32932933, 29.42942943, 29.52952953,\n", " 29.62962963, 29.72972973, 29.82982983, 29.92992993,\n", " 30.03003003, 30.13013013, 30.23023023, 30.33033033,\n", " 30.43043043, 30.53053053, 30.63063063, 30.73073073,\n", " 30.83083083, 30.93093093, 31.03103103, 31.13113113,\n", " 31.23123123, 31.33133133, 31.43143143, 31.53153153,\n", " 31.63163163, 31.73173173, 31.83183183, 31.93193193,\n", " 32.03203203, 32.13213213, 32.23223223, 32.33233233,\n", " 32.43243243, 32.53253253, 32.63263263, 32.73273273,\n", " 32.83283283, 32.93293293, 33.03303303, 33.13313313,\n", " 33.23323323, 33.33333333, 33.43343343, 33.53353353,\n", " 33.63363363, 33.73373373, 33.83383383, 33.93393393,\n", " 34.03403403, 34.13413413, 34.23423423, 34.33433433,\n", " 34.43443443, 34.53453453, 34.63463463, 34.73473473,\n", " 34.83483483, 34.93493493, 35.03503504, 35.13513514,\n", " 35.23523524, 35.33533534, 35.43543544, 35.53553554,\n", " 35.63563564, 35.73573574, 35.83583584, 35.93593594,\n", " 36.03603604, 36.13613614, 36.23623624, 36.33633634,\n", " 36.43643644, 36.53653654, 36.63663664, 36.73673674,\n", " 36.83683684, 36.93693694, 37.03703704, 37.13713714,\n", " 37.23723724, 37.33733734, 37.43743744, 37.53753754,\n", " 37.63763764, 37.73773774, 37.83783784, 37.93793794,\n", " 38.03803804, 38.13813814, 38.23823824, 38.33833834,\n", " 38.43843844, 38.53853854, 38.63863864, 38.73873874,\n", " 38.83883884, 38.93893894, 39.03903904, 39.13913914,\n", " 39.23923924, 39.33933934, 39.43943944, 39.53953954,\n", " 39.63963964, 39.73973974, 39.83983984, 39.93993994,\n", " 40.04004004, 40.14014014, 40.24024024, 40.34034034,\n", " 40.44044044, 40.54054054, 40.64064064, 40.74074074,\n", " 40.84084084, 40.94094094, 41.04104104, 41.14114114,\n", " 41.24124124, 41.34134134, 41.44144144, 41.54154154,\n", " 41.64164164, 41.74174174, 41.84184184, 41.94194194,\n", " 42.04204204, 42.14214214, 42.24224224, 42.34234234,\n", " 42.44244244, 42.54254254, 42.64264264, 42.74274274,\n", " 42.84284284, 42.94294294, 43.04304304, 43.14314314,\n", " 43.24324324, 43.34334334, 43.44344344, 43.54354354,\n", " 43.64364364, 43.74374374, 43.84384384, 43.94394394,\n", " 44.04404404, 44.14414414, 44.24424424, 44.34434434,\n", " 44.44444444, 44.54454454, 44.64464464, 44.74474474,\n", " 44.84484484, 44.94494494, 45.04504505, 45.14514515,\n", " 45.24524525, 45.34534535, 45.44544545, 45.54554555,\n", " 45.64564565, 45.74574575, 45.84584585, 45.94594595,\n", " 46.04604605, 46.14614615, 46.24624625, 46.34634635,\n", " 46.44644645, 46.54654655, 46.64664665, 46.74674675,\n", " 46.84684685, 46.94694695, 47.04704705, 47.14714715,\n", " 47.24724725, 47.34734735, 47.44744745, 47.54754755,\n", " 47.64764765, 47.74774775, 47.84784785, 47.94794795,\n", " 48.04804805, 48.14814815, 48.24824825, 48.34834835,\n", " 48.44844845, 48.54854855, 48.64864865, 48.74874875,\n", " 48.84884885, 48.94894895, 49.04904905, 49.14914915,\n", " 49.24924925, 49.34934935, 49.44944945, 49.54954955,\n", " 49.64964965, 49.74974975, 49.84984985, 49.94994995,\n", " 50.05005005, 50.15015015, 50.25025025, 50.35035035,\n", " 50.45045045, 50.55055055, 50.65065065, 50.75075075,\n", " 50.85085085, 50.95095095, 51.05105105, 51.15115115,\n", " 51.25125125, 51.35135135, 51.45145145, 51.55155155,\n", " 51.65165165, 51.75175175, 51.85185185, 51.95195195,\n", " 52.05205205, 52.15215215, 52.25225225, 52.35235235,\n", " 52.45245245, 52.55255255, 52.65265265, 52.75275275,\n", " 52.85285285, 52.95295295, 53.05305305, 53.15315315,\n", " 53.25325325, 53.35335335, 53.45345345, 53.55355355,\n", " 53.65365365, 53.75375375, 53.85385385, 53.95395395,\n", " 54.05405405, 54.15415415, 54.25425425, 54.35435435,\n", " 54.45445445, 54.55455455, 54.65465465, 54.75475475,\n", " 54.85485485, 54.95495495, 55.05505506, 55.15515516,\n", " 55.25525526, 55.35535536, 55.45545546, 55.55555556,\n", " 55.65565566, 55.75575576, 55.85585586, 55.95595596,\n", " 56.05605606, 56.15615616, 56.25625626, 56.35635636,\n", " 56.45645646, 56.55655656, 56.65665666, 56.75675676,\n", " 56.85685686, 56.95695696, 57.05705706, 57.15715716,\n", " 57.25725726, 57.35735736, 57.45745746, 57.55755756,\n", " 57.65765766, 57.75775776, 57.85785786, 57.95795796,\n", " 58.05805806, 58.15815816, 58.25825826, 58.35835836,\n", " 58.45845846, 58.55855856, 58.65865866, 58.75875876,\n", " 58.85885886, 58.95895896, 59.05905906, 59.15915916,\n", " 59.25925926, 59.35935936, 59.45945946, 59.55955956,\n", " 59.65965966, 59.75975976, 59.85985986, 59.95995996,\n", " 60.06006006, 60.16016016, 60.26026026, 60.36036036,\n", " 60.46046046, 60.56056056, 60.66066066, 60.76076076,\n", " 60.86086086, 60.96096096, 61.06106106, 61.16116116,\n", " 61.26126126, 61.36136136, 61.46146146, 61.56156156,\n", " 61.66166166, 61.76176176, 61.86186186, 61.96196196,\n", " 62.06206206, 62.16216216, 62.26226226, 62.36236236,\n", " 62.46246246, 62.56256256, 62.66266266, 62.76276276,\n", " 62.86286286, 62.96296296, 63.06306306, 63.16316316,\n", " 63.26326326, 63.36336336, 63.46346346, 63.56356356,\n", " 63.66366366, 63.76376376, 63.86386386, 63.96396396,\n", " 64.06406406, 64.16416416, 64.26426426, 64.36436436,\n", " 64.46446446, 64.56456456, 64.66466466, 64.76476476,\n", " 64.86486486, 64.96496496, 65.06506507, 65.16516517,\n", " 65.26526527, 65.36536537, 65.46546547, 65.56556557,\n", " 65.66566567, 65.76576577, 65.86586587, 65.96596597,\n", " 66.06606607, 66.16616617, 66.26626627, 66.36636637,\n", " 66.46646647, 66.56656657, 66.66666667, 66.76676677,\n", " 66.86686687, 66.96696697, 67.06706707, 67.16716717,\n", " 67.26726727, 67.36736737, 67.46746747, 67.56756757,\n", " 67.66766767, 67.76776777, 67.86786787, 67.96796797,\n", " 68.06806807, 68.16816817, 68.26826827, 68.36836837,\n", " 68.46846847, 68.56856857, 68.66866867, 68.76876877,\n", " 68.86886887, 68.96896897, 69.06906907, 69.16916917,\n", " 69.26926927, 69.36936937, 69.46946947, 69.56956957,\n", " 69.66966967, 69.76976977, 69.86986987, 69.96996997,\n", " 70.07007007, 70.17017017, 70.27027027, 70.37037037,\n", " 70.47047047, 70.57057057, 70.67067067, 70.77077077,\n", " 70.87087087, 70.97097097, 71.07107107, 71.17117117,\n", " 71.27127127, 71.37137137, 71.47147147, 71.57157157,\n", " 71.67167167, 71.77177177, 71.87187187, 71.97197197,\n", " 72.07207207, 72.17217217, 72.27227227, 72.37237237,\n", " 72.47247247, 72.57257257, 72.67267267, 72.77277277,\n", " 72.87287287, 72.97297297, 73.07307307, 73.17317317,\n", " 73.27327327, 73.37337337, 73.47347347, 73.57357357,\n", " 73.67367367, 73.77377377, 73.87387387, 73.97397397,\n", " 74.07407407, 74.17417417, 74.27427427, 74.37437437,\n", " 74.47447447, 74.57457457, 74.67467467, 74.77477477,\n", " 74.87487487, 74.97497497, 75.07507508, 75.17517518,\n", " 75.27527528, 75.37537538, 75.47547548, 75.57557558,\n", " 75.67567568, 75.77577578, 75.87587588, 75.97597598,\n", " 76.07607608, 76.17617618, 76.27627628, 76.37637638,\n", " 76.47647648, 76.57657658, 76.67667668, 76.77677678,\n", " 76.87687688, 76.97697698, 77.07707708, 77.17717718,\n", " 77.27727728, 77.37737738, 77.47747748, 77.57757758,\n", " 77.67767768, 77.77777778, 77.87787788, 77.97797798,\n", " 78.07807808, 78.17817818, 78.27827828, 78.37837838,\n", " 78.47847848, 78.57857858, 78.67867868, 78.77877878,\n", " 78.87887888, 78.97897898, 79.07907908, 79.17917918,\n", " 79.27927928, 79.37937938, 79.47947948, 79.57957958,\n", " 79.67967968, 79.77977978, 79.87987988, 79.97997998,\n", " 80.08008008, 80.18018018, 80.28028028, 80.38038038,\n", " 80.48048048, 80.58058058, 80.68068068, 80.78078078,\n", " 80.88088088, 80.98098098, 81.08108108, 81.18118118,\n", " 81.28128128, 81.38138138, 81.48148148, 81.58158158,\n", " 81.68168168, 81.78178178, 81.88188188, 81.98198198,\n", " 82.08208208, 82.18218218, 82.28228228, 82.38238238,\n", " 82.48248248, 82.58258258, 82.68268268, 82.78278278,\n", " 82.88288288, 82.98298298, 83.08308308, 83.18318318,\n", " 83.28328328, 83.38338338, 83.48348348, 83.58358358,\n", " 83.68368368, 83.78378378, 83.88388388, 83.98398398,\n", " 84.08408408, 84.18418418, 84.28428428, 84.38438438,\n", " 84.48448448, 84.58458458, 84.68468468, 84.78478478,\n", " 84.88488488, 84.98498498, 85.08508509, 85.18518519,\n", " 85.28528529, 85.38538539, 85.48548549, 85.58558559,\n", " 85.68568569, 85.78578579, 85.88588589, 85.98598599,\n", " 86.08608609, 86.18618619, 86.28628629, 86.38638639,\n", " 86.48648649, 86.58658659, 86.68668669, 86.78678679,\n", " 86.88688689, 86.98698699, 87.08708709, 87.18718719,\n", " 87.28728729, 87.38738739, 87.48748749, 87.58758759,\n", " 87.68768769, 87.78778779, 87.88788789, 87.98798799,\n", " 88.08808809, 88.18818819, 88.28828829, 88.38838839,\n", " 88.48848849, 88.58858859, 88.68868869, 88.78878879,\n", " 88.88888889, 88.98898899, 89.08908909, 89.18918919,\n", " 89.28928929, 89.38938939, 89.48948949, 89.58958959,\n", " 89.68968969, 89.78978979, 89.88988989, 89.98998999,\n", " 90.09009009, 90.19019019, 90.29029029, 90.39039039,\n", " 90.49049049, 90.59059059, 90.69069069, 90.79079079,\n", " 90.89089089, 90.99099099, 91.09109109, 91.19119119,\n", " 91.29129129, 91.39139139, 91.49149149, 91.59159159,\n", " 91.69169169, 91.79179179, 91.89189189, 91.99199199,\n", " 92.09209209, 92.19219219, 92.29229229, 92.39239239,\n", " 92.49249249, 92.59259259, 92.69269269, 92.79279279,\n", " 92.89289289, 92.99299299, 93.09309309, 93.19319319,\n", " 93.29329329, 93.39339339, 93.49349349, 93.59359359,\n", " 93.69369369, 93.79379379, 93.89389389, 93.99399399,\n", " 94.09409409, 94.19419419, 94.29429429, 94.39439439,\n", " 94.49449449, 94.59459459, 94.69469469, 94.79479479,\n", " 94.89489489, 94.99499499, 95.0950951 , 95.1951952 ,\n", " 95.2952953 , 95.3953954 , 95.4954955 , 95.5955956 ,\n", " 95.6956957 , 95.7957958 , 95.8958959 , 95.995996 ,\n", " 96.0960961 , 96.1961962 , 96.2962963 , 96.3963964 ,\n", " 96.4964965 , 96.5965966 , 96.6966967 , 96.7967968 ,\n", " 96.8968969 , 96.996997 , 97.0970971 , 97.1971972 ,\n", " 97.2972973 , 97.3973974 , 97.4974975 , 97.5975976 ,\n", " 97.6976977 , 97.7977978 , 97.8978979 , 97.997998 ,\n", " 98.0980981 , 98.1981982 , 98.2982983 , 98.3983984 ,\n", " 98.4984985 , 98.5985986 , 98.6986987 , 98.7987988 ,\n", " 98.8988989 , 98.998999 , 99.0990991 , 99.1991992 ,\n", " 99.2992993 , 99.3993994 , 99.4994995 , 99.5995996 ,\n", " 99.6996997 , 99.7997998 , 99.8998999 , 100. ])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0,100,1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 9\n", "Create a 10x10 identity matrix using NumPy." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 10\n", "Returns a random sample of numbers with 10 values where each value is between 0 and 1." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.19170402, 0.55645186, 0.65700129, 0.1648909 , 0.58771596,\n", " 0.78439065, 0.06312085, 0.46611294, 0.57032727, 0.31190655])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 11\n", "Returns a random sample of numbers with 10 values where each value is between 0 and 10.\n", "\n", "Hint: Use the `random.rand` method combined with a multiplication operation." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.80533202, 8.3994994 , 1.57603441, 9.13256339, 9.97815142,\n", " 4.7302086 , 3.80373864, 4.678893 , 0.96672324, 8.55569928])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(10)*10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 12\n", "Generate a random sample of 15 numbers from a normal distribution." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.34623648, 0.31064557, -0.4459493 , -1.40253534, 0.49850674,\n", " -0.29975628, 1.21338415, -0.484629 , -0.4221513 , 1.01339043,\n", " 1.00563727, 0.73421987, -0.62300471, 0.84863864, 0.32504362])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randn(15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 13\n", "Generate a random sample of 7 integers that range between 5 and 10." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 5, 7, 7, 5, 8, 8])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(5, 10, 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 14\n", "Reshape the following one-dimensional NumPy array into a two-dimensional Numpy array with 3 rows and 3 columns." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.array([0,1,2,3,4,5,6,7,8])\n", "arr.reshape(3,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 15\n", "Print the minimum and maximum values of the following NumPy array." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "arr = np.array([0,1,2,3,4,5,6,7,8])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Print the minimum value here.\n", "arr.min()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Print the maximum value here.\n", "arr.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 16\n", "For the following NumPy array, print the index of the minimum and maximum values." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "my_array = np.array([6, 7, 0, 2])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Print the minimum value's index here.\n", "my_array.argmin()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Print the maximum value's index here.\n", "my_array.argmax()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }