十月学习打卡-numpy-2

十月学习打卡-numpy-2

import numpy as np
import random

1.调整数组的形状

four = np.array([[1,2,3],[4,5,6]])
# 修改的是原有的
four.shape = (3,2)
print(four)
[[1 2]
 [3 4]
 [5 6]]
# 返回一个新的数组
four_other = np.array([[1,2,3],[4,5,6]])
four_other_new = four.reshape(3,2)
print(four_other)
print(four_other_new)
[[1 2 3]
 [4 5 6]]
[[1 2]
 [3 4]
 [5 6]]
# 将多维变成一维数组
five = four.reshape((6,),order='F')
# 默认情况下‘C’以行为主的顺序展开,‘F’(Fortran风格)意味着以列的顺序展开
six = four.flatten(order='C')
print(five)
print(six)
[1 3 5 2 4 6]
[1 2 3 4 5 6]

1.1 多维数组的形状

t = np.arange(24)
print(t)
print("shape:",t.shape)
# 转换成二维
t1 = t.reshape((4,6))
print(t1)
print("shape:",t1.shape)
# 转成三维
t2 = t.reshape((2,3,4))
print(t2)
print("shape:",t2.shape)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
shape: (24,)
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
shape: (4, 6)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
shape: (2, 3, 4)

tolist() 数组转list

a= np.array([9, 12, 88, 14, 25])
list_a = a.tolist()
print(list_a)
print(type(list_a))
[9, 12, 88, 14, 25]
<class 'list'>

1.2 NumPy 的数据类型

dtype 获取数据类型
astype 改变数据类型

f = np.array([1,2,3,4,5], dtype = np.int16)
# 返回数组中每个元素的字节单位长度
print(f.itemsize)
# 获取数据类型
print(f.dtype)
# 调整数据类型
f1 = f.astype(np.int64)
print(f1.dtype)
2
int16
int64
# 随机生成小数
# 使用python语法,保留两位
print(round(random.random(),2))
#随机生成十个数字
arr = np.array([random.random() for i in range(10)])
print(arr)
# 取小数点后两位
print(np.round(arr,2))
0.93
[0.57458443 0.35506316 0.23451839 0.66950003 0.47128081 0.55178002
 0.48226117 0.33951354 0.855257   0.15476563]
[0.57 0.36 0.23 0.67 0.47 0.55 0.48 0.34 0.86 0.15]

2.数组的副本操作

numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x
y[0] = -1
print(x)
# [-1  2  3  4  5  6  7  8]
print(y)
# [-1  2  3  4  5  6  7  8]

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x.copy()
y[0] = -1
print(x)
# [1 2 3 4 5 6 7 8]
print(y)
# [-1  2  3  4  5  6  7  8]

【例】数组切片操作返回的对象只是原数组的视图。

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x
# ::2  代表每隔两行取一次
# :3:2  表示从第0列到第3列,但是不包括第三列,每隔两列取一次
y[::2, :3:2] = -555
print(x)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]
print(y)
# [[-555   12 -555   14   15]
#  [  16   17   18   19   20]
#  [-555   22 -555   24   25]
#  [  26   27   28   29   30]
#  [-555   32 -555   34   35]]

【例】

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.copy()
y[::2, :3:2] = -1
print(x)
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
print(y)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]

3.索引与切片

一维数组的操作方法:冒号分隔切片参数 [start:stop:step] 来进行切片操作

a = np.arange(10)
print(a)

print(a[2:7:2])# 从索引 2 开始到索引 7 停止,间隔为 2
# 如果只放置一个参数,如 [2],将返回与该索引相对应的单个元素
print(a[2],a)
# 如果为 [2:],表示从该索引开始以后的所有项都将被提取
print(a[2:])
[0 1 2 3 4 5 6 7 8 9]
[2 4 6]
2 [0 1 2 3 4 5 6 7 8 9]
[2 3 4 5 6 7 8 9]

3.1多维数组的操作方法

