SOLVED SVM : AttributeError: 'numpy.ndarray' object has no attribute 'lower' when I try fitting TFIDF with LinearSVC

妖精的绣舞 提交于 2020-03-04 15:38:19

问题


Please help me. I got error AttributeError: 'numpy.ndarray' object has no attribute 'lower' when I try put Pipeline code.

This is the code:

    from sklearn.model_selection import train_test_split
    X = X.replace([np.inf, -np.inf], np.nan)
    y = y.replace([np.inf, -np.inf], np.nan)
    X = X.dropna()
    y = y.dropna()
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

    from sklearn.feature_extraction.text import CountVectorizer
    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(X_train)

    from sklearn.feature_extraction.text import TfidfTransformer
    tfidf_transformer = TfidfTransformer()
    X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

    from sklearn.feature_extraction.text import TfidfVectorizer
    vectorizer = TfidfVectorizer()
    X_train_tfidf = vectorizer.fit_transform(X_train)

    from sklearn.svm import LinearSVC
    clf = LinearSVC()
    clf.fit(X_train_tfidf,y_train)

    from sklearn.pipeline import Pipeline
    text_clf = Pipeline([('tfidf',TfidfVectorizer()),('clf',LinearSVC())])
    text_clf.fit(X_train,y_train)

    if request.method == 'POST':
        message = request.form['message']
        data = [message]
        vect = vectorizer.transform(data).toarray()
        my_prediction = text_clf.predict(vect)

    return render_template('result.html',prediction = my_prediction)

SOLVED, this is a new code to fix it in my problem.

   from sklearn.feature_extraction.text import TfidfVectorizer
   from sklearn.svm import LinearSVC
   from sklearn.pipeline import Pipeline
        text_clf = Pipeline([('tfidf',TfidfVectorizer()),('clf',LinearSVC())])

I remove CountVectorizer and TfidfTransformer just it.

来源:https://stackoverflow.com/questions/60191634/solved-svm-attributeerror-numpy-ndarray-object-has-no-attribute-lower-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!