预测神经网络中的 Y 变量给出相同的数字

发布于 2025-01-13 23:30:16 字数 2754 浏览 1 评论 0原文

我想使用一个神经网络,其中变量 X 有 50 行和 5 列,变量 Y 有 5 个不同的值(类别)。

下面是使用 softmax 的代码,因为有 5 个类,但预测值始终是相同的数字。

如下所示,已对 XY 变量进行归一化。

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X).reshape((50,5))
X

给出

array([[ 0.29488391,  0.56011949, -0.46154113, -0.16126706,  0.29488391],
       [-3.39116499, -2.41923951,  1.42589534, -2.24213229,  0.29488391],
       [ 0.29488391,  0.56011949, -0.33571203, -0.94159152, -1.54814054],
       [-3.39116499, -2.41923951,  0.67092075,  0.03381406,  0.29488391],...])

对于 Y

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
y = ohe.fit_transform(y).toarray()
y

array([[0., 0., 0., 0., 1.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],...])

然后是模型:

model = Sequential()
model.add(Dense(4, input_dim=5, activation='relu'))
model.add(Dense(3, activation='relu'))
model.add(Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=100, batch_size=64)

然后是预测

 y_pred = model.predict(X_test)
 pred = list()
 for i in range(len(y_pred):
    pred.append(np.argmax(y_pred[i]))
 test = list()
 for i in range(len(y_test):
    test.append(np.argmax(y_test[i]))
 pred

,给出

[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

y_pred

给出

 array([[0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.1443704 , 0.12886432, 0.1797213 , 0.22652027, 0.32052374],
       [0.18675748, 0.18065727, 0.1806546 , 0.202152  , 0.24977861],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],...])

使用 y_pred = model.predict(X_train) 并打印y_pred 然后返回:

array([[1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [1.00677039e-06, 7.87629324e-06, 2.85332426e-05, 1.69713758e-02,
        9.82991219e-01],
       [1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [3.98923963e-01, 1.06269494e-01, 1.76579416e-01, 1.83922082e-01,
        1.34305030e-01],...])

I want to use a neural network with variable X which 50 rows and 5 columns and a variable Y which has 5 different values (categories).

Below is the code that uses softmax because there are 5 classes but the predicted values are always the same number.

As shown below, the normalization has been done on the X and Y variables.

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X).reshape((50,5))
X

which gives:

array([[ 0.29488391,  0.56011949, -0.46154113, -0.16126706,  0.29488391],
       [-3.39116499, -2.41923951,  1.42589534, -2.24213229,  0.29488391],
       [ 0.29488391,  0.56011949, -0.33571203, -0.94159152, -1.54814054],
       [-3.39116499, -2.41923951,  0.67092075,  0.03381406,  0.29488391],...])

and for Y

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
y = ohe.fit_transform(y).toarray()
y

which gives:

array([[0., 0., 0., 0., 1.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],...])

and then the model:

model = Sequential()
model.add(Dense(4, input_dim=5, activation='relu'))
model.add(Dense(3, activation='relu'))
model.add(Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=100, batch_size=64)

and then the prediction

 y_pred = model.predict(X_test)
 pred = list()
 for i in range(len(y_pred):
    pred.append(np.argmax(y_pred[i]))
 test = list()
 for i in range(len(y_test):
    test.append(np.argmax(y_test[i]))
 pred

which gives

[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

and

y_pred

which gives

 array([[0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],
       [0.1443704 , 0.12886432, 0.1797213 , 0.22652027, 0.32052374],
       [0.18675748, 0.18065727, 0.1806546 , 0.202152  , 0.24977861],
       [0.07307784, 0.06410044, 0.0949591 , 0.29170573, 0.47615686],...])

Using the y_pred = model.predict(X_train) and printing the y_pred then returns:

array([[1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [1.00677039e-06, 7.87629324e-06, 2.85332426e-05, 1.69713758e-02,
        9.82991219e-01],
       [1.39623194e-06, 9.69740995e-06, 3.63348372e-05, 1.66217107e-02,
        9.83330905e-01],
       [3.98923963e-01, 1.06269494e-01, 1.76579416e-01, 1.83922082e-01,
        1.34305030e-01],...])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文