In [1]:
range(10)
Out[1]:
range(0, 10)
In [2]:
[24, 25, 10]
Out[2]:
[24, 25, 10]
In [3]:
list(range(10))
Out[3]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [4]:
l = list(range(10))
In [5]:
print(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [6]:
[ v**2 for v in l ]
Out[6]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [7]:
import numpy as np
In [8]:
t = np.array(l)
In [9]:
print(t)
[0 1 2 3 4 5 6 7 8 9]
In [10]:
t
Out[10]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [11]:
l
Out[11]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [12]:
for i in t:
    print(i, "a pour carré", i**2)
0 a pour carré 0
1 a pour carré 1
2 a pour carré 4
3 a pour carré 9
4 a pour carré 16
5 a pour carré 25
6 a pour carré 36
7 a pour carré 49
8 a pour carré 64
9 a pour carré 81
In [13]:
t[4]
Out[13]:
4
In [15]:
liste_mélangée = [12, "Bonjour"]
print(liste_mélangée)
[12, 'Bonjour']
In [16]:
np.array(liste_mélangée)
Out[16]:
array(['12', 'Bonjour'],
      dtype='<U21')

Un tableau numpy ne peut contenir qu'un seul type de donné (à la fois).

création de tableaux numpy

In [17]:
print(np.arange(10))
[0 1 2 3 4 5 6 7 8 9]
In [18]:
print(np.arange(5, 10))
[5 6 7 8 9]
In [19]:
print(np.arange(5, 15, 2))
[ 5  7  9 11 13]
In [20]:
print(np.arange(5.5, 15, 2.5))
[  5.5   8.   10.5  13. ]
In [21]:
range(5.5, 15, 2.5)
------------------------------------------------
TypeError      Traceback (most recent call last)
<ipython-input-21-ee00d0ddca69> in <module>()
----> 1 range(5.5, 15, 2.5)

TypeError: 'float' object cannot be interpreted as an integer
In [23]:
print(np.arange(0, 10.1, 0.5)) # 21 valeurs
[  0.    0.5   1.    1.5   2.    2.5   3.    3.5   4.    4.5   5.    5.5
   6.    6.5   7.    7.5   8.    8.5   9.    9.5  10. ]
In [24]:
print(np.arange(0, 10.1, 10/19)) # 20 valeurs
[  0.           0.52631579   1.05263158   1.57894737   2.10526316
   2.63157895   3.15789474   3.68421053   4.21052632   4.73684211
   5.26315789   5.78947368   6.31578947   6.84210526   7.36842105
   7.89473684   8.42105263   8.94736842   9.47368421  10.        ]
In [25]:
print( np.linspace(0, 10, 20) )
[  0.           0.52631579   1.05263158   1.57894737   2.10526316
   2.63157895   3.15789474   3.68421053   4.21052632   4.73684211
   5.26315789   5.78947368   6.31578947   6.84210526   7.36842105
   7.89473684   8.42105263   8.94736842   9.47368421  10.        ]
In [26]:
t2 = np.linspace(0, 10, 20)
print(t2)
[  0.           0.52631579   1.05263158   1.57894737   2.10526316
   2.63157895   3.15789474   3.68421053   4.21052632   4.73684211
   5.26315789   5.78947368   6.31578947   6.84210526   7.36842105
   7.89473684   8.42105263   8.94736842   9.47368421  10.        ]
In [27]:
t2 ** 2
Out[27]:
array([   0.        ,    0.27700831,    1.10803324,    2.49307479,
          4.43213296,    6.92520776,    9.97229917,   13.5734072 ,
         17.72853186,   22.43767313,   27.70083102,   33.51800554,
         39.88919668,   46.81440443,   54.29362881,   62.32686981,
         70.91412742,   80.05540166,   89.75069252,  100.        ])

apparté matplotlib

In [28]:
import matplotlib.pyplot as plt
In [32]:
plt.plot(t2, t2**2 - t2**3 / 10)
plt.show()

Fin de l'apparté.

Création de tableaux à plusieurs dimensions

In [35]:
np.zeros(10)
Out[35]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [37]:
np.ones(10)
Out[37]:
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [39]:
np.zeros( (3, 10) )
Out[39]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
In [40]:
np.ones( (4, 2) )
Out[40]:
array([[ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.]])
In [42]:
t3 = np.ones( (4,5) )
print(t3)
[[ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]]
In [45]:
print(t3.shape)
print("Lignes :", t3.shape[0])
print("Colonnes :", t3.shape[1])
(4, 5)
Lignes : 4
Colonnes : 5
In [47]:
print(t3 * 3.14)
[[ 3.14  3.14  3.14  3.14  3.14]
 [ 3.14  3.14  3.14  3.14  3.14]
 [ 3.14  3.14  3.14  3.14  3.14]
 [ 3.14  3.14  3.14  3.14  3.14]]
In [50]:
import random
print(t3 * random.uniform(10, 20))
[[ 12.06637886  12.06637886  12.06637886  12.06637886  12.06637886]
 [ 12.06637886  12.06637886  12.06637886  12.06637886  12.06637886]
 [ 12.06637886  12.06637886  12.06637886  12.06637886  12.06637886]
 [ 12.06637886  12.06637886  12.06637886  12.06637886  12.06637886]]

Créer des tableaux de nombres au hasard

In [53]:
print(np.random.uniform(10, 20, (4, 5) ))
[[ 18.37556247  17.70269211  11.94468548  15.87690676  10.99609224]
 [ 11.4551631   18.99528968  16.66570666  16.2637936   10.86554944]
 [ 19.45712246  12.40397507  10.09439898  18.2819238   12.59957659]
 [ 17.41290099  12.05312429  14.23546735  19.77299892  13.3838662 ]]
In [55]:
print(np.random.normal(10, 2, (4, 5) ))
[[  7.36308589   8.69738279  11.1351847   12.36079176  10.21319558]
 [  8.96007081  11.15917855   9.99468813   6.07840387  10.58287109]
 [  9.94413085   8.27686843  12.4719652   12.71903856   5.74019002]
 [  8.15836351   7.66592266  10.36790908   9.59513803  10.46829937]]
In [57]:
print(np.random.normal(10, 0.2, (4, 5) ))
[[  9.81325462  10.37098986   9.83829029   9.95337993   9.93315361]
 [ 10.34548801  10.26529742  10.45201338   9.98226836  10.21656157]
 [ 10.05586323   9.95759468  10.36145772  10.11761559   9.77531131]
 [ 10.07088045  10.0993495    9.95402675   9.70391774  10.03908134]]

Shape

In [60]:
print(t3)
print("a pour shape", t3.shape)
[[ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]]
a pour shape (4, 5)
In [63]:
t5 = np.arange(100, 112)
print(t5)
print(t5.shape)
print(t5.shape[0])
[100 101 102 103 104 105 106 107 108 109 110 111]
(12,)
12
In [66]:
t6 = t5.reshape( (2, 6) )
print(t6)
print(t6.shape)
[[100 101 102 103 104 105]
 [106 107 108 109 110 111]]
(2, 6)
In [68]:
t5.reshape( (5, 5) ) # 5*5 != 12
------------------------------------------------
ValueError     Traceback (most recent call last)
<ipython-input-68-bafd733426b2> in <module>()
----> 1 t5.reshape( (5, 5) ) # 5*5 != 12

ValueError: cannot reshape array of size 12 into shape (5,5)
In [70]:
t5.reshape( (2, -1) )
Out[70]:
array([[100, 101, 102, 103, 104, 105],
       [106, 107, 108, 109, 110, 111]])
In [71]:
t5.reshape( (-1, 6) )
Out[71]:
array([[100, 101, 102, 103, 104, 105],
       [106, 107, 108, 109, 110, 111]])
In [73]:
t5.reshape( (-1, 2) )
Out[73]:
array([[100, 101],
       [102, 103],
       [104, 105],
       [106, 107],
       [108, 109],
       [110, 111]])
In [75]:
t5.reshape( (-1, -1) )
------------------------------------------------
ValueError     Traceback (most recent call last)
<ipython-input-75-347034128f7b> in <module>()
----> 1 t5.reshape( (-1, -1) )

ValueError: can only specify one unknown dimension

TD2

In [76]:
#Q1
print( np.array([4, 5]) )
[4 5]
In [77]:
#Q2
print( np.zeros((4, 5)) )
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
In [78]:
#Q3
print( np.ones((5, 4)) )
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
In [80]:
#Q4
print( np.random.uniform(2, 3, (4, 5)) )
[[ 2.29096451  2.28301624  2.58576388  2.08873532  2.63329332]
 [ 2.87674056  2.7541203   2.15893294  2.90548919  2.23735394]
 [ 2.90401726  2.34634195  2.99583213  2.96871496  2.95065988]
 [ 2.61942449  2.35729664  2.62050621  2.95558967  2.19078038]]
In [81]:
#Q5
print( np.random.normal(2, 3, (4, 5)) )
[[-1.24258074  2.84003166 -5.82491069  2.41438278  3.16903483]
 [ 3.08807574  1.84605959 -4.92364871  0.91357041 -1.905491  ]
 [ 1.78153032  7.17773944  4.85730504 -2.39704442  2.31917398]
 [-3.01048669 -1.5056997   5.56597982  0.21662987  0.99700518]]
In [91]:
v = np.random.uniform(2, 3, 10)
print(v)
n = 1000
v = np.random.uniform(2, 3, n)
plt.scatter(v, np.arange(n), marker='.')
[ 2.33400584  2.17424386  2.30432846  2.77507002  2.67822753  2.55953181
  2.41961921  2.25963803  2.35865981  2.91240274]
Out[91]:
<matplotlib.collections.PathCollection at 0x7f5295b3aac8>
In [95]:
n = 3000
v = np.random.normal(2, 0.1, n)
plt.scatter(v, np.arange(n), marker='.')
Out[95]:
<matplotlib.collections.PathCollection at 0x7f529569fe48>
In [97]:
n = 3000
v = np.random.normal(2, 3, n)
plt.scatter(v, np.arange(n), marker='.')
Out[97]:
<matplotlib.collections.PathCollection at 0x7f52955ee320>
In [98]:
#Q6
print( np.linspace(3, 4, 5) )
[ 3.    3.25  3.5   3.75  4.  ]
In [101]:
#Q7
print( np.arange(100, 1000, 10) )
print( np.arange(100, 1000, 55.2) )
[100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270
 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450
 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630
 640 650 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810
 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990]
[ 100.   155.2  210.4  265.6  320.8  376.   431.2  486.4  541.6  596.8
  652.   707.2  762.4  817.6  872.8  928.   983.2]
In [104]:
#Q8
print( np.arange(100, 225, 25)  )
print( np.linspace(100, 200, 5) )
print( np.array([100, 125, 150, 175, 200]) )
[100 125 150 175 200]
[ 100.  125.  150.  175.  200.]
[100 125 150 175 200]
In [ ]:

In [ ]: