1. 第一種方式,通過Numpy函數(shù),借助python數(shù)組進(jìn)行創(chuàng)建
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = [1, 2, 3, 4, 5]
z = np.array({1, "2", 3, 4, 5, "2"}) ##不報錯么?(是的?。?
print(x)
print(y)
print(z) #是個集合么?(看樣子像)
print(type(x))
print(type(y))
print(type(z))
[1 2 3 4 5]
[1, 2, 3, 4, 5]
{'2', 1, 3, 4, 5}
<class 'numpy.ndarray'>
<class 'list'>
<class 'numpy.ndarray'>
注意,print的結(jié)果中,ndarray的一維數(shù)組各元素中間用空格分開,list的各元素用 "," 分開
1.1 dtype函數(shù)用來顯示adarray中元素的類型
只能通過
變量.dtype
使用,不能通過函數(shù)調(diào)用。
x.dtype # dtype() is wrong
dtype('int32')
z.dtype # 類型是0 是個什么鬼 (可能是集合!)
dtype('O')
1.2 shape屬性 用來獲得數(shù)組的形狀 輸出結(jié)果為元組,代表數(shù)組的 行數(shù) X 列數(shù)(一位數(shù)組只代表元素個數(shù))
x.shape
(5,)
Y = np.array([[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]])
print(Y)
print(type(Y))
print(Y.dtype)
print(Y.shape)
[[ 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]]
<class 'numpy.ndarray'>
int32
(5, 5)
1.3 轉(zhuǎn)化為ndarray時,如果數(shù)組內(nèi)元素類型不一致,會自動進(jìn)行轉(zhuǎn)換。
但是轉(zhuǎn)換依據(jù)是啥呢?為啥上面的變量z 在np.array后就變成集合了呢?還自動去重了。
估計(jì)是集合比列表等級高吧
X = np.array(["Hello", "World", "!"])
print(X)
print(type(X))
print(X.dtype)
['Hello' 'World' '!']
<class 'numpy.ndarray'>
<U5
Z = np.array([1, 2, 3.5, 4])
print(Z)
print(type(Z))
print(Z.dtype)
[1. 2. 3.5 4. ]
<class 'numpy.ndarray'>
float64
x = np.array([1, 2, 3, 4, 5], dtype = np.int64)
print(x)
print(type(x))
print(x.dtype)
#對比 1.1中的 x.dtype 結(jié)果為 int32
[1 2 3 4 5]
<class 'numpy.ndarray'>
int64
1.4 ndarry數(shù)組 另存為文件/自文件讀取
np.save("np_array_x", x)
y = np.load("np_array_x.npy")
print(y)
print(type(y))
print(y.dtype)
[1 2 3 4 5]
<class 'numpy.ndarray'>
int64
2. 使用內(nèi)置函數(shù)創(chuàng)建 ndarray
2.1 使用zeros創(chuàng)建指定形狀的 零矩陣
a = np.zeros((5,6))
print(a)
print(type(a))
print(a.dtype)
[[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. 0. 0. 0. 0. 0.]]
<class 'numpy.ndarray'>
float64
a = np.zeros(5)
print(a)
print(type(a))
print(a.dtype)
[0. 0. 0. 0. 0.]
<class 'numpy.ndarray'>
float64
a = np.zeros((4,5))
print(a)
print(type(a))
print(a.dtype)
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
<class 'numpy.ndarray'>
float64
a = np.zeros((5,1))
print(a)
print(type(a))
print(a.dtype)
[[0.]
[0.]
[0.]
[0.]
[0.]]
<class 'numpy.ndarray'>
float64
2.2 使用ones 創(chuàng)建指定形狀的 1矩陣
a = np.ones((5,6))
print(a)
print(type(a))
print(a.dtype)
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]
<class 'numpy.ndarray'>
float64
2.3 使用full 創(chuàng)建指定形狀的 制定矩陣
a = np.full((5,6), 3)
print(a)
print(type(a))
print(a.dtype)
[[3 3 3 3 3 3]
[3 3 3 3 3 3]
[3 3 3 3 3 3]
[3 3 3 3 3 3]
[3 3 3 3 3 3]]
<class 'numpy.ndarray'>
int32
a = np.full((5,6), 3 , dtype = np.float64)
print(a)
print(type(a))
print(a.dtype)
[[3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3.]]
<class 'numpy.ndarray'>
float64
2.4 使用eye 和diag 創(chuàng)建單位矩陣和對角矩陣
a = np.eye(5)
print(a)
print(a.dtype)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
float64
a = np.diag([1, 2, 3, 4, 5.5])
print(a)
print(a.dtype)
[[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.5]]
float64
2.5 使用arange 和linspace 創(chuàng)建一維序列數(shù)組
a = np.arange(5)
print(a)
print(a.dtype)
[0 1 2 3 4]
int32
a = np.arange(3, 5) #右側(cè)是開區(qū)間
print(a)
print(a.dtype)
[3 4]
int32
a = np.arange(1, 10, 3)
print(a)
print(a.dtype)
[1 4 7]
int32
a = np.linspace(1, 10 ,3) #右側(cè)是閉區(qū)間
print(a)
print(a.dtype)
[ 1. 5.5 10. ]
float64
a = np.linspace(1, 10 ,3, endpoint = False) #右側(cè)是開區(qū)間
print(a)
print(a.dtype)
[1. 4. 7.]
float64
2.6 使用reshape 創(chuàng)建制定形狀的數(shù)組
a = np.arange(20)
a = np.reshape(a, (5,4))
print(a)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
a = np.arange(20)
#a = np.reshape(a, (5,5)) #不合適的時候會報錯 ValueError: cannot reshape array of size 20 into shape (5,5)
print(a)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
a = np.linspace(0, 50, 10, endpoint = False)
a = np.reshape(a, (2,5))
print(a)
[[ 0. 5. 10. 15. 20.]
[25. 30. 35. 40. 45.]]
2.7 創(chuàng)建隨機(jī)數(shù)字組成的矩陣
a = np.random.random((6,4))
print(a)
[[0.16341192 0.92425624 0.98660487 0.23340336]
[0.88237895 0.8998482 0.59402848 0.67691237]
[0.43041398 0.21376123 0.36314821 0.07867518]
[0.77297942 0.04516418 0.03975976 0.6301805 ]
[0.38989407 0.93983862 0.36824193 0.23511734]
[0.09223506 0.63774615 0.34881241 0.12780873]]
a = np.random.randint(1,25,(6,4))
print(a)
[[ 8 2 5 24]
[ 3 24 12 8]
[12 10 9 7]
[ 8 5 1 16]
[23 16 1 18]
[20 23 2 7]]
a = np.random.normal(0, 0.1, size = (1000,1000))
print(a)
[[-0.19938692 -0.13490166 0.06927083 ... -0.00691356 0.07267684
-0.13059106]
[-0.03249243 0.06512099 0.14695018 ... 0.00762984 0.00093603
0.00070895]
[-0.05814277 0.16556299 -0.00316467 ... -0.02531911 0.03528357
0.05864788]
...
[-0.01659603 -0.15226881 0.06939579 ... -0.02749888 -0.04761764
-0.06417794]
[-0.16429182 -0.04392884 0.10834514 ... 0.02568081 0.03545597
-0.05895939]
[-0.05120482 -0.0368964 -0.07374493 ... -0.21786843 -0.15273928
-0.10539549]]
print('a = \n', a)
# We print information about a
print('X has dimensions:', a.shape)
print('X is an object of type:', type(a))
print('The elements in X are of type:', a.dtype)
print('The elements in X have a mean of:', a.mean())
print('The maximum value in X is:', a.max())
print('The minimum value in X is:', a.min())
print('X has', (a < 0).sum(), 'negative numbers')
print('X has', (a > 0).sum(), 'positive numbers')
a =
[[-0.19938692 -0.13490166 0.06927083 ... -0.00691356 0.07267684
-0.13059106]
[-0.03249243 0.06512099 0.14695018 ... 0.00762984 0.00093603
0.00070895]
[-0.05814277 0.16556299 -0.00316467 ... -0.02531911 0.03528357
0.05864788]
...
[-0.01659603 -0.15226881 0.06939579 ... -0.02749888 -0.04761764
-0.06417794]
[-0.16429182 -0.04392884 0.10834514 ... 0.02568081 0.03545597
-0.05895939]
[-0.05120482 -0.0368964 -0.07374493 ... -0.21786843 -0.15273928
-0.10539549]]
X has dimensions: (1000, 1000)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float64
The elements in X have a mean of: -0.000213846977574448
The maximum value in X is: 0.4726203284424575
The minimum value in X is: -0.488902791694619
X has 501307 negative numbers
X has 498693 positive numbers