0% found this document useful (0 votes)
91 views9 pages

Untitled10 - Jupyter Notebook

The document shows examples of using NumPy to create arrays from lists of numbers, perform operations on arrays like taking the square root and logarithm, and creating arrays with linspace. Various NumPy array properties and indexing operations are also demonstrated.

Uploaded by

Sakura chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views9 pages

Untitled10 - Jupyter Notebook

The document shows examples of using NumPy to create arrays from lists of numbers, perform operations on arrays like taking the square root and logarithm, and creating arrays with linspace. Various NumPy array properties and indexing operations are also demonstrated.

Uploaded by

Sakura chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

In [1]: In [4]: 20,30.

9])

In [5]: type(a)

import numpy as
np

a=np.array(c[10,

Out[5]: numpy.ndarray
b=np.array(['20',
10,10.9]) b
In [6]:

Out[6]: array(['20', '10', '10.9'], dtype='<U32')


c=np.array((10,20
.5,'70')) c
In [7]:

Out[7]: array(['10', '20.5', '70'], dtype='<U32')


np.arange(1
,10)
In [8]:

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


a=np.arange(1
In [10]: In
,101) a
[11]:

Out[11]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
type(a)
In [12]:

Out[12]: numpy.ndarray
[13]:
len(a)
In

Out[13]: 100
b=np.arange(1,
101,3)
In [15]:
In b
[16]:
Out[16]: array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97,
100])
[17]:
b[3]
In

Out[17]: 10
[18]:
b[-5]
In

Out[18]: 88
b[2:14:2]
In [19]:

Out[19]: array([ 7, 13, 19, 25, 31, 37])


[20]:
b
In

Out[20]: array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97,
100])
b[-10:-1:2
]
In [21]:

Out[21]: array([73, 79, 85, 91, 97])


b[:20:3]
In [22]:

Out[22]: array([ 1, 10, 19, 28, 37, 46, 55])


c=b[::-1]
In [24]:
c
In [25]:

Out[25]: array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1])
l=[b,c,list(range
(1,21))] l
In [26]:

Out[26]: [array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97,
100]),
array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]
In [27]:
c

Out[27]: array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1])
d=list(c)
In [29]:
type(d)
In [30]:

Out[30]: list
[31]:
d
In

Out[31]: [100,
97,
94,
91,
88,
85,
82,
79,
76,
73,
70,
67,
64,
61,
58,
55,
52,
49,
46,
43,
40,
37,
34,
31,
28,
25,
22,
19,
16,
13,
10,
7,
4,
1]
In [32]: d) a
a=np.array(

Out[32]: array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1])
t=(b,c,a)
t
In [33]:

Out[33]: (array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97,
100]),
array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1]),
array([100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 70, 67, 64, 61, 58,
55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4,
1]))
[34]:
t[0]
In

Out[34]: array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97,
100])
001,100) e
In [41]: In [42]:

