Pv66
Pv66

Reputation: 155

How do I extract x co-ordinate of a point using Python

I'm trying to build an NMF model for topic extraction. For re-training of the model, I've to pass a parameter to the nmf function, for which I need to pass the x co-ordinate from a given point that the algorithm returns, here is the code for reference:

no_features = 1000
no_topics = 9
print ('Old number of topics: ', no_topics)
tfidf_vectorizer = TfidfVectorizer(max_df = 0.95, min_df = 2, max_features = no_features, stop_words = 'english')
tfidf = tfidf_vectorizer.fit_transform(documents)
tfidf_feature_names = tfidf_vectorizer.get_feature_names()

no_topics = tfidf.shape
print('New number of topics :', no_topics)
# nmf = NMF(n_components = no_topics, random_state = 1, alpha = .1, l1_ratio = .5, init = 'nndsvd').fit(tfidf)

On the third last line, the tfidf.shape returns a point (3,1000) to the variable 'no_topics', however I want that variable to be set to only the x co-ordinate, i.e (3). How can I extract just the x co-ordinate from the point?

Upvotes: 1

Views: 305

Answers (2)

Kalpit
Kalpit

Reputation: 1101

You can do a slicing on your numpy array tfidf with

topics = tfidf[0,:]

Upvotes: 1

Kenan
Kenan

Reputation: 14104

you can select the first values with no_topics[0]

print('New number of topics : {}'.format(no_topics[0]))

Upvotes: 1

Related Questions