0% found this document useful (0 votes)
2 views8 pages

Numpy_TE2D6

Uploaded by

caue.dantas2000
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)
2 views8 pages

Numpy_TE2D6

Uploaded by

caue.dantas2000
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/ 8

Numpy_TE2D6

February 28, 2024

[1]: import numpy as np

[2]: meu_array_numpy = np.arange(1000000)


minha_lista = list(range(1000000))

[3]: %time for _ in range(10):meu_array_numpy2 = meu_array_numpy*2

CPU times: user 40.8 ms, sys: 6.8 ms, total: 47.6 ms
Wall time: 42.8 ms

[4]: %time for _ in range(10):minha_lista2 = [x*2 for x in minha_lista]

CPU times: user 523 ms, sys: 113 ms, total: 636 ms
Wall time: 633 ms

[5]: 633/43

[5]: 14.720930232558139

[10]: data = np.random.rand(2,3)

[11]: data

[11]: array([[0.33536433, 0.20099355, 0.93921757],


[0.28255456, 0.0647105 , 0.98367486]])

[12]: data * 2

[12]: array([[0.67072866, 0.4019871 , 1.87843514],


[0.56510912, 0.12942101, 1.96734971]])

[13]: data/data

[13]: array([[1., 1., 1.],


[1., 1., 1.]])

[14]: 1/data

[14]: array([[ 2.98183172, 4.97528401, 1.06471603],


[ 3.53913947, 15.45344155, 1.01659608]])

1
[15]: data + data

[15]: array([[0.67072866, 0.4019871 , 1.87843514],


[0.56510912, 0.12942101, 1.96734971]])

[16]: data**3

[16]: array([[3.77181695e-02, 8.11981937e-03, 8.28511660e-01],


[2.25583310e-02, 2.70971964e-04, 9.51819750e-01]])

[17]: 1/0

---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[17], line 1
----> 1 1/0

ZeroDivisionError: division by zero

[18]: data+0.1

[18]: array([[0.43536433, 0.30099355, 1.03921757],


[0.38255456, 0.1647105 , 1.08367486]])

[19]: data.shape

[19]: (2, 3)

[20]: data.dtype

[20]: dtype('float64')

[21]: lista3=[6, 7.2, 8, 10]

[22]: array = np.array(lista3)

[23]: array

[23]: array([ 6. , 7.2, 8. , 10. ])

[24]: array.shape

[24]: (4,)

[26]: array.dtype

[26]: dtype('float64')

2
[27]: lista4=[[6, 7.2, 8, 10],[3, 4.3, 6, 1]]

[28]: array2 = np.array(lista4)

[29]: array2

[29]: array([[ 6. , 7.2, 8. , 10. ],


[ 3. , 4.3, 6. , 1. ]])

[30]: array2.shape

[30]: (2, 4)

[31]: array2.dtype

[31]: dtype('float64')

[32]: array.ndim

[32]: 1

[33]: array2.ndim

[33]: 2

Funções de numpy para criar arrays


zeros
ones
empty
eye

[34]: np.zeros(6)

[34]: array([0., 0., 0., 0., 0., 0.])

[35]: np.zeros((3,6))

[35]: array([[0., 0., 0., 0., 0., 0.],


[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])

[36]: np.ones(5)

[36]: array([1., 1., 1., 1., 1.])

[38]: np.ones((3,3,3))

3
[38]: 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.]]])

[39]: np.empty((3,3))

[39]: array([[1., 1., 1.],


[1., 1., 1.],
[1., 1., 1.]])

[40]: np.empty((5,5,5))