e=np.arange(100,3

Out[42]: array([ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200,
1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600, 2700, 2800, 2900, 3000])
f=np.sqrt(
In [43]:
e) f
In [44]:

Out[44]: array([10. , 14.14213562, 17.32050808, 20. , 22.36067977, 24.49489743,


26.45751311, 28.28427125, 30. , 31.6227766 , 33.1662479 , 34.64101615,
36.05551275, 37.41657387, 38.72983346, 40. , 41.23105626, 42.42640687,
43.58898944, 44.72135955, 45.82575695, 46.9041576 , 47.95831523,
48.98979486, 50. , 50.99019514, 51.96152423, 52.91502622, 53.85164807,
54.77225575])
In [45]: reroot':f} d
d={'number':e,'squa

Out[45]: {'number': array([ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1 100,
1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200,
2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000]),
'squareroot': array([10. , 14.14213562, 17.32050808, 20. , 22.36 067977,
24.49489743, 26.45751311, 28.28427125, 30. , 31.6227766 , 33.1662479 ,
34.64101615, 36.05551275, 37.41657387, 38.72983346, 40. , 41.23105626,
42.42640687, 43.58898944, 44.72135955, 45.82575695, 46.9041576 ,
47.95831523, 48.98979486, 50. , 50.99019514, 51.96152423, 52.91502622,
53.85164807, 54.77225575])}
d['squareroo
t']
In [46]:

Out[46]: array([10. , 14.14213562, 17.32050808, 20. , 22.36067977, 24.49489743,


26.45751311, 28.28427125, 30. , 31.6227766 , 33.1662479 , 34.64101615,
36.05551275, 37.41657387, 38.72983346, 40. , 41.23105626, 42.42640687,
43.58898944, 44.72135955, 45.82575695, 46.9041576 , 47.95831523,
48.98979486, 50. , 50.99019514, 51.96152423, 52.91502622, 53.85164807,
54.77225575])
math
math.sqrt(20
In [51]:
.0)
l=[10,20.8,3
0] import

Out[51]: 4.47213595499958
for i in l:
In [52]: In
print(math.log(
i))

2.3025850929940
46
3.0349529867072
724
3.4011973816621
[54]: 555

Out[54]: array([ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200,
1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600, 2700, 2800, 2900, 3000])
np.log(e
)
In [55]:

Out[55]: array([4.60517019, 5.29831737, 5.70378247, 5.99146455, 6.2146081 ,


6.39692966, 6.55108034, 6.68461173, 6.80239476, 6.90775528,
7.00306546, 7.09007684, 7.17011954, 7.24422752, 7.31322039,
7.37775891, 7.43838353, 7.49554194, 7.54960917, 7.60090246,
7.64969262, 7.69621264, 7.7406644 , 7.78322402, 7.82404601,
7.86326672, 7.90100705, 7.9373747 , 7.97246602, 8.00636757])
In [63]: ,30,30) a
a=np.linspace(1

Out[63]: array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.,
15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28.,
29., 30.])
a[1]-a[0]
In [59]:

Out[59]: 0.4736842105263157
a[2]-a[1]
In [60]:

Out[60]: 0.4736842105263157
c=np.array([[2,4,7,10],[5,6,10,60],[10
0,200,300,90],[20,10,20,30]]) c
In [77]:

Out[77]: array([[ 2, 4, 7, 10], [ 5,


6, 10, 60],
[100, 200, 300, 90],
[ 20, 10, 20, 30]])
[72]:
c.ndim
In

Out[72]: 2
c.shape
In [73]:

Out[73]: (3, 4)
[74]:
c.size
In

Out[74]: 12
t=np.arange(1
In [79]: In
,65) t
[80]:

Out[80]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64])
In [90]: 8,8) g
g=t.reshape(

Out[90]: array([[ 1, 2, 3, 4, 5, 6, 7, 8], [ 9,


10, 11, 12, 13, 14, 15, 16], [17, 18,
19, 20, 21, 22, 23, 24], [25, 26, 27,
28, 29, 30, 31, 32], [33, 34, 35, 36,
37, 38, 39, 40], [41, 42, 43, 44, 45,
46, 47, 48], [49, 50, 51, 52, 53, 54,
55, 56], [57, 58, 59, 60, 61, 62, 63,
64]])
g
g[4][5]
In [87]:

Out[87]: 38
g[-4][-3]
In [88]:

Out[88]: 38
g[0:3,0:3
]
In [89]:

Out[89]: array([[ 1, 2, 3],


[ 9, 10, 11],
[17, 18, 19]])
[91]:
g
In

Out[91]: array([[ 1, 2, 3, 4, 5, 6, 7, 8], [ 9,


10, 11, 12, 13, 14, 15, 16], [17, 18,
19, 20, 21, 22, 23, 24], [25, 26, 27,
28, 29, 30, 31, 32], [33, 34, 35, 36,
37, 38, 39, 40], [41, 42, 43, 44, 45,
46, 47, 48], [49, 50, 51, 52, 53, 54,
55, 56], [57, 58, 59, 60, 61, 62, 63,
64]])
g[-5:-1,-7:
-4]
In [93]:

Out[93]: array([[26, 27, 28],


[34, 35, 36],
[42, 43, 44],
[50, 51, 52]])
h=g[0:7:2,0:
7:2]
In [95]:
In h
[96]:

Out[96]: array([[ 1, 3, 5, 7],


[17, 19, 21, 23],
[33, 35, 37, 39],
[49, 51, 53, 55]])
[97]:
h.ndim
In

Out[97]: 2
h.shape
In [98]:

Out[98]: (4, 4)
[99]:
h.size
In

Out[99]: 16
l=[g,h]
l
In
[100]:

Out[100]: [array([[ 1, 2, 3, 4, 5, 6, 7, 8], [ 9,


10, 11, 12, 13, 14, 15, 16], [17, 18, 19,
20, 21, 22, 23, 24], [25, 26, 27, 28, 29,
30, 31, 32], [33, 34, 35, 36, 37, 38, 39,
40], [41, 42, 43, 44, 45, 46, 47, 48],
[49, 50, 51, 52, 53, 54, 55, 56], [57,
58, 59, 60, 61, 62, 63, 64]]),
array([[ 1, 3, 5, 7],
[17, 19, 21, 23],
[33, 35, 37, 39],
[49, 51, 53, 55]])]
[101]:
l[1]
In

Out[101]: array([[ 1, 3, 5, 7],


[17, 19, 21, 23],
[33, 35, 37, 39],
[49, 51, 53, 55]])
In l[0]
[102]:

Out[102]: array([[ 1, 2, 3, 4, 5, 6, 7, 8], [ 9,


10, 11, 12, 13, 14, 15, 16], [17, 18,
19, 20, 21, 22, 23, 24], [25, 26, 27,
28, 29, 30, 31, 32], [33, 34, 35, 36,
37, 38, 39, 40], [41, 42, 43, 44, 45,
46, 47, 48], [49, 50, 51, 52, 53, 54,
55, 56], [57, 58, 59, 60, 61, 62, 63,
64]])
matrix':g,'matrix
piece':h} d
In [103]:
d={'original

Out[103]: {'original matrix': array([[ 1, 2, 3, 4, 5, 6, 7, 8], [ 9,


10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 31, 32],
[33, 34, 35, 36, 37, 38, 39, 40],
[41, 42, 43, 44, 45, 46, 47, 48],
[49, 50, 51, 52, 53, 54, 55, 56],
[57, 58, 59, 60, 61, 62, 63, 64]]),
'matrix piece': array([[ 1, 3, 5, 7],
[17, 19, 21, 23],
[33, 35, 37, 39],
[49, 51, 53, 55]])}
[104]:
len(d)
In

Out[104]: 2

In [ ]:

You might also like