当前位置: 移动技术网 > IT编程>脚本编程>Python > python大规模机器学习day5-数据流的特征管理

python大规模机器学习day5-数据流的特征管理

2020年08月10日  | 移动技术网IT编程  | 我要评论

数据流的特征管理

实验要求:
1.掌握缩放策略
2.得到训练集上的四个特征量:平均值,最大值,最小值,标准偏差
实验内容:
1.通过绘制值后,检查估算值相对于最终结果的波动程度
2.通过与打乱版本的shuffled_har.csv文件的图表对比,了解随机化观测顺序的重要性
代码注释:

import os, csv //csv库用于读取和写入表格数据
local_path = os.getcwd() //用getcwd来获取当前的保存路径
source = ‘bikesharing\hour.csv’ //source来指定目标文件路径
SEP=’,’
running_mean = list() //创建两个列表
running_std = list()
with open(local_path+’\’+source,‘rt’) as R:
iterator =csv.DictReader(R, delimiter=SEP) //创建一个迭代器,每次读取文件中的一行,添加索引,并以’,’为结束
x=0.0
x_squared=0.0
for n,row in enumerate(iterator):
temp = float(row[‘temp’]) //每一行的temp列,此时temp表示温度
if n==0:
max_x, min_x=temp, temp //声明三个变量,分别是最高温度,最低温度,当前温度
else:
max_x, min_x=max(temp, max_x),min(temp, min_x)
x += temp //x的变量表示临时变量,保存一日的temp之和,方便计算均值
x_squared += temp2 //临时变量,为了方便计算标准差
running_mean.append(x / (n+1)) //在running_mean中添加当日气温平均值
running_std.append(((x_squared - (x
2)/(n+1))/(n+1))**0.5) //在running_std中添加当日气温的标准差
#DATA PROCESSING placeholder //以上是数据处理部分,得到了两个列表信息
#MACHINE LEARNING placeholder //以下是机器学习部分
pass
print(‘Total rows: %i’ % (n+1))
print (‘Feature ‘temp’: mean=%0.3f,max=%0.3f,min=%0.3f,\sd=%0.3f’ %(running_mean[-1],max_x,min_x, running_std[-1])) //特征值取四个:平均温度,最高温度,最低温度,标准差

代码2:

import matplotlib.pyplot as plt //导入matlplotlib库
plt.figure() //首先要创建一个画布
plt.plot(running_mean,‘r-’, label=‘mean’) //画平均值曲线
plt.plot(running_std,‘b-’, label=‘standard deviation’)//画标准差曲线
plt.ylim(0.0,0.6) //画纵坐标的范围
plt.xlabel(‘Number of training examples’) //画横轴名
plt.ylabel(‘Value’) //画纵轴名
plt.legend(loc=‘lower right’, numpoints= 1) //legend将两个曲线表示在一个图上,后面两个是控制位置的参数
plt.show()
运行截图:
在这里插入图片描述在这里插入图片描述在这里插入图片描述

源代码:
import os, csv
local_path = os.getcwd()
source = ‘bikesharing\hour.csv’
SEP=’,’
running_mean = list()
running_std = list()
with open(local_path+’\’+source,‘rt’) as R:
iterator =csv.DictReader(R, delimiter=SEP)
x=0.0
x_squared=0.0
for n,row in enumerate(iterator):
temp = float(row[‘temp’])
if n==0:
max_x, min_x=temp, temp
else:
max_x, min_x=max(temp, max_x),min(temp, min_x)
x += temp
x_squared += temp2
running_mean.append(x / (n+1))
running_std.append(((x_squared - (x
2)/(n+1))/(n+1))**0.5)
#DATA PROCESSING placeholder
#MACHINE LEARNING placeholder
pass
print(‘Total rows: %i’ % (n+1))
print (‘Feature ‘temp’: mean=%0.3f,max=%0.3f,min=%0.3f,\sd=%0.3f’ %(running_mean[-1],max_x,min_x, running_std[-1]))

代码2:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(running_mean,‘r-’, label=‘mean’)
plt.plot(running_std,‘b-’, label=‘standard deviation’)
plt.ylim(0.0,0.6)
plt.xlabel(‘Number of training examples’)
plt.ylabel(‘Value’)
plt.legend(loc=‘lower right’, numpoints= 1)
plt.show()

实验总结:
由于特征值之间差异很大,需要标准化,也就是缩放在[0,1]的范围之内,这样便于统计。
随机化的样本能够更快速地帮助我们观测到数据趋势。

本文地址:https://blog.csdn.net/qq_43920838/article/details/107886226

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网