1. 第一种方式,通过Numpy函数,借助python数组进行创建
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的结果中,ndarray的一维数组各元素中间用空格分开,list的各元素用 "," 分开
1.1 dtype函数用来显示adarray中元素的类型
只能通过
变量.dtype
使用,不能通过函数调用。
x.dtype # dtype() is wrong
dtype('int32')
z.dtype # 类型是0 是个什么鬼 (可能是集合!)
dtype('O')
1.2 shape属性 用来获得数组的形状 输出结果为元组,代表数组的 行数 X 列数(一位数组只代表元素个数)
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 转化为ndarray时,如果数组内元素类型不一致,会自动进行转换。
但是转换依据是啥呢?为啥上面的变量z 在np.array后就变成集合了呢?还自动去重了。
估计是集合比列表等级高吧
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 结果为 int32
[1 2 3 4 5]
<class 'numpy.ndarray'>
int64
1.4 ndarry数组 另存为文件/自文件读取
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. 使用内置函数创建 ndarray
2.1 使用zeros创建指定形状的 零矩阵
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 创建指定形状的 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 创建指定形状的 制定矩阵
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 创建单位矩阵和对角矩阵
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 创建一维序列数组
a = np.arange(5)
print(a)
print(a.dtype)
[0 1 2 3 4]
int32
a = np.arange(3, 5) #右侧是开区间
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) #右侧是闭区间
print(a)
print(a.dtype)
[ 1. 5.5 10. ]
float64
a = np.linspace(1, 10 ,3, endpoint = False) #右侧是开区间
print(a)
print(a.dtype)
[1. 4. 7.]
float64
2.6 使用reshape 创建制定形状的数组
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 创建随机数字组成的矩阵
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