array(...)
array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
Create an array.
Parameters
----------object: array_like
An array,anyobject exposing the array interface, an
object whose __array__ method returns an array,orany(nested) sequence.
dtype : data-type, optional
The desired data-typefor 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.......
2 ,最小值 :
代碼 :
data = numpy.array([[1,2,3,4],[2,3,4,1],[3,4,1,2]])
res = data.min()print(res)
結果 :
1
3 ,最大值 :
代碼 :
data = numpy.array([[1,2,3,4],[2,3,4,1],[3,6,1,2]])
res = data.max()print(res)
結果 :
6
4 ,求和 : 列的和
代碼 :
data = numpy.array([[1,2,3,4],[2,3,4,1],[3,6,1,2]])print(data)
d = data.sum(axis=0)print(d)
結果 :
[[1234][2341][3612]][61187]
5 ,求和 : 行的和
data = numpy.array([[1,2,3,4],[2,3,4,1],[3,6,1,2]])print(data)
d = data.sum(axis=1)print(d)
結果 :
[[1234][2341][3612]][101012]
6 ,求和 : 全部數據的和
data = numpy.array([[1,2,3,4],[2,3,4,1],[3,6,1,2]])print(data)
d = data.sum()print(d)
結果 :
[[1234][2341][3612]]32
7 ,造數據 : 矩陣 ( 3 × 5 ) ( reshape )
代碼 :
import numpy as np
# 生成 0-14 的數字
data = np.arange(15)# 變形 : 讓數據成爲 3 × 5 的矩陣
d = data.reshape(3,5)print(d)
結果 :
[[01234][56789][1011121314]]
8 ,查看矩陣 : 形狀,維數
查看矩陣的行列數 :
s = d.shape
結果 :(3,5)
查看矩陣的維數 :幾維矩陣
s = d.ndim
結果 :2
9 ,查看矩陣 : 元素類型,元素總數
元素類型 :
s = d.dtype.name
結果 :int32
元素總數 :
s = d.size
結果 :15
10 ,造矩陣 : 初始值爲 0
data = np.zeros((3,5))print(data)
結果 :
[[0.0.0.0.0.][0.0.0.0.0.][0.0.0.0.0.]]
11 ,造矩陣 : 3 階矩陣,初始值爲 1
目標 : 造三維矩陣
1 ,三維矩陣的形狀 : 2 × 3 × 4
2 ,初始值 : 1
代碼 :
data = np.ones((2,3,4),dtype=np.int32)print(data)
結果 :
[[[1111][1111][1111]][[1111][1111][1111]]]
12 ,造數據 : 階梯數
目的 : 造出 3,6,9
data = np.arange(3,10,3)print(data)
結果 :
[369]
13 ,造數據 : 隨機數矩陣
目的 : 造出 2 × 3 的矩陣
data = np.random.random([2,3])print(data)
結果 :
[[0.247494620.260183310.07207925][0.019589970.222002280.42326946]]
14 ,造數據 : 起止點,取 n 的數字
目的 : 從 1 - 10 ,取 100 個數字
注意 : 含頭,含尾
代碼 :
data = np.linspace(0,5,11)print(data)
結果 :
[0.0.51.1.52.2.53.3.54.4.55.]
15 ,三角函數 : 正弦
sin(90o)
from numpy import pi
res = np.sin(pi/2)print(res)
結果 :
1.0