Python Forum

Full Version: What is the cause of this error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following code generates an error as shown:

# random search logistic regression model on the sonar dataset
from scipy.stats import loguniform
from pandas import read_csv
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import RandomizedSearchCV
# load dataset
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.csv'
dataframe = read_csv(url, header=None)
# split into input and output elements
data = dataframe.values
X, y = data[:, :-1], data[:, -1]
# define model
model = LogisticRegression()
# define evaluation
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# define search space
space = dict()
space['solver'] = ['newton-cg', 'lbfgs', 'liblinear']
space['penalty'] = ['none', 'l1', 'l2', 'elasticnet']
space['C'] = loguniform(1e-5, 100)
# define search
search = RandomizedSearchCV(model, space, n_iter=500, scoring='accuracy', n_jobs=-1, cv=cv, random_st=1)
# execute search
result = search.fit(X, y)
# summarize result
print('Best Score: %s' % result.best_score_)
print('Best Hyperparameters: %s' % result.best_params_)
The error is: now shown:

Error:
TypeError Traceback (most recent call last) Input In [1], in <cell line: 23>() 21 space['C'] = loguniform(1e-5, 100) 22 # define search ---> 23 search = RandomizedSearchCV(model, space, n_iter=500, scoring='accuracy', n_jobs=-1, cv=cv, random_st=1) 24 # execute search 25 result = search.fit(X, y) TypeError: RandomizedSearchCV.__init__() got an unexpected keyword argument 'random_st'
I am attaching a captured printout of my install modules. I know it has something to do with scikit-learn, but I am not
sure what is causing the error.
Use the editor buttons to make python and error tags.

The error message tells you exactly where and what the error is.
This is your error. On line 23 you used an "unexpected" keyword argument, "random_st":
search = RandomizedSearchCV(model, space, n_iter=500, scoring='accuracy', n_jobs=-1, cv=cv, random_st=1)
RandomizedSearchCV does not have a random_st argument. Id does have a random_state argument.

https://scikit-learn.org/stable/modules/...rchCV.html
You are correct. But RandomizedSearchSercgCV must have had that argument at one of its earlier versions because I see a lot of legacy code with that argument. Actually, I see some legacy code. It was used in python in 2020.

So, when did it drop that argument? I think that must be the case.

This argument is used to insure the same random numbers are used each time the program is run. So, with the argument now eliminated what is done to ensure each run of the program uses the same random numbers?

Any help appreciated.

Led_Zeppelin
The parameter was not dropped. It was renamed. Does it matter why? I looked back as far as I could (version 0.15 from 2014), and it is has always been "random_state". If you want to search for documentation for even earlier versions, feel free.

I suggest reading the current documentation instead of depending on examples. Python changes all the time. Libraries change all the time. Compatibility is usually, but not always maintained. You have to adapt. There is a reason why "software maintenance" is a thing.