-#!/usr/bin/env python

-# # # -- coding: utf-8 --

-# # # @Time : 2019.5.22 14:09

-# # # @Author : AndrewCHH

-# # # @File : after_class.py

1. 获取数据

  • 使用MNIST数据集练习分类任务
from __future__ import print_function
import pandas as pd # 导入后加入以下列,再显示时显示完全。
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
from sklearn.datasets import fetch_mldata
from scipy.io import loadmat mnist = fetch_mldata('MNIST original', transpose_data=True, data_home='files')
print("mnist",mnist)
# *DESCR为description,即数据集的描述
# *CLO_NAMES为列名
# *target键,带有标记的数组
# *data键,每个实例为一行,每个特征为1列
# 共七万张图片,每张图片784个特征点
X, y = mnist["data"], mnist["target"]
print(X.shape, y.shape)
print(type(X))
# 显示图片
import matplotlib
import matplotlib.pyplot as plt some_digit = X[36001]
some_digit_image = some_digit.reshape(28, 28) # 将一维数组转化为28*28的数组
# cmap->颜色图谱(colormap)
# interpolation: 图像插值参数,图像插值就是利用已知邻近像素点的灰度值(或rgb图像中的三色值)来产生未知像素点的灰度值,以便由原始图像再生出具有更高分辨率的图像。
# * If interpolation is None, it defaults to the image.interpolation rc parameter.
# If the interpolation is 'none', then no interpolation is performed for the Agg, ps and pdf backends. Other backends will default to 'nearest'.
# For the Agg, ps and pdf backends, interpolation = 'none' works well when a big image is scaled down,
# while interpolation = 'nearest' works well when a small image is scaled up.
plt.imshow(some_digit_image, cmap=matplotlib.cm.binary,
interpolation="nearest")
plt.axis("off")
plt.show()
print(y[36001])
D:\Anaconda3\lib\site-packages\sklearn\utils\deprecation.py:77: DeprecationWarning: Function fetch_mldata is deprecated; fetch_mldata was deprecated in version 0.20 and will be removed in version 0.22
warnings.warn(msg, category=DeprecationWarning)
D:\Anaconda3\lib\site-packages\sklearn\utils\deprecation.py:77: DeprecationWarning: Function mldata_filename is deprecated; mldata_filename was deprecated in version 0.20 and will be removed in version 0.22
warnings.warn(msg, category=DeprecationWarning) mnist {'DESCR': 'mldata.org dataset: mnist-original', 'COL_NAMES': ['label', 'data'], 'target': array([0., 0., 0., ..., 9., 9., 9.]), 'data': array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)}
(70000, 784) (70000,)
<class 'numpy.ndarray'> <Figure size 640x480 with 1 Axes> 5.0

2. 创建测试集训练集

x_train, x_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]

2.1 数据洗牌(注意数据的顺序敏感性)

import numpy as np

