数据挖掘笔记一【不定期更新】

来源:互联网 发布:淘宝米兰密码是正品吗 编辑:程序博客网 时间:2024/06/13 00:10

#matplotlib中的小技巧
1.无法显示中文和负号(乱码)
#coding:utf-8
import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
#有中文出现的情况,需要u'内容'

2.独立数字转到one-hot
import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utils# X shape (60,000 28x28), y shape (10,000, )(X_train,y_train),(X_test,y_test)=mnist.load_data()y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)
print(y_train[:3])"""[[ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.] [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.] [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]]#变成了one hot 形式(即哪个位置正确哪个位置就是1)

3,将二维数组用图表现出来
  1. import matplotlib as mpl  
  2.   
  3. import matplotlib.pyplot as plt  
  4.   
  5. import numpy as np  
  6.  
  7. data=np.clip(np.random.randn(5,5),-1,1#生成随机数据,5行5列,最大值1,最小值-1  
  8.   
  9. fig = plt.figure()  
  10. # 第一个子图,按照默认配置  
  11. ax = fig.add_subplot(111)  
  12. ax.imshow(data)  

4.图像数据白化
像数据预处理:在mnist的例子中,所有图像都是使用的原始像素值(从0到255)。在机器学习中,对于输入的特征做归一化(normalization)处理是常见的套路。而在图像分类的例子中,图像上的每个像素可以看做一个特征。在实践中,对每个特征减去平均值来中心化数据是非常重要的。在这些图片的例子中,该步骤意味着根据训练集中所有的图像计算出一个平均图像值,然后每个图像都减去这个平均值,这样图像的像素值就大约分布在[-127, 127]之间了。下一个常见步骤是,让所有数值分布的区间变为[-1, 1]。零均值的中心化是很重要的,等我们理解了梯度下降后再来详细解释。
0 0