使用 sklearn 的具有分类特征的多元线性回归 - python
Multiple linear regression with categorical features using sklearn - python
我有一个数据集,其中每个文档都有相应的分数/评级
dataset = [
{"text":"I don't like this small device", "rating":"2"},
{"text":"Really love this large device", "rating":"5"},
....
]
此外,我从同一数据集
的text
个变量中提取了一个类别(变量)的术语列表
x1 = [short, slim, small, shrink]
x2 = [big,huge,large]
那么,我如何用 multiple independent variables
作为单词列表( 或代表相应术语列表中任何单词的存在的变量进行线性回归,因为列表中的每个术语都是唯一的 ) 和 dependent variable as a rating
。换句话说
how could I evaluate term lists impact on the rating with sklearn
我使用 TfidfVectorizer
推导了文档术语矩阵。如果可能,请提供简单的代码片段或示例。
鉴于评论中的讨论,似乎解释应该是每个列表定义一个二进制变量,其值取决于列表中的任何单词是否出现在相关文本中。所以,让我们首先更改文本,使单词实际出现:
dataset = [
{"text": "I don't like this large device", "rating": "2"},
{"text": "Really love this small device", "rating": "5"},
{"text": "Some other text", "rating": "3"}
]
为了简化我们的工作,我们会将这些数据加载到数据框中,将评分更改为整数,并创建相关变量:
df = pd.DataFrame(dataset)
df['rating'] = df['rating'].astype(int)
df['text'] = df['text'].str.split().apply(set)
x1 = ['short', 'slim', 'small', 'shrink']
x2 = ['big', 'huge', 'large']
df['x1'] = df.text.apply(lambda x: x.intersection(x1)).astype(bool)
df['x2'] = df.text.apply(lambda x: x.intersection(x2)).astype(bool)
即此时df
是如下数据框:
rating text x1 x2
0 2 {this, large, don't, like, device, I} False True
1 5 {this, small, love, Really, device} True False
2 3 {other, Some, text} False False
有了这个,我们可以创建相关模型,并检查系数最终是什么:
model = LinearRegression()
model.fit(df[['x1', 'x2']], df.rating)
print(model.coef_) # array([ 2., -1.])
print(model.intercept_) # 3.0
正如评论中还提到的,这个东西最多会产生四个评级,每个 x1
和 x2
的组合是 True
或 False
.在这种情况下,碰巧所有可能的输出都是整数,但一般来说,它们不需要,也不需要限制在感兴趣的区间内。考虑到评级的顺序性质,这确实是某种 ordinal regression (cf. e.g. mord).
的情况
我有一个数据集,其中每个文档都有相应的分数/评级
dataset = [
{"text":"I don't like this small device", "rating":"2"},
{"text":"Really love this large device", "rating":"5"},
....
]
此外,我从同一数据集
的text
个变量中提取了一个类别(变量)的术语列表
x1 = [short, slim, small, shrink]
x2 = [big,huge,large]
那么,我如何用 multiple independent variables
作为单词列表( 或代表相应术语列表中任何单词的存在的变量进行线性回归,因为列表中的每个术语都是唯一的 ) 和 dependent variable as a rating
。换句话说
how could I evaluate term lists impact on the rating with sklearn
我使用 TfidfVectorizer
推导了文档术语矩阵。如果可能,请提供简单的代码片段或示例。
鉴于评论中的讨论,似乎解释应该是每个列表定义一个二进制变量,其值取决于列表中的任何单词是否出现在相关文本中。所以,让我们首先更改文本,使单词实际出现:
dataset = [
{"text": "I don't like this large device", "rating": "2"},
{"text": "Really love this small device", "rating": "5"},
{"text": "Some other text", "rating": "3"}
]
为了简化我们的工作,我们会将这些数据加载到数据框中,将评分更改为整数,并创建相关变量:
df = pd.DataFrame(dataset)
df['rating'] = df['rating'].astype(int)
df['text'] = df['text'].str.split().apply(set)
x1 = ['short', 'slim', 'small', 'shrink']
x2 = ['big', 'huge', 'large']
df['x1'] = df.text.apply(lambda x: x.intersection(x1)).astype(bool)
df['x2'] = df.text.apply(lambda x: x.intersection(x2)).astype(bool)
即此时df
是如下数据框:
rating text x1 x2
0 2 {this, large, don't, like, device, I} False True
1 5 {this, small, love, Really, device} True False
2 3 {other, Some, text} False False
有了这个,我们可以创建相关模型,并检查系数最终是什么:
model = LinearRegression()
model.fit(df[['x1', 'x2']], df.rating)
print(model.coef_) # array([ 2., -1.])
print(model.intercept_) # 3.0
正如评论中还提到的,这个东西最多会产生四个评级,每个 x1
和 x2
的组合是 True
或 False
.在这种情况下,碰巧所有可能的输出都是整数,但一般来说,它们不需要,也不需要限制在感兴趣的区间内。考虑到评级的顺序性质,这确实是某种 ordinal regression (cf. e.g. mord).