0

some strange things are happening while I try to convert an numpy array to a tuple.

code:

data_block = np.append(training_values, target_value)  # merge
print('data_block: ', data_block)
data_block = tuple(data_block)
print('data_block tuple: ', data_block)

output:

data_block:  [ 0.03478261  0.00869565  0.03478261  0.07826087  0.05217391  0.07826087 0.14782609]
data_block tuple:  (0.034782608695652174, 0.0086956521739130436, 0.034782608695652174, 0.078260869565217397, 0.052173913043478258, 0.078260869565217397, 0.14782608695652172)

Can someone explain to me what is happening?

This is part of a function that tries to create data that can be used for supervised learning out of a time series. Goal is to create a pandas data frame. The function itself is not finished yet and contains errors, but I want to post it here for more context.

def series_to_supervised(data_list, look_back=1, look_forward=0):
print(look_back)
data, labels = [], []
for i in range(len(data_list) - look_back):
    training_values = data_list[i:(i + look_back)]  
    target_value = data_list[i + look_back + look_forward]  
    print('target_value: ', target_value)

    data_block = np.append(training_values, target_value)  # merge
    data_block = tuple(data_block)
    data = np.append(data, data_block)  # add to data as tuple

for i in range(look_back):
    labels.append("lb_" + str(i))
labels.append("target_value")
print(labels)

df = pandas.DataFrame(data=data)
return df
1
  • 1
    The values are not "messed up", they are just printed at a higher precision (more digits after the comma). Commented Dec 19, 2018 at 12:43

1 Answer 1

1

The values that you show for data_block and tuple(data_block) actually do match. Numpy arrays get printed a little differently from most other things in Python, so the output of the print statements looks a little different.

You can get the printed output to match like so:

with np.printoptions(precision=20, linewidth=9999):
    print(data_block)
print(tuple(data_block))

Output:

[0.034782608695652174 0.008695652173913044 0.034782608695652174 0.0782608695652174   0.05217391304347826  0.0782608695652174   0.14782608695652172 ]
(0.034782608695652174, 0.008695652173913044, 0.034782608695652174, 0.0782608695652174, 0.05217391304347826, 0.0782608695652174, 0.14782608695652172)

If you always want your numpy arrays to print at that high precision (which you probably don't in reality; it gets annoying quickly), you can set a global option in your script like so:

np.set_printoptions(precision=20)
Sign up to request clarification or add additional context in comments.

2 Comments

Aaahhh sry for that. Never thougt about that and it confused me. Many thanks and sorry for the stupid question :_(
floats are some damn mysterious objects when you're just starting out. No worries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.