# Problem 1¶

Import the NumPy library under the alias np.

In [1]:
import numpy as np


# Problem 2¶

Create a NumPy array called my_array that contains the following elements: 1, 3, and 5.

In [2]:
my_array = np.array([1, 3, 5])


# Problem 3¶

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.

In [3]:
my_matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])


# Problem 4¶

Use NumPy's arange method to generate the following output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

In [4]:
np.arange(0, 21)

Out[4]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20])

# Problem 5¶

Use NumPy's arange method to generate the following output:

array([ 1, 4, 7, 10, 13])

Hint: You will need to use arange's third argument.

In [5]:
np.arange(1,15,3)

Out[5]:
array([ 1,  4,  7, 10, 13])

# Problem 6¶

Generate a NumPy array that contains 20 zeros.

In [6]:
np.zeros(20)

Out[6]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0.])

# Problem 7¶

Generate a NumPy array that contains 50 ones.

In [7]:
np.ones(50)

Out[7]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

# Problem 8¶

Using NumPy, divide the space between 0 and 100 into 1000 even intervals.

In [8]:
np.linspace(0,100,1000)

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

# Problem 9¶

Create a 10x10 identity matrix using NumPy.

In [9]:
np.eye(10)

Out[9]:
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

# Problem 10¶

Returns a random sample of numbers with 10 values where each value is between 0 and 1.

In [10]:
np.random.rand(10)

Out[10]:
array([0.19170402, 0.55645186, 0.65700129, 0.1648909 , 0.58771596,
0.78439065, 0.06312085, 0.46611294, 0.57032727, 0.31190655])

# Problem 11¶

Returns a random sample of numbers with 10 values where each value is between 0 and 10.

Hint: Use the random.rand method combined with a multiplication operation.

In [11]:
np.random.rand(10)*10

Out[11]:
array([1.80533202, 8.3994994 , 1.57603441, 9.13256339, 9.97815142,
4.7302086 , 3.80373864, 4.678893  , 0.96672324, 8.55569928])

# Problem 12¶

Generate a random sample of 15 numbers from a normal distribution.

In [12]:
np.random.randn(15)

Out[12]:
array([-0.34623648,  0.31064557, -0.4459493 , -1.40253534,  0.49850674,
-0.29975628,  1.21338415, -0.484629  , -0.4221513 ,  1.01339043,
1.00563727,  0.73421987, -0.62300471,  0.84863864,  0.32504362])

# Problem 13¶

Generate a random sample of 7 integers that range between 5 and 10.

In [13]:
np.random.randint(5, 10, 7)

Out[13]:
array([6, 5, 7, 7, 5, 8, 8])

# Problem 14¶

Reshape the following one-dimensional NumPy array into a two-dimensional Numpy array with 3 rows and 3 columns.

In [14]:
arr = np.array([0,1,2,3,4,5,6,7,8])
arr.reshape(3,3)

Out[14]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# Problem 15¶

Print the minimum and maximum values of the following NumPy array.

In [15]:
arr = np.array([0,1,2,3,4,5,6,7,8])

In [16]:
#Print the minimum value here.
arr.min()

Out[16]:
0
In [17]:
#Print the maximum value here.
arr.max()

Out[17]:
8

# Problem 16¶

For the following NumPy array, print the index of the minimum and maximum values.

In [18]:
my_array = np.array([6, 7, 0, 2])

In [19]:
#Print the minimum value's index here.
my_array.argmin()

Out[19]:
2
In [20]:
#Print the maximum value's index here.
my_array.argmax()

Out[20]:
1