【问题标题】:Using three different labels in machine learning在机器学习中使用三种不同的标签
【发布时间】:2020-03-03 16:01:20
【问题描述】:

我真的是机器学习的大一新生。我正在审查在电子邮件中分隔垃圾邮件或非垃圾邮件值的代码。当我为另一个数据集设置代码时遇到问题。因此,我的数据集不仅包含 ham 或 spam 值。我有 2 个不同的分类值(年龄和性别)。当我尝试在下面的代码块中使用 2 个分类值时,我收到一个错误,解包的值太多。我怎样才能把我的全部价值观?

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test  = train_test_split(messages_bow, import_data['age'], import_data['gender'], test_size = 0.20, random_state = 0)

全码:

import numpy as np
import pandas
import nltk
from nltk.corpus import stopwords
import string

# Import Data.
import_data = pandas.read_csv('/root/Desktop/%20/%100.csv' , encoding='cp1252') 

# To See Columns Headers.
print(import_data.columns) 

# To Remove Duplications.
import_data.drop_duplicates(inplace = True) 

# To Find Data Size.
print(import_data.shape) 


#Tokenization (a list of tokens), will be used as the analyzer
#1.Punctuations are [!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]
#2.Stop words in natural language processing, are useless words (data).
def process_text(text):
    '''
    What will be covered:
    1. Remove punctuation
    2. Remove stopwords
    3. Return list of clean text words
    '''

    #1
    nopunc = [char for char in text if char not in string.punctuation]
    nopunc = ''.join(nopunc)

    #2
    clean_words = [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]

    #3
    return clean_words

#Show the Tokenization (a list of tokens )
print(import_data['text'].head().apply(process_text)) 

# Convert the text into a matrix of token counts.
from sklearn.feature_extraction.text import CountVectorizer 
messages_bow = CountVectorizer(analyzer=process_text).fit_transform(import_data['text']) 

#Split data into 80% training & 20% testing data sets

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test  = train_test_split(messages_bow, import_data['gender'], import_data['frequency'], test_size = 0.20, random_state = 0)

#Get the shape of messages_bow
print(messages_bow.shape)

【问题讨论】:

  • 嘿,艾哈迈德,你在关注哪个例子?你有链接吗?否则,你能插入一个示例数据吗?

标签: python pandas machine-learning sklearn-pandas


【解决方案1】:

train_test_split 将您传递给它的每个参数拆分为训练集和测试集。由于您要拆分三种不同类型的数据,因此需要 6 个变量:

X_train, X_test, age_train, age_test, gender_train, gender_test = train_test_split(messages_bow, import_data['age'], import_data['gender'], test_size=0.20, random_state=0)

【讨论】:

  • 感谢先生 :)
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 2022-12-14
  • 2020-03-09
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多