分类器中的特征

Features in a classifier

我正在测试不同的分类器(SVM、逻辑回归、随机森林、朴素贝叶斯、梯度提升)。

我的数据集是这样的:

Text                 User         Date          Label
some text here     LucaDiMauro   2020/02/12        0
learning ML!!!     Mika          2018/12/03        1
Attention please!  user2         2012/02/04        1

等等。

1标识正常内容; 0 识别潜在的垃圾内容。

我确定了可以在主题中获得可信度的最重要特征:用户名中数字的存在、单词、字符、特殊字符的数量、代词的使用、句子开头的数字使用。我想知道如何使用这些选定的功能检查分类器的性能(需要一个,而不是全部)。

我的部分功能如下:

df['Punctuation']=df['Text'].str.findall('[?!<>']+')
Count = df['Text'].str.split().str.len()
df['comma_count'] = df.Text.str.count(',')
df.Text.astype(str).sum(axis=1).str.len()
df['User'] = pd.np.where(Text.str.contains("0"),"None",

我只想看看如何在模型中考虑这些特征来预测其他一些 spam/not 垃圾邮件。 目前尚不清楚如何将这些功能包含在我的分类器中。我一直认为Text作为清理、预处理的变量……我从来没有考虑过其他特性:只有Label(y)和Text as X。

例如,我使用了这个分类器:

# Import train_test_split function
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics

# Split dataset into training set and test set

y=df['Label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, stratify=y)

# all parameters not specified are set to their defaults
logisticRegr = LogisticRegression()
logisticRegr.fit(X_train, y_train)

logisticRegr.predict(X_test[0].reshape(1,-1))

logisticRegr.predict(X_test[0:10])

predictions = logisticRegr.predict(X_test)

score = logisticRegr.score(X_test, y_test)
print(score)

cm = metrics.confusion_matrix(y_test, predictions)
print(cm)

我想知道在此代码中我考虑的是其他功能还是仅考虑文本。 如果您能给我一个在分类器中集成某些功能的示例,那将非常有用。

这取决于pre-processing你做了什么。

如果您不确定是否包含其他功能,请尝试打印 X 的头部,看看是否包含其他功能。

考虑到您所做的 pre-processing,您的代码很可能会考虑其他功能,除非您故意决定只考虑 'Text' X。

附带说明,如果您已经从 Text 属性中提取了有用的信息并将它们作为单独的属性,那么您可能不再真正需要 'Text'。

我是新来的,无法添加评论。能否请您在问题中包括X的负责人?

编辑: 你可以试试这个..

# Extract features from Text and User as per your observations..
df['Punctuation']= df['Text'].str.contains("[?!<>']+")
Count = df['Text'].str.split().str.len()
df['comma_count'] = df.Text.str.count(',')
df['UserHasDigit'] = df['User'].apply(lambda x: 1 if(any(char.isdigit() for char in x)) == True else 0)

# Add more if you find any useful features

# Split the dependent (Target/Label) column from independent (features) columns
y = df['Label']
X = df.drop(columns=['Text', 'User', 'Label', 'Date']) # Drop attributes from which you extracted features and the ones that add no value

# print the head of X and y to see if it is correct
print("X")
print(X.head())
print("------")
print("y")
print(y.head())

# Apply any encoding if needed (Label-encoding / one-hot encoding)
# Now, apply test train split
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, stratify=y)

# carry on with the modeling as usual..

# ----- copied from your code ------
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
.
.
.
.

在此代码中,它考虑了所有功能(标点符号、comma_count、UserHasDigit.. 如果您添加了更多功能,则更多)而不是 'Text',因为它已被删除。