Tagged: IPython

4,062  2

[TF.7]多类别神经网络

本文参考《Google机器学习速成课程》多类别神经网络点击下载源码 设置 !wget https://storage.googleapis.com/mledu-datasets/mnist_train_small.csv -O /tmp/mnist_train_small.csv import glob import io import math import os from IPython import display from matplotlib import cm from matplotlib import gridspec from matplotlib import pyplot as plt import numpy as np import pandas as pd import seaborn as sns from sklearn import metrics import tensorflow as tf from tensorflow.python.data import Dataset tf.logging.set_verbosity(tf.logging.ERROR) pd.options.display.max_rows = 10 pd.options.display.float_format = ‘{:.1f}’.format mnist_dataframe = pd.read_csv( io.open("/tmp/mnist_train_small.csv", "r"), sep=",", header=None) # Use just the first 10,000 records for training/validation mnist_dataframe = mnist_dataframe.head(10000) mnist_dataframe = mnist_dataframe.reindex(np.random.permutation(mnist_dataframe.index)) training_targets, training_examples = parse_labels_and_features(mnist_dataframe[:7500]) validation_targets, validation_examples = parse_labels_and_features(mnist_dataframe[7500:10000]) seaborn Seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,使用起来更加方便快捷。 构造神经网络模型 # Extracts labels and...

5,068  0

[TF.3]多特征训练与交叉验证

本文参考《Google机器学习速成课程》验证点击下载源码 交叉验证分为: 简单交叉验证 S折较差验证 留一交叉验证 import math from IPython import display from matplotlib import cm from matplotlib import gridspec from matplotlib import pyplot as plt import numpy as np import pandas as pd from sklearn import metrics import tensorflow as tf from tensorflow.python.data import Dataset tf.logging.set_verbosity(tf.logging.ERROR) pd.options.display.max_rows = 10 pd.options.display.float_format = ‘{:.1f}’.format #加载并准备数据 california_housing_dataframe = pd.read_csv(“https://storage.googleapis.com/mledu-datasets/california_housing_train.csv”, sep=”,”) #对数据进行随机化处理 california_housing_dataframe = california_housing_dataframe.reindex( np.random.permutation(california_housing_dataframe.index)) #对多个特征进行预处理 def preprocess_features(california_housing_dataframe): selected_features = california_housing_dataframe[ [“latitude”, “longitude”, “housing_median_age”, “total_rooms”, “total_bedrooms”, “population”, “households”, “median_income”]] processed_features = selected_features.copy() # Create a synthetic feature. processed_features[“rooms_per_person”] = ( california_housing_dataframe[“total_rooms”] / california_housing_dataframe[“population”]) return processed_features #对目标特征进行预处理 def preprocess_targets(california_housing_dataframe): output_targets = pd.DataFrame() # Scale...

3,141  0

[TF.1]使用 TensorFlow 的基本步骤

本文参考《Google机器学习速成课程》使用TF的基本步骤 点击下载源码 设置 import math from IPython import display from matplotlib import cm from matplotlib import gridspec from matplotlib import pyplot as plt import numpy as np import pandas as pd from sklearn import metrics import tensorflow as tf from tensorflow.python.data import Dataset tf.logging.set_verbosity(tf.logging.ERROR) pd.options.display.max_rows = 10 pd.options.display.float_format = ‘{:.1f}’.format california_housing_dataframe = pd.read_csv("https://storage.googleapis.com/mledu-datasets/california_housing_train.csv", sep=",") #加载数据集 california_housing_dataframe = california_housing_dataframe.reindex( np.random.permutation(california_housing_dataframe.index)) #对数据进行随机化处理,以确保不会出现任何病态排序结果(可能会损害随机梯度下降法的效果) california_housing_dataframe["median_house_value"] /= 1000.0 #将 median_house_value 调整为以千为单位 IPython ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能和函数,也是利用Python进行科学计算和交互可视化的一个最佳的平台。IPython提供了两个主要的组件: 1.一个强大的python交互式shell 2.供Jupyter notebooks使用的一个Jupyter内核(IPython notebook) 安装IPython: pip install ipythonpip install jupyter #想在notebook中或者在Qt console中使用IPython,我们还需要安装Jupyterpip install numpy #NumPy高性能多维数组矢量运算库pip install matplotlib #Matplotlib绘图以及交互式可视化 使用IPython: 1.直接win + R打开运行,然后输入ipython即可2.输入ipython qtconsole进入ipython图形交互界面 使用IPython Notebook: Win+R打开运行,输入ipython notebook,如果正确安装的话,这个命令就会默认在本地8888端口启动一个web服务,并自动打开浏览器,打开http://localhost:8888/tree页面,在这个页面我们可以看到当前目录下的所有文件夹以及ipynb文件。 scikit-learn(sklearn) sklearn是基于numpy和scipy的一个机器学习算法库,让我们能够使用同样的接口来实现所有不同的算法调用。支持包括分类、回归、降维和聚类四大机器学习算法,还包含了特征提取、数据处理和模型评估三大模块。拥有着完善的文档,上手容易,具有着丰富的API,内置大量数据集,在学术界颇受欢迎。 构建第一个模型...