# Randomly permute a sequence, or return a permuted range.
shuffle_index = np.random.permutation(60000)
x_train, y_train = x_train[shuffle_index], y_train[shuffle_index] # # Q1:
# ## 1.1 创建KNN分类器
y_train_5 =(y_train==5)
from sklearn.neighbors import KNeighborsClassifier
# knn_clf = KNeighborsClassifier()
# knn_clf = KNeighborsClassifier(n_jobs=-1, weights='distance', n_neighbors=4)
# knn_clf.fit(x_train, y_train)
# array = knn_clf.predict(x_test)
# print("knnResult",array) # # ## 1.2 对knn执行网格搜索
# from sklearn.model_selection import GridSearchCV
# para_grid = [
# {'n_neighbors':[3,4,5,6],'weights':["uniform","distance",]}
# ]
# knn_clf = KNeighborsClassifier()
# grid_search = GridSearchCV(knn_clf,para_grid,cv=5,verbose=3,n_jobs=-1,scoring="neg_mean_squared_error")
# grid_search.fit(x_train,y_train)
# print(grid_search.best_params_)
# print(grid_search.best_estimator_) # from sklearn.model_selection import GridSearchCV
#
# param_grid = [{'weights': ["uniform", "distance"], 'n_neighbors': [3, 4, 5]}]
#
# knn_clf = KNeighborsClassifier()
# grid_search = GridSearchCV(knn_clf, param_grid, cv=5, verbose=3, n_jobs=-1)
# grid_search.fit(x_train, y_train)
# print(grid_search.best_params_)
# print(grid_search.best_estimator_) # ## 1.2 评估准确性
# y_knn_pred = knn_clf.predict(x_test)
# from sklearn.metrics import accuracy_score
# a = accuracy_score(y_test,y_knn_pred)
# print(a) # # Q2:
# 使用shaift方法移动图片中的像素,注意,self传进来的X[1]是一维数组,要使用reshape变成28*28的数组。
# cval参数指的是移动图片后填补的像素值
from scipy.ndimage.interpolation import shift
def movePiexOfImage(self,dx,dy,new=0):
return shift(self.reshape(28,28),[dx,dy],cval=new) # 图片显示维度错误检测
def valid_imshow_data(data):
data = np.asarray(data)
if data.ndim == 2:
return True
elif data.ndim == 3:
if 3 <= data.shape[2] <= 4:
return True
else:
print('The "data" has 3 dimensions but the last dimension '
'must have a length of 3 (RGB) or 4 (RGBA), not "{}".'
''.format(data.shape[2]))
return False
else:
print('To visualize an image the data must be 2 dimensional or '
'3 dimensional, not "{}".'
''.format(data.ndim))
return False print("image:",X[1])
imageShift=movePiexOfImage(X[1],5,1,new=100)
imageShift = imageShift.reshape(28,28)
plt.imshow(imageShift,cmap=matplotlib.cm.binary)
plt.show() print(len(x_train))
for i in range(len(x_train)):
moveLeft = movePiexOfImage(x_train[i],1,0,new=100)
# moveDown = movePiexOfImage(x_train[i],0,1,new=100)
# moveRight = movePiexOfImage(x_train[i],-1,0,new=100)
# moveUp = movePiexOfImage(x_train[i],0,-1,new=100)
# moveDown = moveDown.reshape(1,784)
moveLeft = moveLeft.reshape(1,784)
# moveRight = moveRight.reshape(1,784)
# moveUp = moveUp.reshape(1,784)
x_train = np.concatenate((x_train,moveLeft),axis=0)
print(len(x_train))
image: [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 64 253 255 63 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 96 205 251 253 205 111
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 96 189 251 251 253 251 251 31 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 16 64 223 244 251 251 211 213
251 251 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 80 181 251 253 251 251 251 94 96 251 251 31 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 92 253 253 253 255 253 253 253
95 96 253 253 31 0 0 0 0 0 0 0 0 0 0 0 0 0
0 92 236 251 243 220 233 251 251 243 82 96 251 251 31 0 0 0
0 0 0 0 0 0 0 0 0 0 80 253 251 251 188 0 96 251
251 109 0 96 251 251 31 0 0 0 0 0 0 0 0 0 0 0
0 96 240 253 243 188 42 0 96 204 109 4 0 12 197 251 31 0
0 0 0 0 0 0 0 0 0 0 0 221 251 253 121 0 0 0
36 23 0 0 0 0 190 251 31 0 0 0 0 0 0 0 0 0
0 0 48 234 253 0 0 0 0 0 0 0 0 0 0 0 191 253
31 0 0 0 0 0 0 0 0 0 0 44 221 251 251 0 0 0
0 0 0 0 0 0 0 12 197 251 31 0 0 0 0 0 0 0
0 0 0 190 251 251 251 0 0 0 0 0 0 0 0 0 0 96
251 251 31 0 0 0 0 0 0 0 0 0 0 190 251 251 113 0
0 0 0 0 0 0 0 0 40 234 251 219 23 0 0 0 0 0
0 0 0 0 0 190 251 251 94 0 0 0 0 0 0 0 0 40
217 253 231 47 0 0 0 0 0 0 0 0 0 0 0 191 253 253
253 0 0 0 0 0 0 12 174 253 253 219 39 0 0 0 0 0
0 0 0 0 0 0 0 67 236 251 251 191 190 111 72 190 191 197
251 243 121 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0
63 236 251 253 251 251 251 251 253 251 188 94 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 27 129 253 251 251 251 251
229 168 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 95 212 251 211 94 59 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]

