fit() got an unexpected keyword argument 'criterion' - python

I am working with sklearn.tree.DecisionTreeClassifier here is the link to it.
I want to use the keyword criterion and set it to "entropy"
I did the following :
model = DecisionTreeClassifier()
model.fit(X_train, y_train, criterion = "entropy")
but it gives this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-103-509174b2caad> in <module>()
1 model = DecisionTreeClassifier()
----> 2 model.fit(X_train, y_train, criterion = "entropy")
TypeError: fit() got an unexpected keyword argument 'criterion'
It is working fine with the default argument 'gini' but not with this.

You probably want
model = DecisionTreeClassifier(criterion="entropy")
model.fit(X_train, y_train)

Related

numpy.float64' object is not callable - hyperparameter tuning

I'm trying to do hyperparameter tuning and every time I run this code.
from sklearn.model_selection import GridSearchCV
param_grid = {'C':[0,1,1,100,1000], 'kernel':['rbf','poly','sigmoid','linear'],'degree':[1,2,3,4,5,6]}
grid =GridSearchCV(svc.sc(),param_grid)
grid.fit(X_train,y_train)
I get this error
TypeError Traceback (most recent call last)
<ipython-input-64-74de9eeb3cae> in <module>
3
4 param_grid = {'C':[0,1,1,100,1000], 'kernel':['rbf','poly','sigmoid','linear'],'degree':[1,2,3,4,5,6]}
----> 5 grid =GridSearchCV(svc.sc(),param_grid)
6 grid.fit(X_train,y_train)
TypeError: 'numpy.float64' object is not callable
Any idea what to do? Also svc.sc is the way defined the model.
What is svc.sc()? Either way, you're probably not meant to call it at that point, just pass it as the callback to GridSearchCV, i.e. drop the parentheses:
grid = GridSearchCV(svc.sc, param_grid)

TypeError: __init__() got an unexpected keyword argument 'i'

I am trying to determine the suitable neighbor number for machine learning KNneighbors algorithm.
from sklearn.neighbors import KNeighborsClassifier
training_accuracy = []
test_accuracy = []
n_s = range(1,11)
for i in n_s:
clf = KNeighborsClassifier(i=i)
clf.fit(X_train, y_train)
training_accuracy.append(clf.score(X_train, y_train))
test_accuracy.append(clf.score(X_test, y_test))
The error I am getting:
TypeError Traceback (most recent call last)
<ipython-input-12-80ad682622ef> in <module>
4 n_s = range(1,11)
5 for i in n_s:
----> 6 clf = KNeighborsClassifier(i=i)
7 clf.fit(X_train, y_train)
8 training_accuracy.append(clf.score(X_train, y_train))
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\neighbors\_classification.py in __init__(self, n_neighbors, weights, algorithm, leaf_size, p, metric, metric_params, n_jobs, **kwargs)
152 leaf_size=leaf_size, metric=metric, p=p,
153 metric_params=metric_params,
--> 154 n_jobs=n_jobs, **kwargs)
155 self.weights = _check_weights(weights)
TypeError: __init__() got an unexpected keyword argument 'i'
How can I fix this issue?
Instead of using i parameter, use n_neighbors instead.
for i in n_s:
clf = KNeighborsClassifier(n_neighbors=i)
Check the documentation of KNeighborsClassifier. Here you can see that there is no i parameter and n_neighbors is the first argument.
There is few updation in syntax of algorithm in wrt to time
Now This works for me !!
> for i in range(1,40):
> knn = RandomForestClassifier(i)

RANSAC algorithm using scikit-learn's RANSACRegressor

I tried to use the code below for fitting a robust regression model using RANSAC
from sklearn.linear_model import RANSACRegressor
ransac = RANSACRegressor(LinearRegression(),
max_trials=100,
min_samples=50,
residual_metric=lambda x: np.sum(np.abs(x), axis=1),
residual_threshold=5.0,
random_state=0)
ransac.fit(X,y)
And I get the following error below:
TypeError Traceback (most recent call last)
<ipython-input-38-832d8b5d351b> in <module>
5 residual_metric=lambda x: np.sum(np.abs(x), axis=1),
6 residual_threshold=5.0,
----> 7 random_state=0)
8 ransac.fit(X,y)
TypeError: __init__() got an unexpected keyword argument 'residual_metric'
Can you help me know what's wrong?
Most likely you got this code that was using an old version of ransac. The input residual_metric is deprecated. If you run without that, it works ok:
from sklearn.linear_model import RANSACRegressor, LinearRegression
ransac = RANSACRegressor(LinearRegression(),
max_trials=100,
min_samples=50,
residual_threshold=5.0,
random_state=0)
ransac
RANSACRegressor(base_estimator=LinearRegression(), min_samples=50,
random_state=0, residual_threshold=5.0)

PySpark logistic regression missing method

I am new to Pyspark. I am using logistic regression API. I followed some tutorials and worked this way :
from pyspark.ml.classification import LogisticRegression
train, test = df.randomSplit([0.80, 0.20], seed = some_seed)
LR = LogisticRegression(featuresCol = 'features', labelCol = 'label', maxIter=some_iter)
LR_model = LR.fit(train)
When I call
trainingSummary = LR_model.summary
trainingSummary.roc
I get
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-319-bf79768ab64e> in <module>()
1 trainingSummary = LR_model.summary
2
----> 3 trainingSummary.roc
AttributeError: 'LogisticRegressionTrainingSummary' object has no attribute 'roc'
Someone has an idea ?

sklearn RidgeClassifierCV: unexpected keyword argument 'random_state'

Why does RidgeClassifierCV from sklearn.linear_model not have a random_state keyword argument?
It cross-validates RidgeClassifier, which does have random_state. Should this keyword not be passed on? Or am I missing something here?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-612-4d3d523acbab> in <module>()
1 # Apply ridge regression
----> 2 model = linear_model.RidgeClassifierCV(cv=3, normalize=True, random_state=random_state)
3 model.fit(Xtrain, ytrain)
4 model = linear_model.RidgeClassifier(alpha=model.alpha_, normalize=True,
5 random_state=random_state)
TypeError: __init__() got an unexpected keyword argument 'random_state'

Categories

Resources