When using a decision tree classifier from scikit-learn, the docs show you reassigning the variable storing the classifier to the output of itself calling the fit() method:
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
However, now if I call the predict method:
clf.predict([[1,1]])
Pycharm warms me:
Unresolved attribute reference 'predict' for class 'object'
You can look up the declaration for fit() in Pycharm easily, and the method merely returns self, so the reassignment is not necessary and you can remove it so that I have instead:
clf = tree.DecisionTreeClassifier()
clf.fit(X, Y)
Everything runs smoothly both ways, but Pycharm doesn't give me a warning with the latter. I'm curious, because I'm fairly new to Python and Pycharm, why does it give me this warning? Is there a way to make this IDE recognize that the method returns self and therefore is still the same type with the same method predict()? Otherwise, is there any way to remove this warning?
You would want to do something like this instead to avoid the warning
km = KMeans(n_clusters=size)
km.fit(x)
predictions = km.predict(x)
For some reason, fit returns a object type. I am unsure if it's intentional. KMeans initializer returns the correct type and fit is in-place.
Related
I need to set the attribute activation_out = 'logistic' in a MLPRegressor of sklearn. It is supposed that this attribute can take the names of the relevant activation functions ('relu','logistic','tanh' etc). The problem is that I cannot find the way that you can control this attribute and set it to the preferred functions. Please, if someone has faced this problem before or knows something more, I want some help.
I have tried to set attribute to MLPRegressor(), error. I have tried with the method set_params(), error. I have tried manually to change it through Variable Explorer, error. Finally, I used MLPName.activation_out = 'logistic' but again when I used fit() method it changed to 'identity'.
CODE:
X_train2, X_test2, y_train2,y_test2 =
train_test_split(signals_final,masks,test_size=0.05,random_state =
17)
scaler2 = MinMaxScaler()
X_train2 = scaler.fit_transform(X_train2)
X_test2 = scaler.transform(X_test2)
MatchingNetwork = MLPRegressor(alpha = 1e-15,hidden_layer_sizes=
(300,)
,random_state=1,max_iter=20000,activation='logistic',batch_size=64)
MLPRegressor().out_activation_ = 'logistic'
You cannot. The output activation is determined by the problem type at fit time. For regression, the identity activation is used; see the User Guide.
Here is the relevant bit of source code. You might be able to hack it by fitting one iteration, changing the attribute, then using partial_fit, since then this _initialize method won't be called again; but it's likely to break when back-propogating.
Generally I think the sklearn neural networks aren't designed to be super flexible: there are other packages that play that role, are more efficient (use GPUs), etc.
kmeans = KMeans(n_clusters = k).fit(df)
preds = kmeans.fit_predict(df)
silhouette_score.append(silhouette_score(df, preds, metric = 'euclidean'))
TypeError: 'list' object is not callable
I am not sure why
You have probably created a list variable somewhere in your code named silhouette_score, which you now try to append. This way, you have overwritten the existing scikit-learn method silhouette_score, hence your script cannot find it now, considers silhouette_score a list, and when trying to execute
silhouette_score(df, preds, metric = 'euclidean')
throws an error as expected, because lists are indeed not callable.
You should never create variables with names identical to these of existing functions and methods. Change the name of your silhouette_score variable to something else, reset & rerun your script, and you should be fine.
Is there any way I can use partial_fit() in a BagginClassifier() which contains multiple MLPClassifier()?
My problem is binary classification, something like this:
clf = MLPClassifier()
model = BaggingClassifier(base_estimator=clf)
model.partial_fit(x, y, classes=[0, 1])
It keeps me giving this error:
AttributeError: 'BaggingClassifier' object has no attribute 'partial_fit'
Seems like it isn't. The documentation of sklearn gave the following list of modules that support partial_fit:
sklearn.naive_bayes.MultinomialNB
sklearn.naive_bayes.BernoulliNB
sklearn.linear_model.Perceptron
sklearn.linear_model.SGDClassifier
sklearn.linear_model.PassiveAggressiveClassifier
sklearn.linear_model.SGDRegressor
sklearn.linear_model.PassiveAggressiveRegressor
sklearn.cluster.MiniBatchKMeans
sklearn.decomposition.MiniBatchDictionaryLearning
sklearn.cluster.MiniBatchKMeans
I'm using StackingCVClassifier (from the mlxtend package) in Python and one of my classifiers is an XGBoostClassifier instance and I'm trying to pass a parameter (early_stopping_rounds or simply verbose) to its fit method via the fit method of the StackingCVClassifier object. The fit method does not seem to support this. The fit_transform method has a fit_params argument. But when I pass my parameter (e.g. **{'XGB_clf__early_stopping_rounds': 20}) it throw the following error:
fit() got an unexpected keyword argument 'XGB_clf__early_stopping_rounds.
Is this not supported? Or did I miss something?
Here is a sketch of what I'm trying to do:
XGB_clf = XGBClassifier()
other_clf = LogisticRegression()
stacked_clf = StackingCVClassifier(classifiers = [XGB_clf, other_clf], meta_classifier = LogisticRegression(), cv=2)
# trying to pass early_stopping_rounds to XGB_clf.fit
stacked_clf.fit(X_train, y_train, **{XGB_clf__early_stopping_rounds = 50})
The developer of mlxtend indicated (link below) that, as of September 2018, this is not yet possible.
https://github.com/rasbt/mlxtend/issues/434
The main aim is to add a deep learning classification method like CNN as an individual in ensemble in python.
The following code works fine:
clf1=CNN()
eclf1=VotingClassifier(estimators=[('lr', clf1)], voting='soft')
eclf1=eclf1.fit(XTrain,YTrain)
But, the error:
'NoneType' object has no attribute 'predict_proba'
comes up once running eclf1=eclf1.predict(XTest).
Just in case, The CNN consists of _fit_ function for training, and the following function:
def predict_proba(self,XTest):
#prediction=np.mean(np.argmax(teY, axis=1) == predict(teX))
teX=XTest.reshape(len(XTest),3,112,112)
p=predict(teX)
i = np.zeros((p.shape[0],p.max()+1))
for x,y in enumerate(p):
i[x,y] = 1
return i
Can you elaborate better what you did and which error you came across?
By your question only I can assume you tried to call 'predic_proba' after the line eclf1=eclf1.predict(XTest). And of course this will turn on an error because the eclf1.predict(XTest) returns an array, which doesn't have a predict() method.
Try just changing it to:
pred_results=eclf1.predict(XTest)
pred_result_probs = eclf1.predict_proba(XTest)