---
NUMPY
---
```python
import numpy as np
np.__version__
```
'1.17.2'
```python
## 3D
() ## 函式計算
[] ## 數值或者參數
{} ## dict
[1,2,3]
```
[1, 2, 3]
```python
np.info(np.array)
```
array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Create an array.
Parameters
----------
object : array_like
An array, any object exposing the array interface, an object whose
__array__ method returns an array, or any (nested) sequence.
dtype : data-type, optional
The desired data-type for the array. If not given, then the type will
be determined as the minimum type required to hold the objects in the
sequence. This argument can only be used to 'upcast' the array. For
downcasting, use the .astype(t) method.
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy will
only be made if __array__ returns a copy, if obj is a nested sequence,
or if a copy is needed to satisfy any of the other requirements
(`dtype`, `order`, etc.).
order : {'K', 'A', 'C', 'F'}, optional
Specify the memory layout of the array. If object is not an array, the
newly created array will be in C order (row major) unless 'F' is
specified, in which case it will be in Fortran order (column major).
If object is an array the following holds.
===== ========= ===================================================
order no copy copy=True
===== ========= ===================================================
'K' unchanged F & C order preserved, otherwise most similar order
'A' unchanged F order if input is F and not C, otherwise C order
'C' C order C order
'F' F order F order
===== ========= ===================================================
When ``copy=False`` and a copy is made for other reasons, the result is
the same as if ``copy=True``, with some exceptions for `A`, see the
Notes section. The default order is 'K'.
subok : bool, optional
If True, then sub-classes will be passed-through, otherwise
the returned array will be forced to be a base-class array (default).
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting
array should have. Ones will be pre-pended to the shape as
needed to meet this requirement.
Returns
-------
out : ndarray
An array object satisfying the specified requirements.
See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
zeros_like : Return an array of zeros with shape and type of input.
full_like : Return a new array with shape of input filled with value.
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
zeros : Return a new array setting values to zero.
full : Return a new array of given shape filled with value.
Notes
-----
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.
Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])
Upcasting:
>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
[3, 4]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])
Type provided:
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])
Data-type consisting of more than one element:
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])
Creating an array from sub-classes:
>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])
>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
[3, 4]])
```python
# reshape 轉換維度
a = np.arange(15).reshape(3, 5)
a
```
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
```python
## RGB
3*3*3
a = np.arange(27).reshape(3, 3 ,3)
a
(0-255)
```
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
```python
## 前15位數
np.arange(15)
```
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
```python
## 開始, 結束, 截距
np.arange(0,10,2)
```
array([0, 2, 4, 6, 8])
```python
a.shape
```
(3, 5)
```python
a.size
```
15
```python
type(a)
```
numpy.ndarray
```python
a.dtype
```
dtype('int64')
```python
## 轉換type
b = a.astype(float)
b
```
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.]])
```python
b.dtype
```
dtype('float64')
```python
np.array([1,2,3,4])
```
array([1, 2, 3, 4])
```python
#np.array(1,2,3,4)
```
```python
b = np.array([(1.5, 2, 3), (4, 5, 6)])
b
```
array([[1.5, 2. , 3. ],
[4. , 5. , 6. ]])
```python
np.linspace(0, 10, 10)
```
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
```python
np.linspace(1, 10, num=10)
```
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
```python
A = np.array([[1, 1],
[0, 1]])
B = np.array([[2, 0],
[3, 4]])
```
```python
A * B
```
array([[2, 0],
[0, 4]])
```python
A @ B
```
array([[5, 4],
[3, 4]])
```python
A.dot(B)
```
array([[5, 4],
[3, 4]])
```python
A.sum(axis=0)
```
array([1, 2])
```python
A.min(axis=0)
```
array([0, 1])
```python
B.max(axis=0)
```
array([3, 4])
```python
def f(x, y):
return 10 * x + y
```
```python
f(1,2)
```
12
```python
f(np.array([2,10]),np.array([1,0]))
```
array([ 21, 100])
```python
f(A,B)
```
array([[12, 10],
[ 3, 14]])
```python
B
```
array([[2, 0],
[3, 4]])
```python
B[0]
```
array([2, 0])
```python
for row in B:
print(row)
```
[2 0]
[3 4]
```python
C = np.array([[2,5,6],
[2,2,2],
[3,4,5]])
C
```
array([[2, 5, 6],
[2, 2, 2],
[3, 4, 5]])
```python
C < 4
```
array([[ True, False, False],
[False, False, True],
[ True, False, False]])
```python
C[C < 4]
```
array([2, 2, 3])
```python
(C < 4) | (C == 5)
```
array([[ True, True, False],
[False, False, True],
[ True, False, True]])
```python
C[(C < 4) | (C == 5)]
```
array([2, 5, 2, 3, 5])
```python
(C <4) | (C > 5)
```
array([[ True, False, True],
[ True, False, True],
[ True, False, False]])
```python
C[(C <4) | (C > 5)]
```
array([2, 6, 7, 2, 3])
```python
np.unique(C)
```
array([2, 3, 4, 5, 6, 7])
```python
np.unique(C, axis=0)
```
array([[2, 2, 2],
[2, 5, 6],
[3, 4, 5]])
```python
# pd.unique != np.unique
```
```python
c = np.array([1, 0, np.nan, np.inf])
c
```
array([ 1., 0., nan, inf])
```python
## na位置
np.isnan(c)
```
array([False, False, True, False])
```python
np.isinf(c)
```
array([False, False, False, True])
```python
c[~np.isnan(c)]
```
array([ 1., 0., inf])
# 功課二
## 題目一 建立5x5的隨機矩陣
### 用一下這個網址,找尋其中一個function使用,並解釋使用的function使用方法與功能 https://numpy.org/doc/1.16/reference/routines.random.html
```python
```
## 題目二 建立方程式,並計算結果
### 設定成 2xW1 + 5xW2 + 1xW3 + 6xW4 + 3xW5
```python
```
## 題目三 隨機插入NA值,並使用is.ns找到其位置
```python
```
## 題目四 使用題目一矩陣,將大於中位數的數值都相加
### https://numpy.org/doc/stable/reference/generated/numpy.median.html
```python
```