Python

Dir

1,Numpy

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

object 数组或嵌套的数列
dtype 数组元素的数据类型,可选
copy 对象是否需要复制,可选
order 创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
subok 默认返回一个与基类类型一致的数组
ndmin 指定生成数组的最小维度

numpy.empty(shape, dtype = float, order = ‘C’)
numpy.zeros(shape, dtype = float, order = ‘C’) #默认为浮点数
numpy.ones(shape, dtype = None, order = ‘C’) #默认为浮点数

shape 数组形状
dtype 数据类型,可选
order 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。
注意 empty: − 数组元素为随机值,因为它们未初始化。

属性 说明

ndarray.ndim 秩,即轴的数量或维度的数量
ndarray.shape 数组的维度,对于矩阵,n 行 m 列
ndarray.size 数组元素的总个数,相当于 .shape 中 n*m 的值
ndarray.dtype ndarray 对象的元素类型
ndarray.itemsize ndarray 对象中每个元素的大小,以字节为单位
ndarray.flags ndarray 对象的内存信息
ndarray.real ndarray元素的实部
ndarray.imag ndarray 元素的虚部
ndarray.data 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。

numpy.asarray(a, dtype = None, order = None)

a 任意形式的输入参数,可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组

numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

buffer 可以是任意对象,会以流的形式读入。
dtype 返回数组的数据类型,可选
count 读取的数据数量,默认为-1,读取所有数据。
offset 读取的起始位置,默认为0。
注意:buffer 是字符串的时候,Python3 默认 str 是 Unicode 类型,所以要转成 bytestring 在原 str 前加上 b。

numpy.fromiter(iterable, dtype, count=-1)

iterable 可迭代对象
dtype 返回数组的数据类型
count 读取的数据数量,默认为-1,读取所有数据

NumPy 从数值范围创建数组

arange

numpy.arange(start, stop, step, dtype);start =< value <= stop

start 起始值,默认为0
stop 终止值(不包含)
step 步长,默认为1
dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。

linspace

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

start 序列的起始值
stop 序列的终止值,如果endpoint为true,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。
dtype ndarray 的数据类型

np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
用于创建一个于等比数列

start 序列的起始值为:base ** start , ** 代表乘方
stop 序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
base 对数 log 的底数。
dtype ndarray 的数据类型

NumPy 切片和索引

import numpy as np

a = np.arange(10)
s = a[slice(2,7,2)]

print (s)

b = a[2:7:2]
print(b)

NumPy 高级索引

整数数组索引:

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

#行索引是 [0,0] 和 [3,3],而列索引是 [0,2] 和 [0,2]。
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  
print (x)

rows = np.array([[0,0],[3,3]]) 
print(rows);

cols = np.array([[0,2],[0,2]])
print(cols);

y = x[rows,cols]  
print (y)

布尔索引:
布尔索引通过布尔运算(如:比较运算符)来获取符合指定条件的元素的数组

import numpy as np 
 
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  
print ('我们的数组是:')
print (x)
print ('\n')
# 现在我们会打印出大于 5 的元素  
print  ('大于 5 的元素是:')
print (x[x >  5])

以下实例使用了 ~(取补运算符)来过滤 NaN。

a = np.array([np.nan,  1,2,np.nan,3,4,5])  
print (a[~np.isnan(a)])

过滤掉非复数元素

a = np.array([1,  2+6j,  5,  3.5+5j])  
print (a[np.iscomplex(a)])

花式索引:
花式索引指的是利用整数数组进行索引。

1、传入顺序索引数组

import numpy as np 
 
x=np.arange(32).reshape((8,4))
print (x[[4,2,1,7]])
#output:
#[[16 17 18 19]
#[ 8  9 10 11]
#[ 4  5  6  7]
#[28 29 30 31]]

2、传入倒序索引数组

import numpy as np 
 
x=np.arange(32).reshape((8,4))
print (x[[-4,-2,-1,-7]])
#output
#[[16 17 18 19]
#[24 25 26 27]
#[28 29 30 31]
#[ 4  5  6  7]]

3,传入多个索引数组(要使用np.ix_)

import numpy as np 
 
x=np.arange(32).reshape((8,4))
print (x[np.ix_([1,5,7,2],[0,3,1,2])])
#output:
#[[ 4  7  5  6]
#[20 23 21 22]
#[28 31 29 30]
#[ 8 11  9 10]]

NumPy 广播(Broadcast)
广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式

import numpy as np 
 
a = np.array([[ 0, 0, 0],
           [10,10,10],
           [20,20,20],
           [30,30,30]])
b = np.array([1,2,3])
#触发广播机制
#bb = np.tile(b, (4, 1))
# a+b == a+bb 
print(a + b)
#output:
#[[ 1  2  3]
#[11 12 13]
#[21 22 23]
#[31 32 33]]

广播的规则

NumPy 迭代数组
迭代器对象 numpy.nditer 提供了一种灵活访问一个或者多个数组元素的方式。

选择的顺序是和数组内存布局一致的

import numpy as np

a = np.arange(6).reshape([2,3]);
b = a.T;

for x in np.nditer(a):
	print (x)
print('\n');

for x in np.nditer(b):
	print(x)
print('\n');
#a,b,print same result,选择的顺序是和数组内存布局一致的
c = b.copy(order='C');
for x in np.nditer(c):
	print(x)
#选择的顺序

修改数组中元素的值:
nditer 对象有另一个可选参数 op_flags,必须指定 read-write 或者 write-only 的模式

for x in np.nditer(a, op_flags=['readwrite']): 
    x[...]=2*x;

广播迭代:
两个数组是可广播的

for x,y in np.nditer([a,b]):  
    print ("%d:%d"  %  (x,y), end=", " )

Numpy 字符串操作
numpy.char

add():对两个数组的逐个元素进行相加

import numpy as np 
 
print ('连接两个字符串:')
print (np.char.add(['hello'],[' xyz']))
print ('\n')
 
print ('连接示例:')
print (np.char.add(['hello', 'hi'],[' abc', ' xyz']))
#输出结果为:
#连接两个字符串:
#['hello xyz']
#连接示例:
#['hello abc' 'hi xyz']

multipy():字符重复连接

center():将Runoob在20个字符居中放置

capitalize()/title()

首字母大写

区别:
capitlize()将字符串的第一个字母大写,title()将字符串中每个单词进行大写

print (np.char.center('Runoob', 20,fillchar = '*'))

lower()/upper():

每个元素转为小写/大写

strip(),lstrip(),rstrip()

strip():去除字符串两端的空格
lstrip():去除字符串左端的空格
rstrip():去除字符串右端的空格

列表

常用函数

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
min(digits)
max(digits)
sum(digits)

digits.append(value**2)

even_numbers = list(range(2,11,2))
even_numbers.sort()#排序,字母
even_numbers.sort(reverse=True)#倒叙
even_numbers_new = even_numbers.sorted()

列表解析

squares = [value**2 for value in range(1,11)]

切片

制定第一个元素和最后一个元素,包含关系[startIdx,endIdx)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值