keras githubregressor有没有效果

莫烦 Keras #4 Regressor 回归 (教学 教程 tutorial)视频
莫烦 Keras #4 Regressor 回归 (教学 教程 tutorial)
来自:视频网
你可能有兴趣的股票成交量的预测基于Keras&sklearn
我的图书馆
股票成交量的预测基于Keras&sklearn
编辑部关键字全网搜索最新排名『量化投资』:排名第一『量 & & & 化』:排名第一『机器学习』:排名第四我们会再接再厉成为全网优质的金融、技术类公众号美国股市上午9:30开盘,下午4:00收盘闭,提供上午9:30和下午2:00之间的交易数据,从下午2:00到下午4:00预测成交量的变动。import pandas as pdimport timeimport numpy as npimport matplotlib.pyplot as plt
pd.options.mode.chained_assignment = None &plt.style.use('ggplot')度量错误率:平均绝对百分比误差(MAPE)。def mean_absolute_percentage_error(y_true, y_pred):
& &return np.mean(np.abs((y_true - y_pred) / y_true)) * 10数据说明训练数据包含1882个不同日期的613220个例子和352个例子。测试数据包含614098个例子,我们的目的是预测目标值。Training = pd.read_csv('training_input.csv', delimiter=',')
Output = pd.read_csv('training_output.csv', delimiter=';')
Testing = pd.read_csv('testing_input.csv', delimiter=',')print Training.shape
Training.head()()print Output.shape
Output.head()()print 'Number of product_id :', len(Training['product_id'].unique())print 'Number of date :', len(Training['date'].unique())Number of product_id : 352
Number of date : 1882这个数据集的主要困难是在训练和测试集中都有大量的缺失值。因此,有两个关键问题:预测目标值,并处理缺失值。print 'Number of rows with missing values in the test set :',TrainiNumber of rows with missing values in the test set : 99273我们逐行研究缺失值的数量(对于具有缺少条目的行):我们绘制单个行的缺失值的直方图;此数据集中的大多数示例只有1或2个缺失值。print 'Distribution of the rows with missing values in the train set'(Training.shape[1] - Training.count(axis=1)).value_counts()[1:].plot(kind='bar',
figsize=(14, 6), & & & & & & & & & & & & & & & & & & & & & & & & & & & print 'Distribution of the rows with missing values in the test set'(Testing.shape[1] - Testing.count(axis=1)).value_counts()[1:].plot(kind='bar',
figsize=(14, 6), & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & print 'Distribution of the missing values in the train set by features'(Training.shape[0] - Training.count(axis=0)).sort_values(ascending=False)[:-3].plot(kind='bar',
figsize=(14, 6), & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &我们清楚地看到,这两个图形非常相似,第一个时间变量'09:30:00'的缺失值表示为sur-representation。开始要对我们的预测变量的质量有一个很好的初步了解,在这里删除所有缺少条目的示例,并在完全填充的数据上构建我们的预测模型。train = pd.merge(Training, Output, on='ID', how='inner')
train_full = train.drop(pd.isnull(train).any(1).nonzero()[0]).resetimport pywtclass DictT(object):def __init__(self, name, level):
& & & & & &self.name = name
& & & & & &self.level = level
& & & & & &self.sizes = [] & &def dot(self, mat):
& & m = [] & & & & & &if mat.shape[0] != mat.size: & & & & & & & & & &for i in xrange(mat.shape[1]):
& & & & & &c = pywt.wavedec(mat[:, i], self.name, level=self.level)
& & & & & &self.sizes.append(map(len, c))
& & & & & &c = np.concatenate(c)
& & & & & &m.append(c) & & & & &
& & & &return np.asarray(m).T & & & & & & else:
& & & & c = pywt.wavedec(mat, self.name, level=self.level)
& & & & self.sizes.append(map(len, c)) & & & & & & & & & & return np.concatenate(c)现在,为了计算矩阵X的变换,我们简单地将其替换为以下:wave_name = 'db20'wave_level = Nonewavelet_operator_t = DictT(level=wave_level, name=wave_name)
basis_t = wavelet_operator_t.dot(np.identity(X.shape[1]))
basis_t /= np.sqrt(np.sum(basis_t ** 2, axis=0))
basis = basis_t.T现在,X变为X.dot(基准)作为回归模型的输入。 不幸的是,离散小波变换(或傅里叶变换)给出了比最后一个方法更简单的结果,如下所述。 此外,当处理缺失值时,内插数据的DWT / DFT(参见下面的部分)介绍了所有级别的内插噪声,并使此表示在此方法中成为不方便的选择。CNN我们还观察了深度学习模型的回归阶段。 其中,我们在数据集上尝试了LSTM和卷积神经网络。我们使用Keras轻松组建网络,并且大量地使用参数,包括Adam Optimizer的学习步骤,每个卷积层的过滤器数量或过滤器长度。不幸的是,没有参数配置给我们一个低于32%的训练分数。 我们仍然相信,这种方法仍然具有很大的潜力,我们可以通过更多的实验获得更多的结果。from keras.models import Sequentialfrom keras.layers.core import Dense, Dropout, Activationfrom keras.layers.convolutional import Convolution1D, MaxPooling1Dfrom keras.layers.core import Flattenfrom keras.optimizers import Adam# Reshape data.train_shape = (x_train.shape[0], x_train.shape[1], 1)
val_shape = (x_val.shape[0], x_val.shape[1], 1)
x_train_2 = np.reshape(x_train, train_shape).astype(theano.config.floatX)
x_val_2 = np.reshape(x_val, val_shape).astype(theano.config.floatX)# CNN Model.model = Sequential()
model.add(Convolution1D(nb_filter=16,
& & & & & & & & & & & &filter_length=2,
& & & & & & & & & & & &init='glorot_uniform',
& & & & & & & & & & & &input_shape=(x_train.shape[1], 1)))
model.add(Activation('relu'))
model.add(MaxPooling1D(3))
model.add(Dropout(0.5))
model.add(Convolution1D(nb_filter=32,
& & & & & & & & & & & &filter_length=4,
& & & & & & & & & & & &init='glorot_uniform'))
model.add(Activation('relu'))
model.add(MaxPooling1D(2))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, init='glorot_uniform'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
learning_rate = 10batch_size = 100nb_epoch = 10adam = Adam(lr=learning_rate)
pile(loss='mean_absolute_percentage_error', optimizer=adprint 'Training | Batch size :', batch_size, ', Number of epochs :', nb_epoch
model.fit(x_train_2, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
& & & & &validation_data=(x_val_2, y_val), show_accuracy=True)
score, acc = model.evaluate(x_val_2, y_val, batch_size=batch_size,
& & & & & & & & & & & & & &show_accuracy=True)print 'Test score :', scoreprint 'Test accuracy:', acc最终回归在测试各种预测变量后,我们的主要选择是??????????????????????????????????????????,在缩短的数据集上使用????????????????????????进行参数选择。 考虑到在这样大的数据上发生的大的训练时间,这是执行网格搜索的唯一合理的方法。features = train_full.drop(['ID','product_id','TARGET'], axis=1)
X_columns = train_full.columns.drop(['ID','product_id','TARGET'])
X = features.values
y = train_full['TARGET'].valuesfrom sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
& &X, y, test_size=0.2, random_state=0)%%timefrom sklearn.ensemble import RandomForestRegressorfrom sklearn.cross_validation import cross_val_score
n_estimators = 50max_depth = 15max_features = 40reg = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, max_features=max_features)
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)print 'MAPE :', mean_absolute_percentage_error(y_test, y_pred)CPU times: user 13 μs, sys: 0 ns, total: 13 μs
Wall time: 16 μs使用我们训练好的预测变量,我们现在可以观测每个变量的重要性。ordering = np.argsort(reg.feature_importances_)[::-1]
importances = reg.feature_importances_[ordering]
feature_names = X_columns[ordering]
pd.DataFrame(importances, index=feature_names).plot(kind='bar', figsize=(14, 6), colo我们试图重新启动模型,没有最低重要性的特征,这可能在我们的回归中带来噪音。 我们尝试几种可能性,但是结果并不是很有说服力地完全消除它们。然而,我们从进一步的模型中删除“日期”,因为它在整个变量池中的重要性不大。如前所述,我们尝试了其他几个模型,没有很大的成功。 以下是对相同数据进行评估的??????????????????????????????????的示例:%%timefrom sklearn.ensemble import ExtraTreesRegressorfrom sklearn.cross_validation import cross_val_scorefrom sklearn.ensemble import AdaBoostRegressor
regto = AdaBoostRegressor(base_estimator=reg, n_estimators=20, learning_rate=0.8)
regto.fit(X_train, y_train)
y_pred = regto.predict(X_test)print 'MAPE :', mean_absolute_percentage_error(y_test, y_pred)MAPE : 36.
CPU times: user 6min 59s, sys: 2.79 s, total: 7min 2s
Wall time: 7min 7s处理缺失值&A.删除第一个时间变量如前所述,第一个时间变量'09:30:00'占用了大量的缺失数据。此外,我们看到这个变量在我们的模型中是最不重要的;因此,我们决定将其从未来的预测中删除。这种删除的效果是增加了训练的大小,因为我们只是在没有丢失值的情况下进行观察,而不改变我们的模型的性能。Training_new = train.copy()
Training_new.drop(['09:30:00'], axis=1, inplace=True)print train.shapeprint Training_new.shape()
()print 'Number of rows without missing values with the feature 09:30:00 in train set :', train. & &drop(pd.isnull(train).any(1).nonzero()[0]).shape[0]print 'Number of rows without missing values without the feature 09:30:00 in train set :', Training_new. & &drop(pd.isnull(Training_new).any(1).nonzero()[0]).shape[0]Number of rows without missing values with the feature 09:30:00 in train set : 513947
Number of rows without missing values without the feature 09:30:00 in train set : 534215Testing_new = Testing.copy()
Testing_new.drop(['09:30:00'], axis=1, inplace=True)print Testing.shapeprint Testing_new.shape()
()在测试集和训练集中,这种方法允许我们获得大约20000次观察而不丢失值。我们因此获得了训练集的尺寸和我们可以用我们的模型预测的测试集的观察值。train_full = Training_new.drop(pd.isnull(Training_new).any(1).nonzero()[0])from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
& &X, y, test_size=0.2, random_state=0)
%%timefrom sklearn.ensemble import RandomForestRegressorfrom sklearn.cross_validation import cross_val_score
n_estimators = 50max_depth = 15max_features = 40reg = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, max_features=max_features)
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)print 'MAPE :', mean_absolute_percentage_error(y_test, y_pred)MAPE : 29.
CPU times: user 17min 55s, sys: 12.1 s, total: 18min 7s
Wall time: 19min 7s补充缺失值我们尝试了几种技术来插值缺失值:sklearn的??????????????计算机(我们尝试的最差),以及从scipy继承的pandas插值技术的整个范围。我们将在这里只提供我们最好的结果:从scipy的时间插值方法。我们把训练组分成两个子集:没有缺少值的观测值(train_filled)和缺失值的观测值(train_missing)。train_filled = Training_new.drop(pd.isnull(Training_new).any(1).nonzero()[0]).reset_index(drop=True)
train_missing = Training_new[~Training_new['ID'].isin(train_filled['ID'].tolist())]print train_filled.shaperow = train_missing.drop(['ID', 'date', 'product_id', 'TARGET'], axis=1).ix[613193] # [62691]row.index = pd.to_datetime(row.index)print 'Missing values :', np.isnan(row).sum()
d = {'time': row.interpolate(method='time'),'none': row}
pd.DataFrame(d).plot(colormap=plt.get_cmap('bwr'), figsize=(14, 6))Missing values : 28直接帖结果:Dataset shape after filling :
()MAPE : 29.
CPU times: user 18min 14s, sys: 13.5 s, total: 18min 28s
Wall time: 19min 53s在这里只测试了缺少值的观察结果,结果相当糟糕。无插值的解决方案一个重要的说法是:我们使用的指标不仅惩罚了真实值与预测值之间的距离,而且还惩罚了这两者之间的相对位置。例如,我们用ypredypred表示我们的预测值,ytrueytrue为真值:如果ypred = 10,ytrue = 100,则两者之间的距离为90,MAPE为90%;但是如果ypred = 100,ytrue = 10,距离仍然为90,但MAPE现在为900%。 这就是为什么我们决定,如果具有太多缺失值的示例,如果在该产品训练集中设置了TARGET,则分配最小值。 这样,我们对这些观察结果最多有100%的误差。 这是一种限制观察值与缺失值的错误的方法。所以我们用训练集缺失值的观察结果来测试。train_filled = Training_new.drop(pd.isnull(Training_new).any(1).nonzero()[0]).reset_index(drop=True)
train_missing = Training_new[~Training_new['ID'].isin(train_filled['ID'].tolist())]print 'train_filled',train_filled.shapeprint 'train_missing',train_missing.shapetrain_filled ()
train_missing (79005, 57)MAPE : 83.所以对于这些观察值我们有84%的MAP。 但是我们也观察到,按产品,特征“目标”具有很大的标准偏差(几乎等于平均值)。mean_per_product_id = train.groupby('product_id').agg({'TARGET': np.mean})
mean_per_product_id.columns = ['TARGET_mean']
std_per_product_id = train.groupby('product_id').agg({'TARGET': np.std})
std_per_product_id.columns = ['TARGET_std']
pd.concat([mean_per_product_id, std_per_product_id], axis=1).head()这意味着最低限度与其他价值观相差甚远。 所以我们决定使用不是最小值,而是使用上面的值,而不是重新调整它。MAPE : 83.MAPE : 69.
MAPE : 62.
MAPE : 61.
MAPE : 65.我们确定了以下最小调整价值:MAPE : 61.预测测试集如前所述,我们将测试集分为两个子集:一个没有任何缺少的条目,其余的。在第一种情况下,我们将使用我们训练好的回归器进行预测;在第二种情况下,我们将通过输出每个产品的重新调整的最小值来控制错误。test_filled = Testing_new.drop(pd.isnull(Testing_new).any(1).nonzero()[0]).reset_index(drop=True)
test_missing = Testing_new[~Testing_new['ID'].isin(test_filled['ID'].tolist())]
train_full = Training_new.drop(pd.isnull(Training_new).any(1).nonzero()[0])
features = train_full.drop(['ID','product_id','date','TARGET'], axis=1)
X_columns = train_full.columns.drop(['ID','product_id','date','TARGET'])
X = features.values
y = train_full['TARGET'].values
%%timefrom sklearn.ensemble import RandomForestRegressorfrom sklearn.cross_validation import cross_val_score
n_estimators = 50max_depth = 15max_features = 40reg = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, max_features=max_features)
reg.fit(X, y)
y_pred_filled = reg.predict(test_filled[numeric_cols].values)
submission_filled = test_filled[['ID', 'product_id']].copy()
submission_filled['TARGET'] = y_pred_filled
submission_filled.head()
min_per_product_id = train.groupby('product_id').agg({'TARGET': np.min})
min_per_product_id.head()
submission_missing = test_missing.copy().join(min_per_product_id, on='product_id')
submission_missing.head()
submission_missing['TARGET'] = 3.65 * submission_missing['TARGET']我们最终连接结果,按ID排序,按照所需的格式输出。submission = pd.concat([submission_filled, submission_missing], axis=0).sort_values(by='ID')
submission = submission[['ID','TARGET']]print submission.shape
submission.head()我们终于获得了37,25%的MAP。投稿、商业合作请发邮件到:关注者从1到10000 我们每天都在进步
TA的最新馆藏
喜欢该文的人也喜欢使用keras模型和sklearn库做机器学习任务 - CSDN博客
使用keras模型和sklearn库做机器学习任务
keras是python中比较流行的深度学习库,但是keras本身关注的是深度学习。而python中的scikit-learn库是建立在Scipy上的,有着比较有效的数值计算能力。sklearn是一个具有全特征的通用性的机器学习库,它提供了很多在深度学习中可以用到的工具。举个例子:
1.可以用sklearn中的k-fold交叉验证方法来对模型进行评估
2.模型参数的估计和寻找
Keras提供了深度学习模型的简便包装,可以在sklearn中被用来做分类和回归。在本片文章中,我们举这么一个例子:使用Keras建立神经网络分类器——KerasClassifier,并在scikit-learn库中使用这个分类器来对UCI的Pima Indians数据集进行分类。
利用Keras进行分类或者回归,主要是利用keras中的两个类,一个是KerasClassifier,另一个是KerasRegressor。这两个类带有参数build_fn。build_fn是你创建的keras模型的名称。在创建一个keras模型时,务必要把完成模型的定义,编译和返回。在这里我们假设我们建立的模型叫做create_model()
def create_model():
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
</pile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
将建立好的模型通过参数build_fn传递到KerasClassifier中,并且定义其他的参数选项nb_epoch=150,batch_size=10.KerasClassifier会自动调用fit方法。
在sklearn中,我们使用它cross_validation的包中的StratifiedKFold来进行10折交叉验证,使用cross_val_score来对模型进行评价。
kfold = StratifiedKFold(y=Y, n_folds=10, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold)
本文已收录于以下专栏:
相关文章推荐
实现了正弦曲线的拟合,即regression问题。
创建的模型单输入单输出,两个隐层分别为100、50个神经元。
在keras的官方文档中,给的例子多是关于分类的。因此在测试regression时...
一、分类神经网络构建过程
本例程是在MNIST数据集,构建一个简单分类神经网络,实现对0-9这20个数字的分类。
1.数据预处理
Keras 自身包含 MNIST 这个数据集,再分成训练集和测试集。x...
To know more or get code samples, please visit my website:
https://morvanzhou.github.io/tutorial...
超参数优化是深度学习中的重要组成部分。其原因在于,神经网络是公认的难以配置,而又有很多参数需要设置。最重要的是,个别模型的训练非常缓慢。在这篇文章中,你会了解到如何使用scikit-learn pyt...
keras是python中比较流行的深度学习库,但是keras本身关注的是深度学习。而python中的scikit-learn库是建立在Scipy上的,有着比较有效的数值计算能力。sklearn是一个...
最近几个月为了写小论文,题目是关于用深度学习做人脸检索的,所以需要选择一款合适的深度学习框架,caffe我学完以后感觉使用不是很方便,之后有人向我推荐了Keras,其简单的风格吸引了我,之后的四个月我...
Keras是基于Theano的一个深度学习框架,它的设计参考了Torch,用Python语言编写,是一个高度模块化的神经网络库,支持GPU和CPU。keras官方文档地址 地址
Keras主要包括8大模块,分别是Models、layers、Initializations、Activations、Objectives、Optimizers、Preprocessing、metri...
基于Theano的深度学习(Deep Learning)框架Keras学习随笔-02-Example -- 下面来看几个例子,来了解一下Keras的便捷之处。不需要具体去研究代码的意思,只需要看一下这...
例 5-1 作用域实例// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_1.cpp#include "stdafx.h"
他的最新文章
讲师:何宇健
讲师:董岩
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 keras tensorboard 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信