Exercices : créer les tableaux suivants de la manière la plus simple possible (pas élément par élement)
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 2.]
[ 1. 6. 1. 1.]]
[[0 0 0 0 0]
[2 0 0 0 0]
[0 3 0 0 0]
[0 0 4 0 0]
[0 0 0 5 0]
[0 0 0 0 6]]
Voilà les réponses !
Réponses
>>> a = np.ones((4,4))
>>> a[3,1] = 6
>>> a[2,3] = 2
>>> a
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 2.],
[ 1., 6., 1., 1.]])
>>> b = np.diag(np.arange(1,7))
>>> b = b[:,1:]
>>> b
array([[0, 0, 0, 0, 0],
[2, 0, 0, 0, 0],
[0, 3, 0, 0, 0],
[0, 0, 4, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 6]])
>>> data = np.loadtxt('pourcentage_femmes.txt')
>>> data.shape
(21, 6)
>>> annees = np.arange(2006, 2000, -1)
>>> annees
array([2006, 2005, 2004, 2003, 2002, 2001])
>>> organisms = np.loadtxt('organismes.txt', dtype=str)
>>> organisms
array(['ADEME', 'BRGM', 'CEA', 'CEMAGREF', 'CIRAD', 'CNES', 'CNRS', 'CSTB',
'IFREMER', 'INED', 'INERIS', 'INRA', 'INRETS', 'INRIA', 'INSERM',
'IRD', 'IRSN', 'LCPC', 'ONERA', 'Pasteur', 'Universites'],
dtype='|S11')
>>> data.shape[0] == organisms.shape[0]
True
>>> data.max() # max sur tout le tableau
56.299999999999997
>>> mn = data.mean(axis=1)
>>> mn
array([ 37.8 , 23.16666667, 25.15 , 24.51666667,
24.38333333, 25.46666667, 30.88333333, 23.36666667,
25.08333333, 52.53333333, 29.33333333, 37.58333333,
31.86666667, 18.21666667, 50.16666667, 23.23333333,
33.45 , 15.18333333, 14.35 , 49.86666667, 33.41666667])
>>> np.nonzero(annees==2004)
(array([2]),)
>>> np.argmax(data[:,2])
9
>>> organisms[9]
'INED'
>>> indices = np.argmax(data, axis=0)
>>> indices
array([ 9, 9, 9, 9, 9, 19])
>>> organisms[indices]
array(['INED', 'INED', 'INED', 'INED', 'INED', 'Pasteur'],
dtype='|S11')
>>> np.nonzero(annees==2006)
(array([0]),)
>>> hi = np.histogram(data[:,0])
In [88]: bar(hi[1][:-1], hi[0])
Suite de l’exercice
>>> sup30 = data>30
>>> indices = np.argmax(data, axis=0)
>>> indices
array([ 9, 9, 9, 9, 9, 19])
>>> organisms[indices]
array(['INED', 'INED', 'INED', 'INED', 'INED', 'Pasteur'],
dtype='|S11')