码迷,mamicode.com
首页 > 其他好文 > 详细

数据分析

时间:2019-09-20 16:28:53      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:运算   shuf   shuff   bsp   size   列表   ini   ice   maximum   

numpy

import numpy as np

向量运算
shop_price = [30, 20, 15, 40]
shop_num = [2, 3, 1, 4]
np_shop_price = np.array(shop_price)
np_shop_num = np.array(shop_num)
np.sum(np_shop_price * np_shop_num)

四则运算
b1 = [1,2,3,4,]
b1+2 # 报错
b1*3 # [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

b2 = np.array([1,2,3,4])
b2+3 # array([4, 5, 6, 7])
b2*3 # array([ 3, 6, 9, 12])

常用属性
T 数组的转置(对高维数组(行列式)而言)
dtype 数组元素的数据类型
size 数组元素的总个数
ndim 数组的维数(行数)
shape 数组的维度大小(以元组形式)(行数,列数)

ndarray-多维数组对象
dtype:
bool_, int(8,16,32,64), float(16,32,64)
类型转换:astype()
创建nd.array:
array() 将列表转换为数组,可选择显式指定dtype
linspace() 类似arange(),第三个参数为数组长度
zeros() 根据指定形状和dtype创建全0数组
ones() 根据指定形状和dtype创建全1数组
reshape()


等分
前后包括 [start,stop] num:默认等分数
np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None,axis=0,)
前包括后不包括 [start,stop)
np.linspace(1,8,num=3,endpoint=False,retstep=False,dtype=None,axis=0,)

数据个数等分矩阵
a6 = np.arange(10)
a6.reshape(2,5) # (行数,列数)

索引
b1 = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b1[3] # 3

b2 = np.array([[1,2,3,4,5],[10,20,30,40,50]])
b2[0,3] # 4 [行数,列数]
b2[1,2] # 30 [行数,列数]

切片
[start:stop:step]
# 用法与python中 基础的列表切片一样

布尔索引
b1 = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b1>4 # array([False, False, False, False, False, True, True, True, True, True])
b1[b1>4] # array([5, 6, 7, 8, 9])

花式索引
b1 = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b1[[2,3,5]] # array([2, 3, 5])

通用函数
一元函数:
abs: 绝对值
sqrt: 开根号(Square root)
exp: 指数函数(Exponentia),以e为底
log: 对数函数(Logarithmic)
ceil: 向下取整
floor: 向上取整
rint: 四舍五入
trunc: 舍去小数位
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
>>> np.trunc(a)
array([-1., -1., -0., 0., 1., 1., 2.])
modf: 把小数与整数分开
>>> np.modf([2.8,3.6])
(array([0.8, 0.6]), array([2., 3.])
isnan: 是否是数字 (nan:not a num)
isinf: 是否是无穷(inf:infinity)
cos, sin, tan:三角函数
二元函数:
add,
substract,
multiply,
divide,
power,
mod,
maximum,
mininum,

数学和统计方法
常用函数
sum 求和
mean 求平均数
std 求标准差
var 求方差
min 求最小值
max 求最大值
argmin 求最小值索引位置
argmax 求最大值索引位置
Sort 求和

随机数生成
np.random常用函数
rand 产生0到1之间的随机数组(行列式)(0到1之间的数)后可加(行数,列数)
np.random.rand(5)
array([0.431528 , 0.78256694, 0.57882107, 0.62501602, 0.5008957 ])
np.random.rand(2,5)
array([[0.53423693, 0.64314084, 0.09177743, 0.9410606 , 0.33872707],
[0.72611849, 0.15508284, 0.00941139, 0.19386368, 0.50045678]])
randint 产生给定范围内的一个随机整数或数组(行列式)
np.random.randint(5) 产生一个整数
np.random.randint(5, size=(4)) 产生一个有四个整数的一维数组
np.random.randint(5, size=(2,4)) 产生一个有四个整数的三维数组
choice 给定形状产生随机选择
shuffle 与random.shuffle相同
uniform 给定形状产生随机数组

 

数据分析

标签:运算   shuf   shuff   bsp   size   列表   ini   ice   maximum   

原文地址:https://www.cnblogs.com/Chinesehan/p/11557811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!