Creating Matrices using Numpy

Here are two ways of creating 2d-array:
1) By converting a list of lists (or tuples) into an array:
>>> x = N.array([[1,2,3], [4,5,6]])
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
2) Using the zeros method to create a matrix with 5 rows and 5 columns
>>> x = N.zeros((5,5))
>>> print x
[[ 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.]]
The shape of a matrix can be queried like this.
>>> x.shape
(5, 5)
Individual elements can be accessed and set using this syntax:
>>> x = N.array([[1,2,3], [4,5,6]])
>>> x[0,0]
1
>>> x[0,1]
2
>>> x[0,2]
3
>>> x[:,0]
array([1, 4])
>>> x[0, :]
array([1, 2, 3])

Nema komentara:

Objavi komentar