2

My code

import numpy as np
from pylab import plot,show
import scipy
import scipy.cluster
from scipy.cluster.vq import kmeans, vq

data_sillet = np.loadtxt('clustering_sillet.txt', delimiter=',')
data = data_sillet.astype(int)

print (type(data))

centroids, _ = scipy.cluster.vq.kmeans(data.astype(float), 3)
idx, _ = vq(data,centroids)

How to print the whole content of throwaway variable not just print (centroids)?

6
  • 3
    _ is used as a garbage variable, something you don't care about, use a real name instead if this value matter Commented Aug 13, 2017 at 16:03
  • 3
    even so, what's wrong with print ( _ )? It's ugly, and as @PRMoureu said, if you care about a variable, don't call it _, but it's still a valid name. Commented Aug 13, 2017 at 16:04
  • _ is still a valid identifier, so there's nothing that would forbid printing it using print(_). Commented Aug 13, 2017 at 16:05
  • _ is a valid variable name. It's just a convention that _ is a 'throwaway' variable. Commented Aug 13, 2017 at 16:05
  • 2
    In a script, _ is just another variable. However, in the interactive interpreter it is special: it's bound to the result of the last unassigned expression. Eg, if you do 1+2<Enter> 3 gets printed, and if you then do _<Enter> 3 gets printed again, and if you then do _+5<Enter> 8 will be printed. Commented Aug 13, 2017 at 16:39

1 Answer 1

5

Instead of

centroids, _ = scipy.cluster.vq.kmeans(data.astype(float), 3)

use

centroids, i_want_this = scipy.cluster.vq.kmeans(data.astype(float), 3)

and then print i_want_this.

Sign up to request clarification or add additional context in comments.

1 Comment

@DYZ - There are at least 3 good reasons for not do it: 1) not very descriptive name, 2) elusiveness, and 3) code maintaining.

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.