60000

Hand on Machine Learning第三章课后作业(2):其余小练习的更多相关文章

  1. Hand on Machine Learning第三章课后作业(1):垃圾邮件分类

    import os import email import email.policy 1. 读取邮件数据 SPAM_PATH = os.path.join( "E:\\3.Study\\机器 ...

  2. C++第三章课后作业答案及解析---指针的使用

    今天继续完成上周没有完成的习题---C++第三章课后作业,本章题涉及指针的使用,有指向对象的指针做函数参数,对象的引用以及友元类的使用方法等 它们具体的使用方法在下面的题目中会有具体的解析(解析标注在 ...

  3. Hand on Machine Learning 第三章:分类器

    1. 获取数据 使用MNIST数据集练习分类任务 from sklearn.datasets import fetch_mldata from scipy.io import loadmat mnis ...

  4. CSAPP深入理解计算机系统(第二版)第三章家庭作业答案

    <深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...

  5. 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总

    大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...

  6. JAVA第三周课后作业

    JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...

  7. 中级Perl 第三章课后习题

    3. 10. 1. 练习1 [25 分钟] 读当前目录的文件列表并转换成全路径.不能用shell 命令或外部程序读当前目 录.Perl 的File::Spec 和Cwd 两个模块对这个程序有帮助.每个 ...

  8. Python核心编程2第三章课后练习

    1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...

  9. Machine Learning 第三周

    ML week3 逻辑回归 Logistic Function h_\theta(x)=g(\theta^Tx) g(t)=\frac{1}{1+e^{-z}} 当t大于0, 即下面公式成立时,y=1 ...

随机推荐

  1. 如何给自己的Python项目制作安装包

    Packaging Python Projects¶ 本教程将指导您如何打包一个简单的Python项目.它将向您展示如何添加必要的文件和结构来创建包,如何构建包以及如何将其上载到Python包索引. ...

  2. 如何在Linux下安装JDK1.8

    本文会详细介绍如何在Linux下安装JDK1.8 首先要设置虚拟机的IP地址,不知道如何设置的话可以 翻看我的前一篇博客   http://www.cnblogs.com/xiaoxiaoSMILE/ ...

  3. api权限管理系统与前后端分离实践

    https://segmentfault.com/a/1190000014368885

  4. IOS上编译boost库

    环境:xcode9mac os 10.13 1.下载boost库并解压2.cd到解压后的文件包内,执行 sh bootstrap.sh(解压后会得到这个脚本文件).3.执行./b2 编译整个库.4.经 ...

  5. Python的题目

    1.将元组 (1,2,3) 和集合 {4,5,6} 合并成一个列表.2.在列表 [1,2,3,4,5,6] 首尾分别添加整型元素 7 和 0.3.反转列表 [0,1,2,3,4,5,6,7] .4.反 ...

  6. 【JSOI2014】歌剧表演

    题目 分析 我们抽象的认为一些不能互相辨认的人,被分到了一个集合,每当又有一场演出,就将每个出演的演员扔出集合,再将上次在相同集合的分在同一集合. 然后修改被分的集合和被新创建的时间,当集合只有一个数 ...

  7. Vue的watch和computed方法的使用

    Vue的watch属性 Vue的watch属性可以用来监听data属性中数据的变化 <!DOCTYPE html> <html> <head> <meta c ...

  8. vscode匹配括号插件

    给大家推荐一个vscode匹配括号的插件: Bracket Pair Colorizer.超级好用哦

  9. git windows下换行符问题

    不同操系统下的换行符 CR回车 LF换行 Windows/Dos CRLF \r\n Linux/Unix LF \n MacOS CR \r 1.执行git config --get core.au ...

  10. linux下为已经编译好的php环境添加mysql扩展(php安装完成后如何添加mysql扩展)

    问题背景 平常我们都是先安装mysql,然后才能去安装php.假如先安装php,后安装mysql,由于php需要连接mysql,因而在php引擎中需要配置使用mysql.so扩展.这时需要手动编译生成 ...