numpy&pandas%matplotlib

numpy

import matplotlib.pyplot as plt
import numpy as np

1.创建矩阵
np.array([1,2,3,4])
np.array([[1,2,3,4],[1,2,2,2]])
np.array([[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]]])
np.array([[1,2,3],[4,5,6.1]])

np.ones(shape = (3,4))
np.zeros(shape = (3,4))
np.linspace(0,50,num = 20) 能取到50
np.arange(0,50,10) 取不到50
np.random.randint(0,100,size = (3,4))
np.random.random(size = (3,4))
arr = np.random.randint(0,100,size=(5,6))

2.读取图形数据
img_arr = plt.imread(’./23.jpg’)
plt.imshow(img_arr)

3.矩阵的属性
img_arr.shape
img_arr.size
img_arr.ndim
img_arr.dtype

arr.astype(‘int32’) 更改矩阵的数据类型

4.矩阵的取值
arr[1][1]

5.矩阵的切片
arr[0:2]
arr[:,0:3]
arr[0:2,0:2]

6.矩阵的转置
arr[::-1,::-1]

6.改变矩阵的形状
arr.reshape(30)
arr.reshape(-1,5)

7.矩阵的拼接
#如果横向级联保证行数一致,纵向级联保证列数一致
#注意:维度不一致的数组无法级联
axis轴向的理解
0:列
1:行

np.concatenate((arr,arr),axis=1)

8.常用的函数
数学
NumPy 提供了标准的三角函数:sin()、cos()、tan()
numpy.around(a,decimals) 函数返回指定数字的四舍五入值。
参数说明:
a: 数组
decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

统计学
numpy.amin() 和 numpy.amax(),用于计算数组中的元素沿指定轴的最小、最大值。
numpy.ptp():计算数组中元素最大值与最小值的差(最大值 - 最小值)。
numpy.median() 函数用于计算数组 a 中元素的中位数(中值)
标准差std():标准差是一组数据平均值分散程度的一种度量。
公式:std = sqrt(mean((x - x.mean())2))
如果数组是 [1,2,3,4],则其平均值为 2.5。 因此,差的平方是 [2.25,0.25,0.25,2.25],并且其平均值的平方根除以 4,即 sqrt(5/4) ,结果为 1.1180339887498949。
方差var():统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数,即 mean((x - x.mean())
2)。换句话说,标准差是方差的平方根。

np.dot(arr,arr_T)
arr_T = arr.T

pandas

Series是一种类似与一维数组的对象,由下面两个部分组成:
values:一组数据(ndarray类型)
index:相关的数据索引标签

1.Series 创建
由列表或numpy数组创建
由字典创建
Series(data=[1,2,3,4])

dic={
‘a’:100,
‘b’:20,
‘c’:50,
}

Series(data=dic)
Series(data=np.random.randint(0,100,size=(3,)),index=[‘a’,‘b’,‘c’])
s = Series(data=np.linspace(0,100,num=5),index=[‘a’,‘b’,‘c’,‘d’,‘e’])

2.索引取值
隐式索引:默认形式的索引(0,1,2…)
显示索引:自定义的索引,可以通过index参数设置显示索引
s[1]
s[‘c’]
s.c
s[0:3]
s[‘a’:‘d’]

3.常用属性
s.shape
s.size
s.index
s.values

4.常用方法

head(),tail()
unique()
nunique()
isnull(),notnull()
add() sub() mul() div()

s.head(2)
s.tail(2)
s.unique()
s.nunique()
s.isnull()
s.notnull()
s[s.notnull()]

Series的算术运算
法则:索引一致的元素进行算数运算否则补空
s1 = Series(data=[1,2,3,4,5],index=[‘a’,‘b’,‘c’,‘d’,‘e’])
s2 = Series(data=[1,2,3,4,5],index=[‘a’,‘b’,‘c’,‘f’,‘e’])
s = s1 + s2
s

DataFrame是一个【表格型】的数据结构。DataFrame由按一定顺序排列的多列数据组成。设计初衷是将Series的使用场景从一维拓展到多维。DataFrame既有行索引,也有列索引。
行索引:index
列索引:columns
值:values

1.DataFrame的创建
ndarray创建
字典创建
df = DataFrame(data=np.random.randint(1,100,size=(4,5)),index=[‘a’,‘b’,‘c’,‘d’],columns=[‘A’,‘B’,‘C’,‘D’,‘E’])
df

dic = {
‘name’:[‘jay’,‘tom’,‘bobo’],
‘salary’:[10000,20000,30000]
}
DataFrame(data=dic,index=[‘a’,‘b’,‘c’])

dic = [[100,0],[90,0],[100,0],[80,0]]
df = DataFrame(data=dic,columns=[‘张三’,‘李四’],index=[‘语文’,‘数学’,‘英语’,‘理综’])
df

2.DataFrame的属性
values、columns、index、shape

df.size
df.shape
df.index
df.values
df.columns

3.DataFrame索引操作
对行进行索引
队列进行索引
对元素进行索引

df[‘A’]
df.loc[‘a’]
df.loc[‘a’,‘A’]
df[[‘a’,‘b’]] #取多列
#取出多行
df.iloc[[1,2,3]]
#取出元素
df.loc[‘B’,‘e’]
#取多个元素
df.iloc[[1,2],3]

df[0:3]
df.iloc[:,0:3]
df.iloc[0:2,0:4]

索引:
df[col]:取列
df.loc[index]:取行
df.iloc[index,col]:取元素
切片:
df[index1:index3]:切行
df.iloc[:,col1:col3]:切列

DataFrame的运算
同Series

3.数据类型转换,设置索引
时间数据类型的转换
pd.to_datetime(col)
将某一列设置为行索引
df.set_index()

df[‘hire_date’] = pd.to_datetime(df[‘hire_date’])
df.info()
df.set_index(‘hire_date’)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值