0
votes

How can I resolve the following error: dist = np.sum(train_data_features, axis=0) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/fromnumeric.py", line 1711, in sum return sum(axis=axis, dtype=dtype, out=out) TypeError: sum() got an unexpected keyword argument 'dtype'

this is my code:

import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from KaggleWord2VecUtility import KaggleWord2VecUtility
import pandas as pd
import numpy as np

if __name__ == '__main__':
    train = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'NYTimesBlogTrain.csv'), header=0)
    test = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'NYTimesBlogTest.csv'), header=0)
    train["Abstract"].fillna(0)
    print 'A sample Abstract is:'
    print train["Abstract"][0]
    #raw_input("Press Enter to continue...")


    #print 'Download text data sets. If you already have NLTK datasets downloaded, just close the Python download window...'
    #nltk.download()  # Download text data sets, including stop words

    # Initialize an empty list to hold the clean reviews
    clean_train_reviews = []
    # Loop over each review; create an index i that goes from 0 to the length
    # of the movie review list
    print "Cleaning and parsing the training set abstracts...\n"
    #for i in xrange( 0, len(train["Abstract"])):
    for i in xrange( 0, 10):
        if pd.isnull(train["Abstract"][i])==False:
            clean_train_reviews.append(" ".join(KaggleWord2VecUtility.review_to_wordlist(train["Abstract"][i], True)))
        else:
            clean_train_reviews.append(" ")
    print clean_train_reviews  

    # ****** Create a bag of words from the training set
    #
    print "Creating the bag of words...\n"


    # Initialize the "CountVectorizer" object, which is scikit-learn's
    # bag of words tool.
    vectorizer = CountVectorizer(analyzer = "word",   \
                             tokenizer = None,    \
                             preprocessor = None, \
                             stop_words = None,   \
                             max_features = 5000)

    # fit_transform() does two functions: First, it fits the model
    # and learns the vocabulary; second, it transforms our training data
    # into feature vectors. The input to fit_transform should be a list of
    # strings.
    print clean_train_reviews
    train_data_features = vectorizer.fit_transform(clean_train_reviews)
    print 'train_data_features'
    print train_data_features
    print train_data_features.shape
    # Take a look at the words in the vocabulary
    vocab = vectorizer.get_feature_names()
    print vocab

    # Sum up the counts of each vocabulary word
    dist = np.sum(train_data_features, axis=0)
1

1 Answers

1
votes

It looks like you can't sum what vectorizer is giving you. You will need a different way to do the sum, which you should be able to find in scipy's sparse library, most likely just by calling

dist = train_data_features.sum (axis=0)

Which I got from documentation on coo_sparse matrix sum. See details below

From sklearn documentation:

This implementation produces a sparse representation of the counts using scipy.sparse.coo_matrix.

From google search this type of error:

This never worked before because numpy doesn't know anything about scipy.sparse.