首先说一下直方图和柱形图的区别,看起来差不多,都是柱图,但区别就在这些住上面,
直方图表达的数据分布,比如用户年龄分布,主要是想看一些那些年龄段的人数,比较多就可以,采用直方图,再者 年龄连续性太强画出图,都连在一起了
而柱形图就是反映的大小,当前住的数量有多少,
直方图主要反映分布,如年龄 柱形图主要用来比较大小 一般用于分段比较明显的地方 如部门A和部门B 衣物等
直方图 :
# 原始数据不用自己去统计个数,直接喂给直方图原始数据就行,不用统计个数 # 数据 分段数 颜色 plt.hist(ages, bins=20, color='red') plt.title("") plt.xlabel("e") plt.ylabel("t") plt.savefig("e.png") plt.show()
python版的wordcount
map(lambda fields: (fields[3], 1)).reduceByKey(lambda x, y: x + y).collect()
也可以用countByValue()替代 上面直接额变为
map(lambda fields:fields[3]).countByValue() 不用在(fields[3],1)了,更加简单方便,直接是transform,会直接输出,只要喂给,原始的数据就行,但前者返回的是rdd可以操作,后者返回的是 defaultdict(<class 'int'>,关于defaultdict的一些操作取出key值 dd.keys() 或者list(dd), x=np.array([c for c in list(dd)]) 取出value dd.values() y= [dd[m] for m in x]
matplotlib 柱状图按照从左往右以此递增的y值排列 显示
柱状图对应的数据大多为dict类型 第一种方式,按照value值进行排序
>>> dic = {7:31, 3:5, 2:3, 89:4, 2:74, 44:0}
>>> sorted(dic.items(),key=lambda d:d[0])
[(2, 74), (3, 5), (7, 31), (44, 0), (89, 4)]
>>> sorted(dic.items(),key=lambda d:d[1])
[(44, 0), (89, 4), (3, 5), (7, 31), (2, 74)]
解释一下 dic.items()获取kv对 key通过lambda对那个字段排序 reverse=True降序
>>> sorted(dic.items(),key=lambda d:d[0],reverse=True)
[(89, 4), (44, 0), (7, 31), (3, 5), (2, 74)]
第二种方法比较麻烦点
如果mp类型为defaultdict 主要是wordcount采用countByValue()
直接转为对dict按照k或v排序
然后x_axis= [x[0] for x in sorted(mp.items(),key=lambda dd:dd[1])] 取出key
y_axis[x[1] for x in sorted(mp.items(),lambda dd:dd[1])] 取出values
上面已经排好序了
如果 mp 类型为dtype 主要wordcount采用的map(lambda fields:(fields[3],1)).reduceByKey(lambda x,yx+y).collect()
x_axis1 = np.array([c[0] for c in count_by_occupation])
y_axis1 = np.array([c[1] for c in count_by_occupation])
需要排序按照y值从小到大排列
则
x_axis1 = x_axis1[np.argsort(y_axis1)] y_axis1 = y_axis1[np.argsort(y_axis1)]
argsort将数值从小到大排序然后返回对应数据原来的下标
同理按照x值从小到大排序 需要提前转换成数组 np.array() 好利用下标获取值
x_axis1 = x_axis1[np.argsort(x_axis1)]
y_axis1 = y_axis1[np.argsort(x_axis1)]
举个例子
>>> a=[('doctor',34),('programmer',45),('writer',20),('singer',15)]
>>> m=np.array([c[0] for c in a])
>>> m
array(['doctor', 'programmer', 'writer', 'singer'], dtype='<U10')
>>> n=np.array([c[1] for c in a])
>>> n
array([34, 45, 20, 15])
>>> np.argsort(n)
array([3, 2, 0, 1])
>>> m[np.argsort(n)]
array(['singer', 'writer', 'doctor', 'programmer'], dtype='<U10')
>>> n[np.argsort(n)]
array([15, 20, 34, 45])
>>>
y值从小到大,且kv一一对应
python字典 dict对应与其他的map {0:"sdf",2:"sdf"}
v=dic.get(k)
时刻想着注意类型转换 str(x) int(x)
常用列表推到式 会有意想不到的结果 [c[1] for c in collec]
条形图 :
import matplotlib.pyplot as plt
x_length=np.arange(len(x_axis)) np.arange 从0 到range-1 生成列表 x 轴的长度
#x轴的长度范围一般用np.arange(len(x_axis)) ,y值,颜色柱状图可以用不同颜色,但直方图不行
pb=plt.bar(x_length,y,color='rgb')
#自定义x轴标签
plt.xticks(x_length,x_want_label,size='small',fontsize=6,rotation=30)
# 如果想要条形图上面显示数字
#根据条形图的返回值 可以获得坐标然后通过plt.text显示,然后for循环每个都添加上,
text参数x坐标如果想要在住的中间显示需要x+width/2 y坐标即高度,展示的值(一般为高度)
for position in pb: h = position.get_height() plt.text(position.get_x() + position.get_width() / 2, h, h, ha='center', va='bottom')
plt.title("uss") plt.xlabel("n") plt.ylabel("C") plt.savefig("./png") plt.show()
filter过滤时,注意找特征 ,比如长度 startwith endwith 等 过滤 过滤符合条件的留下来
但也可以反响过滤 filter{! xxx}
list的后进式 [-1:] 倒数第一个
['(1995)']去掉括号提取括号里面的值,试过正则但失败了,只能一步一步的转换了
只有字符串str才有replace ,主要采用先转换成str 字符串在replace 多次 过滤异常值 转换类型
一般的方法为看数据为什么类型的或者转成什么类型对数据处理比较方便一般就是str replace int count
一步一步向目标逼近 逼近!!!
.map(lambda y: str(y)).map( lambda z: z.replace("['(", "")).map(lambda v: v.replace(")']", "")).filter(lambda i: len(i) == 4).map( lambda j: int(j)).map(lambda a: 2019 - a).collect()
python flatmap(lambda x:x )只需要将他们压平到一个list里面即可,返回值为 defaultdict
饼图
labels = ['Me', 'Fale'] sizes = [数量1, 数量2] #直接填数量即可会自动算百分比 plt.pie(sizes, labels=labels, autopct='%3.1f %%') #label 标签 autopct显示的百分比文字 plt.title("Me") plt.savefig("./.png") plt.show()
时间戳转时间 #
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
只取的小时字段 def time_convert(x): time_local = time.localtime(x) return int(time.strftime("%H", time_local))
一般就是list[] dict{"dsf":"df","df",2} tuple(2,3) 列表 字典dict不叫map 元组数据进行封装
pyspark中使用原始的mapreduce .map(lambda x:(x,1)).reduceByKey(lambda x,y:x+y).collect
与使用countByValue()的区别
前者 传统mapreduce 返回的是一个大的list里面包含几对kv对
.map(lambda lines:lines.split("::")).map(lambda fields:(fields[3],1)).reduceByKey(lambda x,y:x+y).collect()
[('10', 195), ('16', 241), ('20', 281), ('9', 92), ('1', 528), ('12', 388), ('17', 502), ('0', 711), ('14', 302), ('4', 759), ('8', 17), ('19', 72), ('15', 144), ('7', 679), ('3', 173), ('11', 129), ('2', 267), ('18', 70), ('5', 112), ('13', 142), ('6', 236)]
可以便利循环,假设上面赋值与a
>>> [x[0] for x in a]
['10', '16', '20', '9', '1', '12', '17', '0', '14', '4', '8', '19', '15', '7', '3', '11', '2', '18', '5', '13', '6']
>>> [x[1] for x in a]
[195, 241, 281, 92, 528, 388, 502, 711, 302, 759, 17, 72, 144, 679, 173, 129, 267, 70, 112, 142, 236]
然后采用
m=np.array([x[0] for x in a]) #转成数组很重要 ,利用下标取值
m=m[np.argsort(m)]
进行排序
后者高级点的countByValue() 不需要什么只需要将待聚合的数据,传入给他就行
>>> sc.textFile("/ha/users.txt").map(lambda lines:lines.split("::")).map(lambda fields:fields[3]).take(100)
['10', '16', '15', '7', '20', '9', '1', '12', '17', '1', '1', '12', '1', '0', '7', '0', '1', '3', '10', '14', '16', '15', '0', '7', '4', '7', '11', '1', '7', '7', '7', '0', '3', '0', '1', '3', '9', '4', '4', '0', '4', '8', '12', '17', '16', '19', '4', '4', '12', '2', '10', '4', '0', '1', '12', '20', '19', '2', '1', '1', '17', '3', '4', '1', '12', '18', '5', '4', '1', '4', '14', '0', '4', '14', '10', '7', '4', '1', '0', '1', '0', '17', '2', '4', '4', '10', '14', '1', '9', '13', '7', '4', '17', '17', '0', '16', '3', '7', '10', '17']
>>> sc.textFile("/hMoviesData/users.txt").map(lambda lines:lines.split("::")).map(lambda fields:fields[3]).countByValue()
defaultdict(<class 'int'>, {'10': 195, '16': 241, '15': 144, '7': 679, '20': 281, '9': 92, '1': 528, '12': 388, '17': 502, '0': 711, '3': 173, '14': 302, '4': 759, '11': 129, '8': 17, '19': 72, '2': 267, '18': 70, '5': 112, '13': 142, '6': 236})
可以看出返回值类型为defaultdict也是dict ,交互式shell真是方便 暂且将上式赋值给b
>>> sorted(b.items(),key=lambda dd:dd[0])
[('0', 711), ('1', 528), ('10', 195), ('11', 129), ('12', 388), ('13', 142), ('14', 302), ('15', 144), ('16', 241), ('17', 502), ('18', 70), ('19', 72), ('2', 267), ('20', 281), ('3', 173), ('4', 759), ('5', 112), ('6', 236), ('7', 679), ('8', 17), ('9', 92)]
>>> 对其K进行排序 发现变成了list内包含着 dict 同理 对V排序也是变成list套dict
>>> sorted(b.items(),key=lambda dd:dd[1],reverse=True)
[('4', 759), ('0', 711), ('7', 679), ('1', 528), ('17', 502), ('12', 388), ('14', 302), ('20', 281), ('2', 267), ('16', 241), ('6', 236), ('10', 195), ('3', 173), ('15', 144), ('13', 142), ('11', 129), ('5', 112), ('9', 92), ('19', 72), ('18', 70), ('8', 17)]
>>> 已经排好序了可以直接额用来绘制图形了
>>> label=sorted(b.items(),key=lambda dd:dd[0])
>>> label
[('0', 711), ('1', 528), ('10', 195), ('11', 129), ('12', 388), ('13', 142), ('14', 302), ('15', 144), ('16', 241), ('17', 502), ('18', 70), ('19', 72), ('2', 267), ('20', 281), ('3', 173), ('4', 759), ('5', 112), ('6', 236), ('7', 679), ('8', 17), ('9', 92)]
>>> [c[0] for c in label]
['0', '1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '2', '20', '3', '4', '5', '6', '7', '8', '9']
>>> [c[1] for c in label]
[711, 528, 195, 129, 388, 142, 302, 144, 241, 502, 70, 72, 267, 281, 173, 759, 112, 236, 679, 17, 92]
>>>
所以推荐使用后者countByValue,比较方便,
由上可知 list 和dict可以互换
>>> a
[('10', 195), ('16', 241), ('20', 281), ('9', 92), ('1', 528), ('12', 388), ('17', 502), ('0', 711), ('14', 302), ('4', 759), ('8', 17), ('19', 72), ('15', 144), ('7', 679), ('3', 173), ('11', 129), ('2', 267), ('18', 70), ('5', 112), ('13', 142), ('6', 236)]
>>> b
defaultdict(<class 'int'>, {'10': 195, '16': 241, '15': 144, '7': 679, '20': 281, '9': 92, '1': 528, '12': 388, '17': 502, '0': 711, '3': 173, '14': 302, '4': 759, '11': 129, '8': 17, '19': 72, '2': 267, '18': 70, '5': 112, '13': 142, '6': 236})
>>>
a mapreduce list包dict
b countByValue defaultdict dict包dict