1.Numpy & Pandas
Numpy
NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:
- 一个强大的N维数组对象 ndarray
- 广播功能函数
- 整合 C/C++/Fortran 代码的工具
- 线性代数、傅里叶变换、随机数生成等功能
import numpy as np
ndarray
生成不同的array
arr = np.array() # data = [1, 2, 3, 4]
# arr = np.array(data)
np.zeros() # all zeros
np.ones() # all ones
np.empty() # uninitialized
np.arrange() # in a sequence in order
Conversion:
arr.astype(int)
关于复制ndarray:
等号 = 是直接取指针,任何对新array的操作会修改原始array。
真正的复制取值是 arr[ ].copy()
Indexing
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', ' Joe’])
names == 'Bob’
>>> array([ True, False, False, True, False, False, False], dtype =bool)
# 判断每一个index的值是否等于‘Bob’,返回T/F
data = np.random.randn(7, 4)
data
>>> array([[ 0.0929, 0.2817, 0.769 , 1.2464], [ 1.0072, -1.2962, 0.275 , 0.2289], [ 1.3529, 0.8864, -2.0016, -0.3718], [ 1.669 , -0.4386, -0.5397, 0.477 ], [ 3.2489, -1.0212, -0.5771, 0.1241], [ 0.3026, 0.5238, 0.0009, 1.3438], [-0.7135,
-0.8312, -2.3702, -1.8608]])
data[names == 'Bob']
>>> array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.669 , -0.4386, -0.5397, 0.477 ]])
np.random
https://blog.csdn.net/u012149181/article/details/78913167
d0, d1, d2, …代表维度,(4,2)四行两列
- **numpy.random.randn(d0, d1, …, dn):**根据给定维度,返回
标准正态分布
的样本值。 - **numpy.random.rand(d0, d1, …, dn):**根据给定维度,返回
随机分布
的样本值。
np.random.rand(4,3,2) # shape: 4*3*2
array([[[ 0.08256277, 0.11408276],
[ 0.11182496, 0.51452019],
[ 0.09731856, 0.18279204]],
[[ 0.74637005, 0.76065562],
[ 0.32060311, 0.69410458],
[ 0.28890543, 0.68532579]],
[[ 0.72110169, 0.52517524],
[ 0.32876607, 0.66632414],
[ 0.45762399, 0.49176764]],
[[ 0.73886671, 0.81877121],
[ 0.03984658, 0.99454548],
[ 0.18205926, 0.99637823]]])
- 生成随机整数
numpy.random.randint(low, high=None, size=None, dtype=’l’):
- 返回
随机整数
,范围区间为[low,high),包含low,不包含high - 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
- high没有填写时,默认生成随机数的范围是[0,low)
np.random.randint(1,size=5) # 返回[0,1)之间的整数,所以只有0
>>> array([0, 0, 0, 0, 0])
np.random.randint(1,5) # 返回1个[1,5)时间的随机整数
>>> 4
np.random.randint(-5,5,size=(2,2))
>>> array([[ 2, -1],
[ 2, 0]])
- 生成[0,1)之间的浮点数
print(np.random.random_sample(size=(2,2)))
[[ 0.34966859 0.85655008]
[ 0.16045328 0.87908218]]
print(np.random.random(size=(2,2)))
[[ 0.25303772 0.45417512]
[ 0.76053763 0.12454433]]
print(np.random.ranf(size=(2,2)))
[[ 0.0379055 0.51288667]
[ 0.71819639 0.97292903]]
print(np.random.sample(size=(2,2)))
[[ 0.59942807 0.80211491]
[ 0.36233939 0.12607092]]
- 从给定数据种生成随机数
numpy.random.choice(a, size=None, replace=True, p=None)
- 参数: a为一维数组类似数据或整数;size为数组维度;p为数组中的数据出现的概率;当replace为False时,生成的随机数不能有重复的数值
- a为整数时,对应的一维数组为np.arange(a)
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])
>>>
array([['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'xiaomi', 'iphone']],
dtype='<U7')
- 随机数种子
numpy.random.seed()
当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
np.random.seed(0)
np.random.rand(5)
array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ]) # seed相同,随机数相同
Pandas
https://blog.csdn.net/pipisorry/article/details/18010307
Series
Series是一个一维的类似的数组对象,包含一个数组的数据
(任何NumPy的数据类型)和一个与数组关联的数据标签
,被叫做索引 。Seriers的输出字符串表示形式是索引在左边,值在右边。
Data Frame
Data Frame attributes:
Data Frame methods: