C Lang/machine learing
- 3-8. Numpy(boolean,numpy i o 2018.11.20
- 3-7. Numpy(comparision:all,any,comparision operation, where,argmax,argmin,sort,converse sort) 2018.11.20
- 3-6.Numpy Session( comparision:> = < , zny(), all(), where, argmax,argmin) 2018.11.16
- 3-5.Numpy Section(array functions:element wise operation, dot product, transpose, broadcasting, $testit performance test,역행렬 구하기) 2018.11.16
- 3-4.Numpy Section(operation functions:sum,axis,concatenate) 2018.11.15
- 3-3.Numpy Section(reshaping,flatten,indexing,slicing,aragne,ones,zeros,identity,eye,dialog,random.uniform,random normal) 2018.11.11
- 3-2.Numpy Section(Ndarray:numpy의 데이터 구조, 메모리사용, 디멘션 구성을 반환하는shape함수, array의 데이터 타입을 반환하는dtype함수, 몇차원인지를 반환하는 ndim함수, Vector, Metrix개념, 데이터의 개.. 2018.11.07
- 2-6(Lab: Simple Linear algebra codes:zip함수, unpacking 기호*, 미지정 파라미터 *args ) 2018.11.07
3-8. Numpy(boolean,numpy i o
3-7. Numpy(comparision:all,any,comparision operation, where,argmax,argmin,sort,converse sort)
ソートされた新たなndarray
が返され、元のndarray
はそのまま。
import numpy as np
a = np.array([3, 4, 2, 0, 1])
print(a)
# [3 4 2 0 1]
a_sort = np.sort(a)
print(a_sort)
# [0 1 2 3 4]
print(a)
# [3 4 2 0 1]
ndarray
のメソッドsort()
を使うとndarray
自身が更新される。後述。
np.sort()
関数にはPython標準のsort()
メソッドやsorted()
関数のように引数reverse
は存在しない。降順にしたい場合はスライス[::-1]
を使う。
a_sort_reverse = np.sort(a)[::-1]
print(a_sort_reverse)
# [4 3 2 1 0]
'C Lang > machine learing' 카테고리의 다른 글
3-6.Numpy Session( comparision:> = < , zny(), all(), where, argmax,argmin)
* test_a > 4 와 같은 부등호 표현은 true or false로 이루어진 np array를 반환한다.
'C Lang > machine learing' 카테고리의 다른 글
3-5.Numpy Section(array functions:element wise operation, dot product, transpose, broadcasting, $testit performance test,역행렬 구하기)
역행렬구하기
1 2 3 4 5 6 7 8 | import numpy as np import numpy.linalg as lin k = np.array([[2,2,0],[-2,1,1],[3,0,1]]) lin.inv(k) #array([[ 0.08333333, -0.16666667, 0.16666667], # [ 0.41666667, 0.16666667, -0.16666667], # [-0.25 , 0.5 , 0.5 ]]) | cs |
'C Lang > machine learing' 카테고리의 다른 글
3-4.Numpy Section(operation functions:sum,axis,concatenate)
'C Lang > machine learing' 카테고리의 다른 글
3-3.Numpy Section(reshaping,flatten,indexing,slicing,aragne,ones,zeros,identity,eye,dialog,random.uniform,random normal)
'C Lang > machine learing' 카테고리의 다른 글
3-2.Numpy Section(Ndarray:numpy의 데이터 구조, 메모리사용, 디멘션 구성을 반환하는shape함수, array의 데이터 타입을 반환하는dtype함수, 몇차원인지를 반환하는 ndim함수, Vector, Metrix개념, 데이터의 개..
https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r
1. The meaning of shapes in NumPy
You write, "I know literally it's list of numbers and list of lists where all list contains only a number" but that's a bit of an unhelpful way to think about it.
The best way to think about NumPy arrays is that they consist of two parts, a data buffer which is just a block of raw elements, and a view which describes how to interpret the data buffer.
For example, if we create an array of 12 integers:
>>> a = numpy.arange(12)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Then a
consists of a data buffer, arranged something like this:
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
and a view which describes how to interpret the data:
>>> a.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
>>> a.dtype
dtype('int64')
>>> a.itemsize
8
>>> a.strides
(8,)
>>> a.shape
(12,)
Here the shape (12,)
means the array is indexed by a single index which runs from 0 to 11. Conceptually, if we label this single index i
, the array a
looks like this:
i= 0 1 2 3 4 5 6 7 8 9 10 11
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
If we reshape an array, this doesn't change the data buffer. Instead, it creates a new view that describes a different way to interpret the data. So after:
>>> b = a.reshape((3, 4))
the array b
has the same data buffer as a
, but now it is indexed by two indices which run from 0 to 2 and 0 to 3 respectively. If we label the two indices i
and j
, the array b
looks like this:
i= 0 0 0 0 1 1 1 1 2 2 2 2
j= 0 1 2 3 0 1 2 3 0 1 2 3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
which means that:
>>> b[2,1]
9
You can see that the second index changes quickly and the first index changes slowly. If you prefer this to be the other way round, you can specify the order
parameter:
>>> c = a.reshape((3, 4), order='F')
which results in an array indexed like this:
i= 0 1 2 0 1 2 0 1 2 0 1 2
j= 0 0 0 1 1 1 2 2 2 3 3 3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
which means that:
>>> c[2,1]
5
It should now be clear what it means for an array to have a shape with one or more dimensions of size 1. After:
>>> d = a.reshape((12, 1))
the array d
is indexed by two indices, the first of which runs from 0 to 11, and the second index is always 0:
i= 0 1 2 3 4 5 6 7 8 9 10 11
j= 0 0 0 0 0 0 0 0 0 0 0 0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
and so:
>>> d[10,0]
10
A dimension of length 1 is "free" (in some sense), so there's nothing stopping you from going to town:
>>> e = a.reshape((1, 2, 1, 6, 1))
giving an array indexed like this:
i= 0 0 0 0 0 0 0 0 0 0 0 0
j= 0 0 0 0 0 0 1 1 1 1 1 1
k= 0 0 0 0 0 0 0 0 0 0 0 0
l= 0 1 2 3 4 5 0 1 2 3 4 5
m= 0 0 0 0 0 0 0 0 0 0 0 0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
and so:
>>> e[0,1,0,0,0]
6
See the NumPy internals documentation for more details about how arrays are implemented.
2. What to do?
Since numpy.reshape
just creates a new view, you shouldn't be scared about using it whenever necessary. It's the right tool to use when you want to index an array in a different way.
However, in a long computation it's usually possible to arrange to construct arrays with the "right" shape in the first place, and so minimize the number of reshapes and transposes. But without seeing the actual context that led to the need for a reshape, it's hard to say what should be changed.
The example in your question is:
numpy.dot(M[:,0], numpy.ones((1, R)))
but this is not realistic. First, this expression:
M[:,0].sum()
computes the result more simply. Second, is there really something special about column 0? Perhaps what you actually need is:
M.sum(axis=0)