t1 = np.arange(24).reshape(4,6)
print(t1)
print('*'*20)
print(t1[1]) # 取一行(一行代表是一条数据,索引也是从0开始的)
print('*'*20)
print(t1[1,:]) # 取一行
print('*'*20)
print(t1[1:])# 取连续的多行
print('*'*20)
print(t1[1:3,:])# 取连续的多行
print('*'*20)
print(t1[[0,2,3]])# 取不连续的多行
print('*'*20)
print(t1[[0,2,3],:])# 取不连续的多行
print('*'*20)
print(t1[:,1])# 取一列
print('*'*20)
print(t1[:,1:])# 连续的多列
print('*'*20)
print(t1[:,[0,2,3]])# 取不连续的多列
print('*'*20)
print(t1[2,3])# # 取某一个值,三行四列
print('*'*20)
print(t1[[0,1,1],[0,1,3]])# 取多个不连续的值,[[行,行。。。],[列,列。。。]]
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
********************
[ 6  7  8  9 10 11]
********************
[ 6  7  8  9 10 11]
********************
[[ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
********************
[[ 6  7  8  9 10 11]
 [12 13 14 15 16 17]]
********************
[[ 0  1  2  3  4  5]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
********************
[[ 0  1  2  3  4  5]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
********************
[ 1  7 13 19]
********************
[[ 1  2  3  4  5]
 [ 7  8  9 10 11]
 [13 14 15 16 17]
 [19 20 21 22 23]]
********************
[[ 0  2  3]
 [ 6  8  9]
 [12 14 15]
 [18 20 21]]
********************
15
********************
[0 7 9]

3.2数组中的数值修改

t = np.arange(24).reshape(4,6)
# 修改某一行的值
t[1,:]=0
# 修改某一列的值
t[:,1]=0
# 修改连续多行
t[1:3,:]=0
# 修改连续多列
t[:,1:4]=0
# 修改多行多列,取第二行到第四行,第三列到第五列
t[1:4,2:5]=0
# 修改多个不相邻的点
t[[0,1],[0,3]]=0
# 可以根据条件修改,比如讲小于10的值改掉
t[t<10]=0
t
array([[ 0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0],
       [18,  0,  0,  0,  0, 23]])
# 使用逻辑判断
# np.logical_and  &
# np.logical_or    |
# np.logical_not   ~
t[(t>2)&(t<6)]=1# 与
t[(t<2)|(t>6)]=0# 或
t[~(t>6)]=0# 非
print(t)
# 拓展
# 三目运算( np.where(condition, x, y)满足条件(condition),输出x,不满足输出y。))
score = np.array([[80,88],[82,81],[75,81]])
result = np.where(score>80,True,False)
print(result)
[[0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]]
[[False  True]
 [ True  True]
 [False  True]]

3.3 dots 索引

NumPy 允许使用…表示足够多的冒号来构建完整的索引列表。

比如,如果 x 是 5 维数组:

x[1,2,…] 等于 x[1,2,:,:,:]

x[…,3] 等于 x[:,:,:,:,3]

x[4,…,5,:] 等于 x[4,:,:,5,:]

[例]

x = np.random.randint(1, 100, [2, 2, 3])
print(x)
# [[[69 65 40]
#   [51 78 71]]
#  [[52 48 63]
#   [28 84 64]]]

print(x[1, ...])
# [[52 48 63]
#  [28 84 64]]

print(x[..., 2])
# [[40 71]
#  [63 64]]
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
#第0行第1列
#第1行第2列
#第2行第2列
y = x[0:3, [1, 2, 2]]
print(y)
[[12 13 13]
 [17 18 18]
 [22 23 23]]

应注意:使用切片索引到numpy数组时,生成的数组视图将始终是原始数组的子数组, 但是整数数组索引,不是其子数组,是形成新的数组。

#切片索引
a=np.array([[1,2],[3,4],[5,6]])
b=a[0:1,0:1]
b[0,0]=2
print(a[0,0]==b)
#[[True]]
[[ True]]
# 整数数组索引
a=np.array([[1,2],[3,4],[5,6]])
b=a[0,0]
b=2
print(a[0,0]==b)
#False
False

3.4布尔索引

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 5
print(y)
# [False False False False False  True  True  True]
print(x[x > 5])
# [6 7 8]
x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])
y = np.logical_not(np.isnan(x))
print(x)
print(x[y])
[nan  1.  2. nan  3.  4.  5.]
[1. 2. 3. 4. 5.]
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x > 25
print(y)
[[False False False False False]
 [False False False False False]
 [False False False False False]
 [ True  True  True  True  True]
 [ True  True  True  True  True]]
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
print(len(x))  # 50
plt.plot(x, y)
50

在这里插入图片描述

mask = y >= 0
print(len(x[mask]))  # 25
print(mask)
plt.plot(x[mask], y[mask], 'bo')
25
[ True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True False False False False False False False False False False False
 False False False False False False False False False False False False
 False False]

在这里插入图片描述

mask = np.logical_and(y >= 0, x <= np.pi / 2)
print(mask)
plt.plot(x[mask], y[mask], 'go')
plt.show()
[ True  True  True  True  True  True  True  True  True  True  True  True
  True False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False]

在这里插入图片描述

4.数组迭代

apply_along_axis(func, axis, arr) Apply a function to 1-D slices along the given axis.

函数func(arr)中的arr是一个数组,函数的主要功能就是对数组里的每一个元素进行变换,得到目标的结果。

其中axis表示函数func对数组arr作用的轴

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

y = np.apply_along_axis(np.sum, 0, x)
print(y)  # [105 110 115 120 125]
y = np.apply_along_axis(np.sum, 1, x)
print(y)  # [ 65  90 115 140 165]

y = np.apply_along_axis(np.mean, 0, x)
print(y)  # [21. 22. 23. 24. 25.]
y = np.apply_along_axis(np.mean, 1, x)
print(y)  # [13. 18. 23. 28. 33.]


def my_func(x):
    return (x[0] + x[-1]) * 0.5


y = np.apply_along_axis(my_func, 0, x)
print(y)  # [21. 22. 23. 24. 25.]
y = np.apply_along_axis(my_func, 1, x)
print(y)  # [13. 18. 23. 28. 33.]
[105 110 115 120 125]
[ 65  90 115 140 165]
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
本程序是在python中完成,基于sklearn.cluster中的k-means聚类包来实现数据的聚类,对于里面使用的数据格式如下:(注意更改程序中的相关参数) 138 0 124 1 127 2 129 3 119 4 127 5 124 6 120 7 123 8 147 9 188 10 212 11 229 12 240 13 240 14 241 15 240 16 242 17 174 18 130 19 132 20 119 21 48 22 37 23 49 0 42 1 34 2 26 3 20 4 21 5 23 6 13 7 19 8 18 9 36 10 25 11 20 12 19 13 19 14 5 15 29 16 22 17 13 18 46 19 15 20 8 21 33 22 41 23 69 0 56 1 49 2 40 3 52 4 62 5 54 6 32 7 38 8 44 9 55 10 70 11 74 12 105 13 107 14 56 15 55 16 65 17 100 18 195 19 136 20 87 21 64 22 77 23 61 0 53 1 47 2 33 3 34 4 28 5 41 6 40 7 38 8 33 9 26 10 31 11 31 12 13 13 17 14 17 15 25 16 17 17 17 18 14 19 16 20 17 21 29 22 44 23 37 0 32 1 34 2 26 3 23 4 25 5 25 6 27 7 30 8 25 9 17 10 12 11 12 12 12 13 7 14 6 15 6 16 12 17 12 18 39 19 34 20 32 21 34 22 35 23 33 0 57 1 81 2 77 3 68 4 61 5 60 6 56 7 67 8 102 9 89 10 62 11 57 12 57 13 64 14 62 15 69 16 81 17 77 18 64 19 62 20 79 21 75 22 57 23 73 0 88 1 75 2 70 3 77 4 73 5 72 6 76 7 76 8 74 9 98 10 90 11 90 12 85 13 79 14 79 15 88 16 88 17 81 18 84 19 89 20 79 21 68 22 55 23 63 0 62 1 58 2 58 3 56 4 60 5 56 6 56 7 58 8 56 9 65 10 61 11 60 12 60 13 61 14 65 15 55 16 56 17 61 18 64 19 69 20 83 21 87 22 84 23 41 0 35 1 38 2 45 3 44 4 49 5 55 6 47 7 47 8 29 9 14 10 12 11 4 12 10 13 9 14 7 15 7 16 11 17 12 18 14 19 22 20 29 21 23 22 33 23 34 0 38 1 38 2 37 3 37 4 34 5 24 6 47 7 70 8 41 9 6 10 23 11 4 12 15 13 3 14 28 15 17 16 31 17 39 18 42 19 54 20 47 21 68 22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值