Why am I getting array instead of vector size? [closed]

2024/7/7 20:24:17

I want to get a vector size(46). But I getting array. The dataset that I used is Reuters.

The place where I print NN predictions is the last lines of code.

Code:

from keras.datasets import reuters
from keras import models, layers, losses
from keras.utils.np_utils import to_categorical
import 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].shape
print(predictions)

Output:

# WHY?                
[[4.2501447e-06 1.9825067e-07 2.3206076e-07 ... 2.1613120e-079.8317461e-09 1.3596014e-07][1.6055314e-02 1.4951903e-01 1.4057434e-04 ... 1.1199807e-041.8230558e-06 2.4111385e-03][7.8554759e-03 6.6994888e-01 1.6705523e-03 ... 4.0704478e-042.4865860e-05 7.2334736e-04]...[2.9577111e-06 9.5703072e-06 3.2641565e-05 ... 2.3492355e-061.8574113e-06 3.1159422e-07][1.7232201e-03 1.7063649e-01 1.5664790e-02 ... 4.8910693e-044.2799808e-04 1.0207186e-03][1.7965600e-04 6.5334785e-01 7.2387634e-03 ... 9.2276223e-061.9617393e-05 1.7480283e-05]]
Answer

Well, I got result that was need for me. I found him in another question from Stack Overflow: How to show graph in Visual Studio Code itself?

Use #%% for creating cells in that you will run independent fragments of code. It seems like Python Shell(this is Jupyter Notebook that I love).

Result that I wanted

Code

https://en.xdnf.cn/q/120629.html

Related Q&A

Sending Email attachment (.txt file) using Python 2.7 (smtplib) [duplicate]

This question already has answers here:How to send email attachments?(21 answers)Closed 9 years ago.So Im trying to send a .txt file as an attachment and I cant find the right code to work. Here is my…

Python selenium drop down menu click

i want to select option from a drop down menu, for this i use that :br.find_element_by_xpath("//*[@id=adyen-encrypted-form]/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").c…

TypeError: Argument must be rect style object - Pygame (Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Pygame (Python) - TypeError: Argument must be rect style object I am trying to make a brick breaker game in Pygame (with P…

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

CSV IO python: converting a csv file into a list of lists

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?Im having trouble with this because some of my entries have a comma in thema file like: 1,2,3…

Sheet of paper in millimeters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Kivy App build with Buildozer. APK crash

I am using Oracle VirtualBox running Ubuntu 16. I have been able to build apk files for a while until my latest build. My program will run and keep its functionality when run with python 2.7 on the sam…