如何将我的训练数据输入到这个神经网络中

我在尝试用一段特定的代码解决一个分类问题,但我无法完全理解我的数据应该如何输入到神经网络中。

我开始使用1-of-C虚拟编码来编码数据,以便保留数据中的类别上下文。我还没有完全完成数据编码,因为我还不完全理解如何利用手头的代码来输入数据。

这是我目前编码的数据样本:

'In Raw format, for predicting Political Party Affiliation   'Age Sex     Income    Area      Party[0] 30  male    38000.00  urban     democrat[1] 36  female  42000.00  suburban  republican[2] 52  male    40000.00  rural     independent[3] 42  female  44000.00  suburban  other'Encoded Format[0] -1.23  -1.0  -1.34  ( 0.0   1.0)  (0.0  0.0  0.0  1.0)[1] -0.49   1.0   0.45  ( 1.0   0.0)  (0.0  0.0  1.0  0.0)[2]  1.48  -1.0  -0.45  (-1.0  -1.0)  (0.0  1.0  0.0  0.0)[3]  0.25   1.0   1.34  ( 1.0   0.0)  (1.0  0.0  0.0  0.0)

我对数值数据使用了高斯归一化,对字符串数据使用了1-of-C虚拟编码和1-of-(C-1)编码。数据的最后一列是类别。

考虑以下代码;输入变量X接受以下格式的数据:

X=np.array([[1,0,1,0],[1,0,1,1],[0,1,0,1]])

我是否应该这样输入我的数据,直到我遍历完所有数据?

X=np.array([[-1.23,-1,-1.34,0010],[00000010,-.49,1,.45],[1000,00001000,1.48,-1]])

我阅读了以下Stack Overflow的问题:如何将输入数据集输入到神经网络中?,这有助于澄清过程。特征应逐行输入,目标特征/标签(在本例中为政治党派)作为每行的最后一个特征。这对我来说是有道理的。在发布的代码中,我假设变量Y是目标。

考虑到这一点,我的输入应该是这样的吗:

X=np.array([[-1.23,-1,-1.34,0010],[00000010,0,0,0],[0,0,0,0]])

我只捕获第一行,并将目标特征作为最后一个输入?

我不确定应该是哪一种。提前感谢您的任何帮助。

import numpy as np#Input array X=np.array([[1,0,1,0],[1,0,1,1],[0,1,0,1]])#Output y=np.array([[1],[1],[0]])#Sigmoid Function def sigmoid (x): return 1/(1 + np.exp(-x))#Derivative of Sigmoid Function def derivatives_sigmoid(x): return x * (1 - x)#Variable initialization epoch=5000 #Setting training iterations lr=0.1 #Setting learning rate inputlayer_neurons = X.shape[1] #number of features in data set hiddenlayer_neurons = 3 #number of hidden layers neurons output_neurons = 1 #number of neurons at output layer#weight and bias initialization wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons)) bh=np.random.uniform(size=(1,hiddenlayer_neurons)) wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons)) bout=np.random.uniform(size=(1,output_neurons))for i in range(epoch):#Forward Propogation hidden_layer_input1=np.dot(X,wh) hidden_layer_input=hidden_layer_input1 + bh hiddenlayer_activations = sigmoid(hidden_layer_input) output_layer_input1=np.dot(hiddenlayer_activations,wout) output_layer_input= output_layer_input1+ bout output = sigmoid(output_layer_input)#Backpropagation E = y-output slope_output_layer = derivatives_sigmoid(output) slope_hidden_layer = derivatives_sigmoid(hiddenlayer_activations) d_output = E * slope_output_layer Error_at_hidden_layer = d_output.dot(wout.T) d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer wout += hiddenlayer_activations.T.dot(d_output) *lr bout += np.sum(d_output, axis=0,keepdims=True) *lr wh += X.T.dot(d_hiddenlayer) *lr bh += np.sum(d_hiddenlayer, axis=0,keepdims=True) *lrprint output

回答:

很好的问题。

首先,使用来自SciKit Learn的预构建神经网络实现

接下来,将你的数据分成特征和标签(首先展平你的输入向量)

X_features=X[:,:-1]X_labels=X[:,-1]

然后设置SciKit MLP

model=MLPClassifier(args...)

拟合你的数据

model.fit(X_features,X_labels)

瞧…

现在你可以用以下方式预测新的输入

Y=model.predict(input_vector)

注意:为了真正的数据科学,请记得将你的数据分成训练集和验证集(例如90/10)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注