NumPy学习7
今天学习了:
13, NumPy字符串处理函数
14, NumPy数学函数
15, NumPy算术运算
numpy_test7.py :
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | import numpy as np ''' 13, NumPy字符串处理函数 NumPy处理字符串数组函数 函数名称 描述 add() 对两个数组相应位置的字符串做连接操作。 multiply() 返回多个字符串副本,比如将字符串“ hello”乘以3,则返回字符串“ hello hello hello”。 center() 用于居中字符串,并将指定的字符,填充在原字符串的左右两侧。 capitalize() 将字符串第一个字母转换为大写。 title() 标题样式,将每个字符串的第一个字母转换为大写形式。 lower() 将数组中所有的字符串的大写转换为小写。 upper() 将数组中所有的字符串的小写转换为大写。 split() 通过指定分隔符对字符串进行分割,并返回一个数组序列,默认分隔符为空格。 splitlines() 以换行符作为分隔符来分割字符串,并返回数组序列。 strip() 删除字符串开头和结尾处的空字符。 join() 返回一个新的字符串,该字符串是以指定分隔符来连接数组中的所有元素。 replace() 用新的字符串替换原数组中指定的字符串。 decode() 用指定的编码格式对数组中元素依次执行解码操作。 encode() 用指定的编码格式对数组中元素依次执行编码操作。 上述函数基于 Python 内置的字符串函数实现, 下面对一些常用函数进行讲解。 ''' ''' 1) numpy.char.add() numpy.char.add() 将两个数组对应位置的字符串元素进行连接。 ''' print ( "----1) numpy.char.add()----" ) arr1 = [ 'Hello ' , 'Welcome ' ] arr2 = [ 'World' , 'PandaCode' ] print ( 'np.char.add(arr1, arr2) : ' , np.char.add(arr1, arr2)) ''' np.char.add(arr1, arr2) : ['Hello World' 'Welcome PandaCode'] ''' ''' 2) numpy.char.multiply() 该函数将指定的字符串进行多次拷贝,并将拷贝结果返回。 ''' print ( "----2) numpy.char.multiply()----" ) str1 = ' PandaCode ' print ( 'str1 : ' , str1) str2 = np.char.multiply(str1, 3 ) print ( 'np.char.multiply(str1, 3) : ' , str2) ''' str1 : PandaCode np.char.multiply(str1, 3) : PandaCode PandaCode PandaCode ''' ''' 3) numpy.char.center() numpy.char.center() 用于居中字符串,其语法格式如下: np.char.center(string, width, fillchar) string: 代表字符串, width: 表示长度, fillchar: 要填充的字符. ''' print ( "----3) numpy.char.center()----" ) str3 = 'PandaCode' print ( 'str3 : ' , str3) str4 = np.char.center(str3, 20 , '*' ) print ( 'np.char.center(string, width, fillchar) : ' , str4) ''' str3 : PandaCode np.char.center(string, width, fillchar) : *****PandaCode****** ''' ''' 4) numpy.char.capitalize() numpy.char.capitalize() 将字符串的第一个字母转换为大写 ''' print ( "----4) numpy.char.capitalize()----" ) print ( 'numpy.char.capitalize() : ' , np.char.capitalize( 'numpy' )) ''' numpy.char.capitalize() : Numpy ''' ''' 5) numpy.char.title() numpy.char.title() 将字符串数组中每个元素的第一个字母转换为大写 ''' print ( "----5) numpy.char.title()----" ) print ( 'numpy.char.title() : ' , np.char.title( "hello world" )) ''' numpy.char.title() : Hello World ''' ''' 6) numpy.char.lower() numpy.char.lower() 将字符串数组中每个元素转换为小写 ''' print ( "----6) numpy.char.lower()----" ) print ( 'numpy.char.lower() : ' , np.char.lower( "WELCOME TO China" )) ''' numpy.char.lower() : welcome to china ''' ''' 7) numpy.char.upper() numpy.char.upper() 将数组中的每个元素转换为大写 ''' print ( "----7) numpy.char.upper()----" ) print ( 'numpy.char.upper() : ' , np.char.upper( "welcome To china" )) ''' numpy.char.upper() : WELCOME TO CHINA ''' ''' 8) numpy.char.split() 该函数通过指定分隔符对字符串进行分割,并返回数组序列。默认情况下,分隔符为空格。 ''' print ( "----8) numpy.char.split()----" ) print ( 'numpy.char.split() : ' , np.char.split( "hello world" ), sep = " " ) ''' numpy.char.split() : ['hello', 'world'] ''' ''' 9) numpy.char.splitlines() numpy.char.splitlines() 以换行符作为分隔符来分割字符串,并返回一个数组序列。 ''' print ( "----9) numpy.char.splitlines()----" ) print ( 'numpy.char.splitlines() : ' , np.char.splitlines( "welcome\nTo\nchina\n" )) ''' numpy.char.splitlines() : ['welcome', 'To', 'china'] ''' ''' 10) numpy.char.strip() numpy.char.strip() 用于移除开头或结尾处的空格。 ''' print ( "----10) numpy.char.strip()----" ) str5 = " Welcome To China " print ( "原字符串 str5 : " , str5) str6 = np.char.strip(str5) print ( "np.char.strip(str5) : " , str6) ''' 原字符串 str5 : Welcome To China np.char.strip(str5) : Welcome To China ''' ''' 11) numpy.char.join() numpy.char.join() 通过指定的分隔符来连接数组中的元素或字符串。 ''' print ( "----11) numpy.char.join()----" ) print ( "numpy.char.join() : " , np.char.join( '-' , 'PandaCode' )) # 也可指定多个分隔符 print ( "numpy.char.join() : " , np.char.join([ ':' , '*' ], [ 'hello' , 'world' ])) ''' numpy.char.join() : P-a-n-d-a-C-o-d-e numpy.char.join() : ['h:e:l:l:o' 'w*o*r*l*d'] ''' ''' 12) numpy.char.replace() numpy.char.replace() 使用新字符替换字符串中的指定字符。 ''' print ( "----12) numpy.char.replace()----" ) str7 = "Welcome to China" print ( "原字符串 str7 : " , str7) # 替换更改后字符串 print ( "numpy.char.replace() : " , np.char.replace(str7, "Welcome to" , "Hello" )) ''' 原字符串 str7 : Welcome to China numpy.char.replace() : Hello China ''' ''' 13) numpy.char.encode() 与 numpy.char.decode() 默认以utf-8的形式进行编码与解码 ''' print ( "----13) numpy.char.encode() 与 numpy.char.decode() ----" ) # cp500国际编码 encode_str = np.char.encode( "Hello World" , 'cp500' ) print ( "numpy.char.encode() : " , encode_str) decode_str = np.char.decode(encode_str, 'cp500' ) print ( "numpy.char.decode() : " , decode_str) ''' numpy.char.encode() : b'\xc8\x85\x93\x93\x96@\xe6\x96\x99\x93\x84' numpy.char.decode() : Hello World ''' ''' 14, NumPy数学函数 NumPy 中包含了大量的数学函数,它们用于执行各种数学运算,其中包括三角函数、舍入函数等等。 ''' ''' (1) 三角函数 NumPy 中提供了用于弧度计算的的 sin()(正弦)、cos()(余弦)和 tan()(正切)三角函数。 ''' print ( "----14, NumPy数学函数----" ) print ( "----(1) 三角函数----" ) arr = np.array([ 0 , 30 , 60 , 90 , 120 , 150 , 180 ]) # 计算arr数组中给定角度的三角函数值 # 通过乘以np.pi/180将其转换为弧度 print ( "np.sin() : " , np.sin(arr * np.pi / 180 )) print ( "np.cos() : " , np.cos(arr * np.pi / 180 )) print ( "np.tan() : " , np.tan(arr * np.pi / 180 )) ''' np.sin() : [0.00000000e+00 5.00000000e-01 8.66025404e-01 1.00000000e+00 8.66025404e-01 5.00000000e-01 1.22464680e-16] np.cos() : [ 1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17 -5.00000000e-01 -8.66025404e-01 -1.00000000e+00] np.tan() : [ 0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16 -1.73205081e+00 -5.77350269e-01 -1.22464680e-16] 除了上述三角函数以外,NumPy 还提供了 arcsin,arcos 和 arctan 反三角函数。 ''' print ( "----反三角函数----" ) # 若要想验证反三角函数的结果,可以通过 numpy.degrees() 将弧度转换为角度来实现 arr1 = np.array([ 0 , 30 , 60 , 90 ]) print ( "arr1 : " , arr1) # 正弦值数组 sinval = np.sin(arr1 * np.pi / 180 ) print ( "sinval : " , sinval) # 计算角度反正弦,返回值以弧度为单位 cosec = np.arcsin(sinval) print ( "cosec : " , cosec) # 通过degrees函数转化为角度进行验证 print ( "np.degrees(cosec) : " , np.degrees(cosec)) #余弦值数组 cosval = np.cos(arr1 * np.pi / 180 ) print ( "cosval : " , cosval) # 计算反余弦值,以弧度为单位 sec = np.arccos(cosval) print ( "sec : " , sec) # 通过degrees函数转化为角度进行验证 print ( "np.degrees(sec) : " , np.degrees(sec)) # 下面是tan()正切函数 tanval = np.tan(arr1 * np.pi / 180 ) print ( "tanval : " , tanval) cot = np.arctan(tanval) print ( "cot : " , cot) print ( "np.degrees(cot) : " , np.degrees(cot)) ''' arr1 : [ 0 30 60 90] sinval : [0. 0.5 0.8660254 1. ] cosec : [0. 0.52359878 1.04719755 1.57079633] np.degrees(cosec) : [ 0. 30. 60. 90.] cosval : [1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17] sec : [0. 0.52359878 1.04719755 1.57079633] np.degrees(sec) : [ 0. 30. 60. 90.] tanval : [0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16] cot : [0. 0.52359878 1.04719755 1.57079633] np.degrees(cot) : [ 0. 30. 60. 90.] ''' ''' (2) 舍入函数 NumPy 提供了三个舍入函数,介绍如下: 1) numpy.around() 该函数返回一个十进制值数,并将数值四舍五入到指定的小数位上。该函数的语法如下: numpy.around(a,decimals) 参数说明: a:代表要输入的数组; decimals:要舍入到的小数位数。它的默认值为0,如果为负数,则小数点将移到整数左侧。 ''' print ( "----(2) 舍入函数----" ) print ( "----1) numpy.around() ----" ) arr2 = np.array([ 12.232 , 90.26720 , 163.620 , 27.282 ]) print ( "原数组 arr2 : " , arr2) print ( "数组值四舍五入到小数点后2位 : " , np.around(arr2, 2 )) print ( "数组值四舍五入到小数点后1位 : " , np.around(arr2, 1 )) print ( "数组值四舍五入到小数点后0位 : " , np.around(arr2)) print ( "数组值四舍五入到小数点后-1位 : " , np.around(arr2, - 1 )) ''' 原数组 arr2 : [ 12.232 90.2672 163.62 27.282 ] 数组值四舍五入到小数点后2位 : [ 12.23 90.27 163.62 27.28] 数组值四舍五入到小数点后1位 : [ 12.2 90.3 163.6 27.3] 数组值四舍五入到小数点后0位 : [ 12. 90. 164. 27.] 数组值四舍五入到小数点后-1位 : [ 10. 90. 160. 30.] ''' ''' 2) numpy.floor() 该函数表示对数组中的每个元素向下取整数,即返回不大于数组中每个元素值的最大整数。 ''' print ( "----2) numpy.floor()----" ) arr3 = np.array([ 12.23 , 90.26 , 163.60 , 27.2 ]) print ( "原数组 arr3 : " , arr3) # 对数组向下取整 print ( "对数组向下取整, numpy.floor() : " , np.floor(arr3)) ''' 原数组 arr3 : [ 12.23 90.26 163.6 27.2 ] 对数组向下取整, numpy.floor() : [ 12. 90. 163. 27.] ''' ''' 3) numpy.ceil() 该函数与 floor 函数相反,表示向上取整。 ''' print ( "----3) numpy.ceil()----" ) # 对数组向下取整 print ( "对数组向下取整, numpy.ceil() : " , np.ceil(arr3)) ''' 对数组向下取整, numpy.ceil() : [ 13. 91. 164. 28.] ''' ''' 15, NumPy算术运算 NumPy 数组的“加减乘除”算术运算,分别对应 add()、subtract()、multiple() 以及 divide() 函数。 注意:做算术运算时,输入数组必须具有相同的形状,或者符合数组的广播规则,才可以执行运算。 ''' print ( "----15, NumPy算术运算----" ) print ( "----(1), NumPy数组的 加减乘除 算术运算----" ) arr_a = np.arange( 6 , dtype = np.float_).reshape( 2 , 3 ) # 数组a print ( "arr_a : " , arr_a) # 数组b arr_b = np.array([ 2 , 2 , 2 ]) print ( "arr_b : " , arr_b) # 数组加法运算 print ( "np.add() : " , np.add(arr_a, arr_b)) # 数组减法运算 print ( "np.subtract() : " , np.subtract(arr_a, arr_b)) # 数组乘法运算 print ( "np.multiply() : " , np.multiply(arr_a, arr_b)) # 数组除法运算 print ( "np.divide() : " , np.divide(arr_a, arr_b)) ''' arr_a : [[0. 1. 2.] [3. 4. 5.]] arr_b : [2 2 2] np.add() : [[2. 3. 4.] [5. 6. 7.]] np.subtract() : [[-2. -1. 0.] [ 1. 2. 3.]] np.multiply() : [[ 0. 2. 4.] [ 6. 8. 10.]] np.divide() : [ [0. 0.5 1. ] [1.5 2. 2.5]] ''' ''' (2) numpy.reciprocal() 该函数对数组中的每个元素取倒数,并以数组的形式将它们返回。 当数组元素的数据类型为整型(int)时,对于绝对值小于 1 的元素,返回值为 0, 而当数组中包含 0 元素时,返回值将出现 overflow(inf) 溢出提示。 ''' print ( "----(2) numpy.reciprocal()----" ) # 注意此处有0 arr_a = np.array([ 0.25 , 2.0 , 1 , 0 , 10 ]) # 数组a默认为浮点类型数据 print ( "arr_a : " , arr_a) # 对数组a使用求倒数操作 print ( "np.reciprocal(arr_a) : " , np.reciprocal(arr_a)) # b数组的数据类型为整形int arr_b = np.array([ 100 , 50 , 20 ], dtype = int ) print ( "arr_b : " , arr_b) # 对数组b使用求倒数操作 print ( "np.reciprocal(arr_b) : " , np.reciprocal(arr_b)) ''' arr_a : [ 0.25 2. 1. 0. 10. ] np.reciprocal(arr_a) : [4. 0.5 1. inf 0.1] arr_b : [100 50 20] np.reciprocal(arr_b) : [0 0 0] ''' ''' (3) numpy.power() 该函数将 a 数组中的元素作为底数,把 b 数组中与 a 相对应的元素作幂 ,最后以数组形式返回两者的计算结果。 ''' print ( "----(3) numpy.power()----" ) arr_a = np.array([ 2 , 10 , 20 ]) # a数组 print ( "arr_a : " , arr_a) # 调用 power 函数 print ( "np.power(arr_a, 2) : " , np.power(arr_a, 2 )) # b数组 arr_b = np.array([ 3 , 2 , 1 ]) print ( "arr_b : " , arr_b) # 调用 power 函数 print ( "np.power(arr_a, arr_b) : " , np.power(arr_a, arr_b)) ''' arr_a : [ 2 10 20] np.power(arr_a, 2) : [ 4 100 400] arr_b : [3 2 1] np.power(arr_a, arr_b) : [ 8 100 20] ''' ''' (4) numpy.mod() 返回两个数组相对应位置上元素相除后的余数,它与 numpy.remainder() 的作用相同 。 ''' print ( "----(4) numpy.mod()----" ) arr_a = np.array([ 12 , 23 , 34 ]) print ( "arr_a : " , arr_a) arr_b = np.array([ 3 , 5 , 7 ]) print ( "arr_b : " , arr_b) # a与b相应位置的元素做除法 print ( "np.mod(arr_a, arr_b) : " , np.mod(arr_a, arr_b)) # remainder方法一样 print ( "np.remainder(arr_a, arr_b) : " , np.remainder(arr_a, arr_b)) ''' arr_a : [12 23 34] arr_b : [3 5 7] np.mod(arr_a, arr_b) : [0 3 6] np.remainder(arr_a, arr_b) : [0 3 6] ''' ''' (5) 复数数组处理函数 NumPy 提供了诸多处理复数类型数组的函数,主要有以下几个: numpy.real() 返回复数数组的实部; numpy.imag() 返回复数数组的虚部; numpy.conj() 通过更改虚部的符号,从而返回共轭复数; numpy.angle() 返回复数参数的角度,该函数的提供了一个 deg 参数, 如果 deg=True,则返回的值会以角度制来表示,否则以以弧度制来表示。 ''' print ( "----(5) 复数数组处理函数----" ) arr5 = np.array([ - 2.6j , 0.3j , 12. , 5 + 1j ]) print ( "arr5 : " , arr5) # numpy.real() 返回复数数组的实部; print ( "np.real() : " , np.real(arr5)) # numpy.imag() 返回复数数组的虚部; print ( "np.imag() : " , np.imag(arr5)) # numpy.conj() 通过更改虚部的符号,从而返回共轭复数; print ( "np.conj() : " , np.conj(arr5)) # numpy.angle() 返回复数参数的角度 print ( "np.angle() : " , np.angle(arr5)) # numpy.angle() 返回复数参数的角度,deg=True,则返回的值会以角度制来表示 print ( "np.angle() : " , np.angle(arr5, deg = True )) ''' arr5 : [-0.-2.6j 0.+0.3j 12.+0.j 5.+1.j ] np.real() : [-0. 0. 12. 5.] np.imag() : [-2.6 0.3 0. 1. ] np.conj() : [-0.+2.6j 0.-0.3j 12.-0.j 5.-1.j ] np.angle() : [-1.57079633 1.57079633 0. 0.19739556] np.angle() : [-90. 90. 0. 11.30993247] ''' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)