[40]: array([[[2.47124349e-316, 0.00000000e+000, 4.67386101e-321,


nan, 6.94074174e-310],
[0.00000000e+000, 2.07458463e+026, 9.08367229e+223,
1.69375010e+190, 2.87453085e+291],
[3.14405836e+233, 8.54265717e+257, 6.99177894e+228,
8.24242019e+160, 1.26829177e+017],
[5.79530389e+199, 7.47730573e+247, 4.44389100e+252,
1.84841356e-004, 4.24515351e+175],
[3.33747054e+257, 5.42791239e-109, 6.99177729e+228,
4.78206908e+180, 8.23560879e+160]],

[[1.09060199e-114, 6.99177794e+228, 3.64393095e+228,


1.06303599e+224, 6.97873719e+228],
[1.94933351e+243, 2.65648976e+233, 1.14131281e-114,
3.93266695e-028, 8.89501052e+252],
[4.69598393e+257, 1.67751539e+243, 7.36093992e+223,
2.86121962e+242, 2.28898721e+243],
[1.69375270e+190, 2.58634340e+161, 3.18274026e+291,
1.94927295e+243, 8.44747281e+252],
[1.67751539e+243, 1.69375270e+190, 1.86390505e-004,
6.92546575e+252, 6.51629129e-109]],

[[7.54178159e+252, 1.21130561e+199, 6.77202251e-109,


4.31579898e+055, 1.94923879e+243],
[7.27373220e+175, 5.32751130e+233, 4.11941833e+257,
1.87238758e+065, 9.36669223e+218],
[2.88067949e+214, 1.23969134e+224, 4.90605860e-109,

4
3.93258379e-028, 1.86575517e-004],
[4.44390491e+252, 2.21203814e+214, 2.71395812e-109,
1.23994566e+290, 8.55447801e+092],
[2.02646857e+267, 1.23994566e+290, 3.93266657e-028,
2.64141742e+233, 9.08367203e+223]],

[[1.82676272e+291, 9.08366793e+223, 3.00454355e+243,


5.93160396e+228, 1.67751539e+243],
[4.30993815e-114, 4.82337451e+228, 4.83497261e+185,
2.85338320e+242, 2.28690020e+243],
[7.35423705e+223, 1.12319906e+219, 6.99177767e+228,
7.35423705e+223, 3.93263240e-028],
[1.84841356e-004, 1.47193364e+224, 1.14445753e+243,
1.56004482e+277, 9.90028080e-114],
[2.87453085e+291, 3.11779745e-109, 1.85174564e-004,
1.17143213e+200, 1.85623318e-004]],

[[1.35792338e+200, 6.33083394e+233, 4.90605860e-109,


1.86390505e-004, 1.34520189e+228],
[1.67751539e+243, 1.86127195e-004, 2.06893869e+026,
1.85176317e-004, 9.48749678e+222],
[5.18293760e-110, 5.46377301e+242, 2.07496271e+026,
9.25079923e+242, 1.96897974e+248],
[7.35423705e+223, 6.66126215e-115, 4.26138717e+257,
4.90605860e-109, 1.06301026e+224],
[3.87206523e+228, 4.90605860e-109, 2.48636862e+262,
1.94932375e+243, 1.44736531e-319]]])

[41]: np.eye(6)

[41]: array([[1., 0., 0., 0., 0., 0.],


[0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 1.]])

[42]: np.arange(10)

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

[43]: np.arange(0,10,2)

[43]: array([0, 2, 4, 6, 8])

[48]: array4 = np.array([1,2,3,4,5],dtype=np.int16)

[49]: array4

5
[49]: array([1, 2, 3, 4, 5], dtype=int16)

[50]: array4.dtype

[50]: dtype('int16')

[51]: array5 = np.array([1,2,3,4,5],dtype=np.float32)

[52]: array5

[52]: array([1., 2., 3., 4., 5.], dtype=float32)

[53]: array5.dtype

[53]: dtype('float32')

[54]: array6 = np.array([1, 2, 3, 4, 5])

[55]: array6.dtype

[55]: dtype('int64')

[56]: float_array6 = array6.astype(np.float32)

[57]: float_array6.dtype

[57]: dtype('float32')

[58]: array6 = np.array([-3.1, 3.2, 4.73, 4, 5])

[59]: array6.dtype

[59]: dtype('float64')

[60]: int_array6=array6.astype(np.int32)

[61]: int_array6

[61]: array([-3, 3, 4, 4, 5], dtype=int32)

[62]: array_string = np.array(['1.32', '-6.4', '23'], dtype=np.string_)

[63]: array_string

[63]: array([b'1.32', b'-6.4', b'23'], dtype='|S4')

[64]: array_string.astype(float)

[64]: array([ 1.32, -6.4 , 23. ])

6
[65]: array7 = np.array([[1., 2., 3.], [4., 5., 6.]])

[66]: array7

[66]: array([[1., 2., 3.],


[4., 5., 6.]])

[67]: array7*array7

[67]: array([[ 1., 4., 9.],


[16., 25., 36.]])

[70]: array8=np.array([[0., 4., 1.],[7., 5., 6.]])

[71]: array7 == array8

[71]: array([[False, False, False],


[False, True, True]])

[72]: array7 == array7

[72]: array([[ True, True, True],


[ True, True, True]])

[73]: array7 >= array8

[73]: array([[ True, False, True],


[False, True, True]])

[75]: np.arange(0,30,5)

[75]: array([ 0, 5, 10, 15, 20, 25])

[76]: np.linspace(0,10,5)

[76]: array([ 0. , 2.5, 5. , 7.5, 10. ])

[77]: array9=np.arange(10)

[78]: array9

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

[79]: array9[5]

[79]: 5

[80]: array9[3:6]

7
[80]: array([3, 4, 5])

0.1 Crie dois arrays de numpy com 3 linhas e 4 colunas com numeros aleatórios
em distribuição normal e:
a) compare os valores dos elementos dos dois arrays;
b) faz um fatiamento dos seguintes elementos dos vetores:
1) o primeiro elemento da linha 1 até o terceiro elemento da linha 1;
2) o segundo elemento da linha 1 até o terceiro elemento da linha 3;
3) o primeiro elemento da linha 1 até o segundo elemento da linha 2;
4) o terceiro elemento da linha 2 até o segundo elemento da linha 3;

[ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy