Python数据分析第二天-Numpy详解¶
In [4]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
plt.rcParams['font.sans-serif'].insert(0, 'SimHei')
plt.rcParams['axes.unicode_minus'] = False
%config InlineBackend.figure_format = 'svg'
创建数组对象¶
将list处理成ndarray对象¶
In [176]:
array1 =np.array([1,2,3,4,5])
array1
Out[176]:
array([1, 2, 3, 4, 5])
In [5]:
array2 = np.array([[1,2,3],[4,5,6]])
array2
Out[5]:
array([[1, 2, 3], [4, 5, 6]])
In [13]:
#方法二:指定范围创建ndarray对象
array3 = np.arange(1,10,2)
array3
Out[13]:
array([1, 3, 5, 7, 9])
In [14]:
#方法三:等差数列创建ndarray对象
array5 = np.linspace(-5,5,101)
array5
Out[14]:
array([-5. , -4.9, -4.8, -4.7, -4.6, -4.5, -4.4, -4.3, -4.2, -4.1, -4. , -3.9, -3.8, -3.7, -3.6, -3.5, -3.4, -3.3, -3.2, -3.1, -3. , -2.9, -2.8, -2.7, -2.6, -2.5, -2.4, -2.3, -2.2, -2.1, -2. , -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1. , -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. ])
In [15]:
x = np.linspace(-2 * np.pi ,2 * np.pi,180)
In [21]:
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x,y1 , color='crimson',linewidth=0.6,marker='.',linestyle='--',label='正弦')
plt.plot(x,y2)
plt.legend() #显示图例
plt.show()
In [ ]:
#方法四:通过np.logspace构造等比数列创建ndarray对象
In [22]:
#方法五:通过随机数创建ndarray对象
array6 = np.random.random((5,3))
array6
Out[22]:
array([[0.80013607, 0.2498826 , 0.67724263], [0.21980679, 0.33914156, 0.55253917], [0.27716929, 0.36938814, 0.36212101], [0.73297476, 0.93050516, 0.5343114 ], [0.79839402, 0.40421958, 0.94820248]])
In [23]:
#标准正态分布随机数
array7 = np.random.randn(20)
array7
Out[23]:
array([-1.28867775, 0.15190725, -1.42346833, 1.77843769, -0.02119879, -0.26442008, -0.07028125, 1.95229431, 0.73698362, 0.17176937, -0.05235791, 0.97201932, -1.07228566, -0.02606679, -1.28050492, 1.60777855, -0.46150364, -1.83954939, 0.04120903, 0.35387242])
In [25]:
array8 = np.random.randint(60,101,(10,3)) #生成60-100的数字 10行 3列
array8
Out[25]:
array([[ 63, 81, 79], [ 73, 74, 98], [ 99, 65, 64], [ 68, 69, 66], [ 77, 98, 100], [ 73, 79, 79], [ 95, 95, 90], [ 97, 82, 89], [ 95, 72, 86], [ 68, 83, 74]], dtype=int32)
In [28]:
#正态分布随机数
array9 = np.random.normal(169.5, 8.5, 5000).astype('i8')
In [29]:
plt.hist(array9)
plt.show
Out[29]:
<function matplotlib.pyplot.show(close=None, block=None)>
In [30]:
#方法六:从字符串中读取数据创建ndarray对象
array10 = np.fromstring("1,2,3,4,5",sep=",",dtype="i8")
array10
Out[30]:
array([1, 2, 3, 4, 5])
In [31]:
array10.dtype
Out[31]:
dtype('int64')
In [41]:
f = open(r'./wenjian/prime.txt',"r",encoding="utf-8")
f.read()
array11 = np.fromstring(f.read(),sep='\n',dtype='i8')
array11
Out[41]:
'2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47\n53\n59\n61\n67\n71\n73\n79\n83\n89\n97\n'
In [39]:
with open ('wenjian/prime.txt') as file_obj:
array11= np.fromstring(file_obj.read(),sep='\n',dtype='i8')
array11
Out[39]:
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
In [42]:
array12 = np.fromfile("wenjian/prime.txt",dtype="i8",count=10,sep='\n')
array12
Out[42]:
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
In [48]:
#面试官:请说一下python中的迭代器
#python中的迭代器是实现了迭代器协议的对象。
#迭代器协议对应类中的两个魔法方法,分别是__iter__和__next__。
#定义迭代器的语法是比较麻烦的,所以python中又提供了两种简化的创建迭代器对象的方式,
#一种是通过内置的iter()来创建;另一种是利用yield或者yield from语法直接创建生成器对象。
def fib(n):
a,b = 0,1
for _ in range(n):
a,b = b ,a+b
yield a
# gen = fib(20)
# for x in gen:
# print(x)
In [43]:
#方法七:从迭代器中获取数据创建ndarray对象
In [ ]:
array13 = np.fromiter(fib(50),dtype='i8')
array13
In [50]:
# 方法八:通过对元素的重复创建ndarray对象
array14 = np.repeat([1,2,3],10)
array14
Out[50]:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
In [51]:
array15 = np.tile([1,2,3],10)
array15
Out[51]:
array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
In [56]:
#其他方法
np.empty((5,3),dtype='i8')
np.zeros((5,3),dtype='i8')
np.ones((5,3),dtype='i8')
Out[56]:
array([[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]])
In [64]:
# 魔法指令 - %timeit - 评估代码运行事件
%timeit np.empty((10000,10000))
17.7 μs ± 1.86 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [65]:
%timeit np.zeros((10000,10000))
17.3 μs ± 1.99 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [66]:
np.eye(5) # 5X5的单位矩阵
Out[66]:
array([[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.]])
In [67]:
np.full((10,10),99)
Out[67]:
array([[99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99], [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]])
In [25]:
#读图片
guido = plt.imread('wenjian/py.jpg')
guido
Out[25]:
array([[[ 36, 33, 28], [ 36, 33, 28], [ 36, 33, 28], ..., [ 32, 31, 29], [ 32, 31, 27], [ 31, 32, 26]], [[ 37, 34, 29], [ 38, 35, 30], [ 38, 35, 30], ..., [ 31, 30, 28], [ 31, 30, 26], [ 30, 31, 25]], [[ 38, 35, 30], [ 38, 35, 30], [ 38, 35, 30], ..., [ 30, 29, 27], [ 30, 29, 25], [ 29, 30, 25]], ..., [[239, 178, 123], [237, 176, 121], [235, 174, 119], ..., [ 78, 68, 56], [ 76, 66, 54], [ 73, 65, 52]], [[238, 177, 120], [236, 175, 118], [234, 173, 116], ..., [ 80, 70, 58], [ 78, 68, 56], [ 74, 67, 51]], [[237, 176, 119], [236, 175, 118], [234, 173, 116], ..., [ 83, 71, 59], [ 81, 69, 57], [ 77, 68, 53]]], shape=(750, 500, 3), dtype=uint8)
In [72]:
plt.imshow(guido)
Out[72]:
<matplotlib.image.AxesImage at 0x1423ec1d130>
In [ ]:
数组对象的属性
In [75]:
#元素个数
array1.size
Out[75]:
5
In [76]:
# 数据类型
# int64 -i8 / int32 -i4
# float64 -f8
array7.dtype
Out[76]:
dtype('float64')
In [77]:
#uint8 - u1
guido.dtype
Out[77]:
dtype('uint8')
In [78]:
# 形状
array1.shape
Out[78]:
(5,)
In [84]:
array2.shape
Out[84]:
(2, 3)
In [79]:
guido.shape
Out[79]:
(750, 500, 3)
In [80]:
#维度
array1.ndim
Out[80]:
1
In [81]:
array2.ndim
Out[81]:
2
In [83]:
guido.ndim
Out[83]:
3
In [85]:
# 每个元素占用的内存空间大小(字节)
array1.itemsize
Out[85]:
8
In [86]:
guido.itemsize
Out[86]:
1
In [87]:
#整个数组占用的内存空间大小(字节)
array1.nbytes
Out[87]:
40
In [88]:
array7.nbytes
Out[88]:
160
In [89]:
guido.nbytes
Out[89]:
1125000
In [90]:
# 内存布局
array1.flags
Out[90]:
C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False
C_CONTIGUOUS : True #行优先存储 F_CONTIGUOUS : True #列优先存储 OWNDATA : True #数据是否是自己的 WRITEABLE : True #写操作 ALIGNED : True WRITEBACKIFCOPY : False
In [91]:
array2.flags
Out[91]:
C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False
In [92]:
guido.flags
Out[92]:
C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : False WRITEABLE : False ALIGNED : True WRITEBACKIFCOPY : False
In [94]:
# 根基(数据的来源)
print(array1.base) #数据来源自己
None
In [95]:
# 转置(行变列,列变行)
array2.T
Out[95]:
array([[1, 4], [2, 5], [3, 6]])
In [97]:
array8.T
Out[97]:
array([[ 63, 73, 99, 68, 77, 73, 95, 97, 95, 68], [ 81, 74, 65, 69, 98, 79, 95, 82, 72, 83], [ 79, 98, 64, 66, 100, 79, 90, 89, 86, 74]], dtype=int32)
数组对象的运算¶
In [ ]:
1.算数运算
2.比较运算
3.逻辑运算
4.索引运算
-普通索引
-花式索引
-布尔索引
-切片索引
In [30]:
temp1 = np.arange(1,10)
temp2 = np.repeat([10,20,30],3)
temp3 = temp1.reshape(3,3) #reshape 调形 改变数据布局 3行3列
temp4 = temp2.reshape(3,3)
In [6]:
temp1
Out[6]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [100]:
temp2
Out[100]:
array([10, 10, 10, 20, 20, 20, 30, 30, 30])
In [101]:
temp3
Out[101]:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [102]:
temp4
Out[102]:
array([[10, 10, 10], [20, 20, 20], [30, 30, 30]])
In [104]:
temp4.base is temp2
Out[104]:
True
In [105]:
temp1 + 10 #数组里所有的元素都进行+10
Out[105]:
array([11, 12, 13, 14, 15, 16, 17, 18, 19])
In [106]:
temp2 / 100 #数组和标量运算,数组的所有元素都会跟标量运算
Out[106]:
array([0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3])
In [107]:
temp1 + temp2
Out[107]:
array([11, 12, 13, 24, 25, 26, 37, 38, 39])
In [ ]:
# temp1 + temp3 #报错,形状不同
In [109]:
temp3 + temp4 #相同形状可以进行运算
Out[109]:
array([[11, 12, 13], [24, 25, 26], [37, 38, 39]])
In [115]:
temp1 > 5
Out[115]:
array([False, False, False, False, False, True, True, True, True])
In [116]:
temp3 > 6
Out[116]:
array([[False, False, False], [False, False, False], [ True, True, True]])
In [117]:
temp1 < temp2
Out[117]:
array([ True, True, True, True, True, True, True, True, True])
In [119]:
temp3 > temp4
Out[119]:
array([[False, False, False], [False, False, False], [False, False, False]])
In [172]:
temp5 = np.array([True,False,True])
temp6 = np.array([True,False,False])
In [127]:
temp5 & temp6 #and
Out[127]:
array([ True, False, False])
In [128]:
temp5 | temp6 #or
Out[128]:
array([ True, False, True])
In [129]:
~temp5 #反
Out[129]:
array([False, True, False])
In [134]:
temp2[2]
Out[134]:
np.int64(10)
In [138]:
temp1[:]
Out[138]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [10]:
temp1[::-1]
Out[10]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1])
In [12]:
temp3[-1,-1] =100
temp3
Out[12]:
array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 100]])
In [15]:
guido[0,0,0] #三维数组 需要三个索引
Out[15]:
np.uint8(36)
In [16]:
temp1
Out[16]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 100])
In [25]:
#花式索引 用放整数的列表或数组充当数据组的索引
In [18]:
temp1[[0,-1,-1,-1]]
Out[18]:
array([ 1, 100, 100, 100])
In [20]:
temp3[[0,-1,-1,-1],[0,-1,-1,-1]] #二维数组 花式索引 两个数组分别放入行列索引取出对应元素
Out[20]:
array([ 1, 100, 100, 100])
In [23]:
guido[[0,-1,-1,-1],[0,-1,-1,-1],[0,-1,-1,-1]] #三维数组 依此类推
Out[23]:
array([36, 53, 53, 53], dtype=uint8)
In [24]:
temp7 = np.random.randint(10,100,12)
temp7
Out[24]:
array([48, 68, 97, 58, 44, 64, 18, 87, 55, 92, 86, 96], dtype=int32)
In [26]:
# 布尔索引 - 用一组布尔值(数组或列表) 充当数组的索引
In [28]:
temp7[temp7 > 50]
Out[28]:
array([68, 97, 58, 64, 87, 55, 92, 86, 96], dtype=int32)
In [29]:
temp7[(temp7 % 2 == 0) & (temp7 > 50) ] #利用布尔的true和false 运用条件进行取舍数据
Out[29]:
array([68, 58, 64, 92, 86, 96], dtype=int32)
In [32]:
temp3[::2,::2]
Out[32]:
array([[1, 3], [7, 9]])
In [ ]:
jt = guido[20:360,80:320]
plt.imshow(jt)
In [ ]:
plt.imshow(guido[::5,::5]) #跨度5 清晰度减少5倍数
In [ ]:
plt.imshow(guido[::-1,::]) # 反转
In [ ]:
temp8 = np.where(guido.mean(axis=2) > guido.mean(),1,0)
plt.imshow(temp8,cmap=plt.cm.gray)
In [53]:
plt.figure(figsize=(12,8))
plt.subplot(2,4,1)
plt.imshow(guido)
plt.subplot(2,4,2)
plt.imshow(guido[30:350,85:310])
plt.subplot(2,4,3)
plt.imshow(guido[:,::-1])
plt.subplot(2,4,4)
temp8 = np.where(guido.mean(axis=2) > guido.mean(),1,0)
plt.imshow(temp8,cmap=plt.cm.gray)
Out[53]:
<matplotlib.image.AxesImage at 0x19417a1d640>
In [99]:
#给脸部打码
temp9 = np.copy(guido)
for i in range(150,321,10):
for j in range(120,311,10):
color = temp9[i,j]
temp9[i:i+10,j:j+10]=color
plt.imshow(temp9)
Out[99]:
<matplotlib.image.AxesImage at 0x1941da385f0>
In [ ]:
###数组对象的方法
1.获取数据的描述性统计信息
-集中趋势 (均值/中位数/众数)
-离散成都 (极值/极差/IQR/方差/标准差/变异系数)
-分布规律 (直方图/偏态系数/风度系数)
In [103]:
scores = np.random.normal(82,6,50).astype('i8')
scores
Out[103]:
array([96, 76, 78, 73, 75, 71, 82, 83, 73, 82, 85, 75, 69, 88, 82, 81, 74, 80, 93, 76, 77, 69, 75, 84, 79, 83, 89, 89, 89, 71, 81, 81, 77, 93, 73, 79, 77, 91, 74, 87, 83, 82, 82, 76, 77, 78, 87, 84, 78, 82])
In [105]:
#算数平均
scores.mean()
Out[105]:
np.float64(80.38)
In [104]:
np.mean(scores)
Out[104]:
np.float64(80.38)
In [106]:
#中位数
np.median(scores)
Out[106]:
np.float64(80.5)
In [ ]:
#众数
In [107]:
#极值
scores.max()
Out[107]:
np.int64(96)
In [108]:
np.argmax(scores)
Out[108]:
np.int64(0)
In [109]:
np.argmin(scores)
Out[109]:
np.int64(12)
In [ ]:
#极差 全距
In [110]:
np.ptp(scores)
Out[110]:
np.int64(27)
In [111]:
# 四分位距离(IQR)
q1,q3 = np.quantile(scores,[0.25,0.75])
q3 - q1
Out[111]:
np.float64(7.75)
In [134]:
stats.iqr(scores)
Out[134]:
np.float64(7.75)
In [ ]:
# 方差
In [112]:
scores.var()
Out[112]:
np.float64(40.3956)
In [113]:
np.var(scores)
Out[113]:
np.float64(40.3956)
In [114]:
np.var(scores,ddof=1)
Out[114]:
np.float64(41.22)
In [ ]:
#标准差
scores.std()
In [ ]:
np.std(scores)
In [ ]:
np.std(scores,ddof=1)
In [ ]:
#变异系数
np.std(scores,ddof=1) / np.mean(scores)
In [ ]:
stats.variation(scores)
In [117]:
# 箱线图
plt.boxplot(scores,showmeans=True,notch=True,sym='.',whis=1.5)
plt.ylim(60,110)
plt.show()
In [119]:
# 直方图
plt.hist(scores,bins=np.arange(60,101,5),edgecolor='white')
plt.show()
In [ ]:
# 偏态系数
stats.skew(scores)
In [ ]:
# 峰度系数
stats.kurtosis(scores)
In [ ]:
%pip install seaborn
In [133]:
#几何平均
stats.gmean(scores)
Out[133]:
np.float64(80.13230589033336)
In [132]:
#调和平均
stats.hmean(scores)
Out[132]:
np.float64(79.8881812012504)
In [ ]:
#中位数
np.median(scores)
In [131]:
#众数
result = stats.mode(scores)
print(result.mode)
print(result.count)
82 6
In [139]:
#所有统计信息
result = stats.describe(scores)
result
Out[139]:
DescribeResult(nobs=50, minmax=(np.int64(69), np.int64(96)), mean=np.float64(80.38), variance=np.float64(41.220000000000006), skewness=np.float64(0.38978111111948943), kurtosis=np.float64(-0.3867165438062572))
In [140]:
#定制需求内容
pd.Series(scores).agg(['count','sum','mean','median','ptp','var','std','skew','kurt'])
Out[140]:
count 50.000000 sum 4019.000000 mean 80.380000 median 80.500000 ptp 27.000000 var 41.220000 std 6.420280 skew 0.401941 kurt -0.298052 dtype: float64
In [147]:
sns.histplot(scores,bins=np.arange(60,101,2.5),edgecolor='white',kde=True)
plt.show()
In [ ]:
scores = np.random.normal(80,6,(50,3)).astype('i8')
scores
In [151]:
scores.mean(axis=1).round(1)
Out[151]:
array([77.7, 78. , 84.3, 81.3, 80. , 79.7, 81.7, 82.3, 84. , 79.3, 83.7, 84.7, 77.3, 77.3, 74.7, 79.3, 78.3, 78. , 84. , 84.3, 72.7, 80.7, 83.3, 81. , 79. , 85.7, 79.3, 81.3, 79.3, 80. , 83.3, 83. , 77. , 82.3, 83. , 73. , 75.3, 89. , 82.7, 79.7, 84.7, 80.3, 80.7, 82.7, 81.3, 84. , 82.3, 77.3, 75.3, 81.3])
In [ ]:
sns.histplot(scores.mean(axis=1))
In [186]:
sns.histplot(scores)
Out[186]:
<Axes: ylabel='Count'>
In [173]:
#2.其他方法
temp5.all()
Out[173]:
np.False_
In [174]:
temp5.any()
Out[174]:
np.True_
In [177]:
temp = array1.astype('i1') #改变类型
temp
Out[177]:
array([1, 2, 3, 4, 5], dtype=int8)
In [178]:
temp.flags
Out[178]:
C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False
In [180]:
array2.reshape(3,2) #改变形状
Out[180]:
array([[1, 2], [3, 4], [5, 6]])
In [181]:
array2.reshape(3,-1) #行数3 列数-1(自动计算)
Out[181]:
array([[1, 2], [3, 4], [5, 6]])
In [ ]:
array2.tofile('文件路径/文件名.txt',sep=',')
In [ ]:
# 对数组做pickle序列化(python私有协议)再写入文件
array2.dump('array2')
#从二进制文件中加载数组
np.load('array2',allow_pickle=True)
In [7]:
# 扁平化
x1 = array2.flatten()
x1
Out[7]:
array([1, 2, 3, 4, 5, 6])
In [12]:
x2 = np.random.randn(5,10)
x2
Out[12]:
array([[-1.68764857, -0.04798852, 0.91272181, 0.08335143, -0.36884819, 0.37838781, 1.40753112, -0.42264744, 0.90991257, 0.93497078], [-0.24179816, -0.06994595, 0.02017362, -1.75619758, 0.52877254, -0.69140953, 0.91534159, -2.0193709 , 1.62590797, 0.41510055], [ 0.40307152, 0.92012627, 0.48625593, -0.14302143, -0.92413233, -0.91868188, 0.65125545, -0.18553757, -0.06828929, -0.33987201], [-0.58610355, -0.07520912, 1.04405195, 1.08412709, -0.9967844 , 0.00376826, -0.25211649, -2.06244384, -0.5263134 , 0.80276044], [-0.88544962, 1.52506836, -1.72173701, -0.69144751, -0.48194276, -0.36118217, -1.17024554, -2.85202519, -0.71468582, -1.37656889]])
In [14]:
x3 = x2.ravel() #不创建副本
x3
Out[14]:
array([-1.68764857, -0.04798852, 0.91272181, 0.08335143, -0.36884819, 0.37838781, 1.40753112, -0.42264744, 0.90991257, 0.93497078, -0.24179816, -0.06994595, 0.02017362, -1.75619758, 0.52877254, -0.69140953, 0.91534159, -2.0193709 , 1.62590797, 0.41510055, 0.40307152, 0.92012627, 0.48625593, -0.14302143, -0.92413233, -0.91868188, 0.65125545, -0.18553757, -0.06828929, -0.33987201, -0.58610355, -0.07520912, 1.04405195, 1.08412709, -0.9967844 , 0.00376826, -0.25211649, -2.06244384, -0.5263134 , 0.80276044, -0.88544962, 1.52506836, -1.72173701, -0.69144751, -0.48194276, -0.36118217, -1.17024554, -2.85202519, -0.71468582, -1.37656889])
In [16]:
x3.flags
Out[16]:
C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : False WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False
In [17]:
x3.base
Out[17]:
array([[-1.68764857, -0.04798852, 0.91272181, 0.08335143, -0.36884819, 0.37838781, 1.40753112, -0.42264744, 0.90991257, 0.93497078], [-0.24179816, -0.06994595, 0.02017362, -1.75619758, 0.52877254, -0.69140953, 0.91534159, -2.0193709 , 1.62590797, 0.41510055], [ 0.40307152, 0.92012627, 0.48625593, -0.14302143, -0.92413233, -0.91868188, 0.65125545, -0.18553757, -0.06828929, -0.33987201], [-0.58610355, -0.07520912, 1.04405195, 1.08412709, -0.9967844 , 0.00376826, -0.25211649, -2.06244384, -0.5263134 , 0.80276044], [-0.88544962, 1.52506836, -1.72173701, -0.69144751, -0.48194276, -0.36118217, -1.17024554, -2.85202519, -0.71468582, -1.37656889]])
In [19]:
x4 = x2.flatten() #创建副本
x4
Out[19]:
array([-1.68764857, -0.04798852, 0.91272181, 0.08335143, -0.36884819, 0.37838781, 1.40753112, -0.42264744, 0.90991257, 0.93497078, -0.24179816, -0.06994595, 0.02017362, -1.75619758, 0.52877254, -0.69140953, 0.91534159, -2.0193709 , 1.62590797, 0.41510055, 0.40307152, 0.92012627, 0.48625593, -0.14302143, -0.92413233, -0.91868188, 0.65125545, -0.18553757, -0.06828929, -0.33987201, -0.58610355, -0.07520912, 1.04405195, 1.08412709, -0.9967844 , 0.00376826, -0.25211649, -2.06244384, -0.5263134 , 0.80276044, -0.88544962, 1.52506836, -1.72173701, -0.69144751, -0.48194276, -0.36118217, -1.17024554, -2.85202519, -0.71468582, -1.37656889])
In [20]:
x4.flags
Out[20]:
C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False
In [21]:
#x4.sort() #直接改变X4的顺序
np.sort(x4) #返回排序后的新内容,不影响x4原顺序
Out[21]:
array([-2.85202519, -2.06244384, -2.0193709 , -1.75619758, -1.72173701, -1.68764857, -1.37656889, -1.17024554, -0.9967844 , -0.92413233, -0.91868188, -0.88544962, -0.71468582, -0.69144751, -0.69140953, -0.58610355, -0.5263134 , -0.48194276, -0.42264744, -0.36884819, -0.36118217, -0.33987201, -0.25211649, -0.24179816, -0.18553757, -0.14302143, -0.07520912, -0.06994595, -0.06828929, -0.04798852, 0.00376826, 0.02017362, 0.08335143, 0.37838781, 0.40307152, 0.41510055, 0.48625593, 0.52877254, 0.65125545, 0.80276044, 0.90991257, 0.91272181, 0.91534159, 0.92012627, 0.93497078, 1.04405195, 1.08412709, 1.40753112, 1.52506836, 1.62590797])
In [22]:
array2.transpose() #列变行,行变列
Out[22]:
array([[1, 4], [2, 5], [3, 6]])
In [26]:
plt.imshow(guido.swapaxes(0,1))
plt.show()
In [ ]:
x5 = array1.tolist() #将数组变成列表