1.numpy
>array 数组
import numpy as np
w = np.array([1,2,3,4]) # 将列表转化为numpy的数组
# ([1,2,3,4]) 一维数组
w = np.array([[1,2,3],[4,5,6]]) #
# ([[1,2,3],
[4,5,6]])
>arange() #类似range
np.arange(6) #([0,1,2,3,4,5])
>ones() #全1数组
np.ones(3) # ([1,1,1])
np.ones((2,3)) # ([[1,1,1],
[1,1,1])
>zeros() #同上,全0数组
>identity(2) #单位矩阵 2*2
>np.random.rand(10) # ([]) 10个随机数的数组
>np.random.randint(start,end,shape) 从start到end之间的随机数
>shape是一个数,表示一维数组;如果是(x,y) 表示矩阵
>np.diag([1,2,3,4]) # 列表是对角矩阵的对角线元素,其余为0
>广播机制
a = np.array([1,2,3])
b = np.array([2,3,4])
a + b # ([3,5,7]) #对应位置相加
a * b # ([2,6,12]) #对应位置相乘
c = np.array([[1,2,3],[1,2,3],[1,2,3]])
a * c # 先把a从1*3的维度变成3*3,然后每一行对应位置同一维度情况向运算
#([[1,4,9],[1,4,9],[1,4,9]])
> 一维数组排序
w = np.array([3,1,2])
w.sort() # w发生改变,([1,2,3])
>矩阵排序
w = np.array([[3,1,2],[1,6,5]])
w.sort(axis = 1) #每行内排序
([[1,2,3],[1,5,6]])
>内积运算
x = np.array([1,2,3])
y= np.array([4,5,6])
x.dot(y) # 1*4 + 2*5 +3*6
>堆叠数组
>np.hstack(()) , np.vstack(())
x = np.array([1,2,3])
y = np.array([4,5,6])
z = np.hstack((x,y)) # ([1,2,3,4,5,6])
z = np.vstack((x,y)) #([[1,2,3],[4,5,6]]) #水平方向
>矩阵的数学运算
x = np.zeros((2,3)) # 2*3的矩阵
y = np.matrix([[1,2,3],[4,5,6]]) # 2*3的矩阵
x.max() #0
y.max() #6
y.mean() #对y所有元素求平均值
y.mean(axis=0) # 0表示纵向,按列,求平均值,([2.5,3.5,4.5])
y.mean(axis=1) # 1表示横向,按行,求平均值,([[2],[5]])
>max(axis = None,out = None)
对于m*n的矩阵,axis = 0 表示按行压扁,最终得到1*n的矩阵
axis = 1 表示按列压扁,最终得到m*1的矩阵
y = np.matrix([[1,2,3],[4,5,6]]) # 2*3的矩阵
y.max(axis = 0) # [[4,5,6]] #按行压扁 ,1*3的矩阵
y.max(axis = 1) # [[3],[6]]#按列压扁,2*1的矩阵
>x.shape = 4,-1 #表示4行,-1表示自动计算
2.pandas
>Series,一维数组,每个元素由key:value组成,其中key是索引,不指定key则默认从0开始
>pd.Series([]) 直接用列表,不指定key
>pd.Series({key1:value1,key2:value2}) 以字典的形式指定key
import pandas as pd
w = pd.Series(range(1,20,5)) #不指定索引key
#----
0 1
1 6
2 11
3 16
#----
w = pd.Series({"eng":90,"math":100,"computer":88})
#---- 排序时按照key的字典序
computer 88
eng 90
math 100
#----
>索引
w.index
>获取值,而非序列
w.values #返回array
>.add_prefix()
#Series索引加前缀后后缀,并不是改变它本身,只是在输出时临时作用
w = pd.Series({"eng":90,"math":100,"computer":88,"python":90})
w.add_prefix(2)# 并不会加上前缀
print(w.add_prefix("china")) #输出时会补上前缀
#---- 排序时按照key的字典序
2computer 88
2eng 90
2math 100
2python 90
#----
w.add_suffix('_tx')
#---- 排序时按照key的字典序
2computer_tx 88
2eng_tx 90
2math_tx 100
2python_tx 90
#----
>对一维数组,值的直方图统计,相同值计数加1
w.hist()
import matplotlib.pyplot as plt
plt.show()
>date_range(start,end,periods=None,freq='D') #时间序列 D表示day,T表示time 分钟,H小时
# 以5天为间隔生成序列
print(pd.date_range(start='20220101',end='20220201',freq='5D'))
DatatimeIndex(['2022-01-01','2022-01-06',...])
# 6点开始,间隔1分钟,12个数据
pd.date_range(start='202201010600',periods=12,freq='T')
# 用时间序列作为Series的索引,指定索引和data
w =pd.Series(index=pd.date_range(start='20220101',periods=24,freq='D'),data=range(24))
#
2022-01-01 0
2022-01-02 1
...
>DataFrame 二维数组,看做二维表格,数据帧
>行名索引(index),列名(columns),值(value)矩阵或者Serise
>pd.DataFrame(矩阵,index=列表, columns=列表)
w = pd.DataFrame(np.random.randint(1,20,(4,5))),index=['A','B','C','D'],columns=range(5))
#其中randint()是生成4*5的矩阵,元素值在1~20之间 ,下面只给出2个元素值
0 1 2 3 4 5
A 12
B 17
C
D
# 一个成绩的例子
w = pd.DataFrame({'math':[20,30,40],'eng':[15,20,63]},index = ['xiaom','bai','ana'] )
# print
math eng
xiaom 20 15
bai 30 20
ana 40 63
>访问列
w.math # 返回第一列
w['math'] # 返回第一列
>访问行
w.iloc[0] # iloc是按照0开始的索引访问 ,将一行转变成Series序列
w.loc['xiaom'] #访问第一行
>访问一个具体的元素.loc[]和.iloc[]
w.iloc[0].math # 返回20
w.loc['xiaom']['math'] # 返回20
>访问单个元素的最高效方法 .at和.iat
w.at['xiaom','math'] # 返回20
w.iat[0,0] #返回20
>对某一列按照值画折线图
w['math'].plot()
w.math.plot() #按照math这列画折线图
#数据不足,自动传播
w = pd.DataFrame({"A":range(4),"B":3}) # B列只有一个3
# print
A B
0 0 3
1 1 3
2 2 3
3 3 3
>pandas操作excel
>pd.read_csv(".csv",index_col = "指定列作为索引",usecols=[需要的列 0 ,2 ,3])
df = pd.read_excel(r'D:\xx\xx.xlsx',usecols=['姓名',..])
df[5:11] #表示第5到11行,右开
df.iloc[5] #索引为5的行
df.iloc[[1,3,5],:] # 第1,3,5行 的所有列
df.iloc[[1,3,5],[0,1,4]] # 第1,3,5行,第0,1,4列
df.at[3,'姓名'] #索引为3,列为姓名的值
>df.size 矩阵的大小500*3=1500
>df.shape (500,3)
>len(df) #500 数据帧行数
>df.index #返回索引构成的序列
3.lambda 函数
>lambda para:func 是一个保留字,用于定义匿名函数
x = lambda y:y+2 #其中y是参数,y+2是函数体,作用于对y+2
print(x(1)) # 3