为什么我得到的是数组而不是向量大小?

我想得到一个向量大小(46)。但我得到的是数组。我使用的数据集是路透社(Reuters)。

我在打印神经网络预测的地方是代码的最后几行。

代码:

from keras.datasets import reutersfrom keras import models, layers, lossesfrom keras.utils.np_utils import to_categoricalimport numpy as np(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)word_index = reuters.get_word_index()reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])def vectorize_sequences(sequences, dimension=10000):    results = np.zeros((len(sequences), dimension))    for i, sequences in enumerate(sequences):        results[i, sequences] = 1.    return resultsx_train = vectorize_sequences(train_data)x_test = vectorize_sequences(test_data)one_hot_train_labels = to_categorical(train_labels)one_hot_test_labels = to_categorical(test_labels)model = models.Sequential()model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))model.add(layers.Dense(64, activation='relu'))model.add(layers.Dense(46, activation='softmax'))model.compile(optimizer='adam',            loss='categorical_crossentropy',             metrics=['accuracy'])x_val = x_train[:1000]partial_x_train = x_train[1000:]y_val = one_hot_train_labels[:1000]partial_y_train = one_hot_train_labels[1000:]history = model.fit(partial_x_train,                    partial_y_train,                    epochs=9,                     batch_size=128,                     validation_data=(x_val, y_val))predictions = model.predict(x_test)predictions[0].shapeprint(predictions)

输出:

# 为什么?                [[4.2501447e-06 1.9825067e-07 2.3206076e-07 ... 2.1613120e-07  9.8317461e-09 1.3596014e-07] [1.6055314e-02 1.4951903e-01 1.4057434e-04 ... 1.1199807e-04  1.8230558e-06 2.4111385e-03] [7.8554759e-03 6.6994888e-01 1.6705523e-03 ... 4.0704478e-04  2.4865860e-05 7.2334736e-04] ... [2.9577111e-06 9.5703072e-06 3.2641565e-05 ... 2.3492355e-06  1.8574113e-06 3.1159422e-07] [1.7232201e-03 1.7063649e-01 1.5664790e-02 ... 4.8910693e-04  4.2799808e-04 1.0207186e-03] [1.7965600e-04 6.5334785e-01 7.2387634e-03 ... 9.2276223e-06  1.9617393e-05 1.7480283e-05]]

回答:

嗯,我得到了我需要的结果。我在Stack Overflow的另一个问题中找到了它:如何在Visual Studio Code中显示图表?

使用#%%来创建单元格,你可以在其中运行独立的代码片段。这看起来像是Python Shell(这是我喜欢的Jupyter Notebook)。

我想要的结果

代码

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中创建了一个多类分类项目。该项目可以对…

发表回复

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