以下代码报错SyntaxError: positional argument follows keyword argument 代码:from sklearn.model_selection import train_test_split df=raw_data.dropna(axis=0) df_binomial=df[(df['Review']==5)|(df['Rating']==1)] x=df_binomial['Review Text'] y=df_binomial['Rating'] transformer=CountVectorizer(analyzer=text_process,ngram_range(2,2),stop_words='english').fit(x) print(len(transformer.vocabulary_))
时间: 2024-03-17 18:44:48 浏览: 18
这个错误是因为在函数调用中,位置参数出现在关键字参数之后,Python不允许这种语法。在这个代码段中,CountVectorizer的analyzer参数是一个位置参数,而ngram_range和stop_words是关键字参数。你需要把analyzer参数改成关键字参数的形式,例如analyzer=text_process,然后放在ngram_range和stop_words之前。修改后的代码如下:
```python
from sklearn.model_selection import train_test_split
df = raw_data.dropna(axis=0)
df_binomial = df[(df['Review'] == 5) | (df['Rating'] == 1)]
x = df_binomial['Review Text']
y = df_binomial['Rating']
transformer = CountVectorizer(
analyzer=text_process,
ngram_range=(2, 2),
stop_words='english'
).fit(x)
print(len(transformer.vocabulary_))
```
相关问题
运行报错SyntaxError: positional argument follows keyword argument
这个错误通常是由于函数调用时,位置参数放在了关键字参数后面引起的。请检查一下你的代码,看看是否有这种情况的出现。在这段代码中,我没有看到这样的错误,但是你可以尝试将代码改为以下形式,避免这种错误的发生:
```
from sklearn.model_selection import train_test_split
df = raw_data.dropna(axis=0)
df_binomial = df[(df['Review']==5)|(df['Rating']==1)]
x = df_binomial['Review Text']
y = df_binomial['Rating']
transformer = CountVectorizer(analyzer=text_process, ngram_range=(2,2), stop_words='english').fit(x)
print(len(transformer.vocabulary_))
```
在这里,我将 `ngram_range` 参数改为了 `(2,2)` 的形式,使用了关键字参数的形式,这样就不会出现位置参数放在关键字参数后面的情况了。
SyntaxError: positional argument follows keyword argument什么意思
"SyntaxError: positional argument follows keyword argument" 的意思是:位置参数在关键字参数后面。
在 Python 中,函数可以接收两种类型的参数:位置参数和关键字参数。位置参数是按顺序传递的,而关键字参数是按照名称传递的。当你在调用函数时,如果你先传递了一个或多个关键字参数,然后又传递了一个位置参数,就会出现该错误。
例如:
```python
def my_function(name, age):
print(name, age)
my_function(age=30, 'John')
```
就会出现 "SyntaxError: positional argument follows keyword argument" 的错误,因为先传递了 age 这个关键字参数,再传递了 name 这个位置参数。
要解决该错误,只需要将位置参数放在关键字参数前面即可:
```python
my_function('John', age=30)
```