thieu1995 / IntelELM

IntelELM: A Python Framework for Intelligent Metaheuristic-based Extreme Learning Machine
https://intelelm.readthedocs.io
GNU General Public License v3.0
12 stars 3 forks source link

get something wrong #2

Closed tangling123 closed 1 year ago

tangling123 commented 1 year ago

Description

Dear thieu, I want to compare you code with kaggle, and I revise the code but something wring,

`import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler, LabelEncoder from sklearn.neighbors import KNeighborsClassifier from permetrics import ClassificationMetric from intelelm import MhaElmClassifier from sklearn.model_selection import KFold #for K-fold cross validation from sklearn.model_selection import cross_val_score #score evaluation from sklearn.model_selection import cross_val_predict #prediction from sklearn.metrics import confusion_matrix #for confusion matrix import matplotlib.pyplot as plt import seaborn as sns

Loading data

hazel_df = pd.read_csv("hazelnuts.txt", sep="\t", header=None) hazel_df = hazel_df.transpose() hazel_df.columns = ["sample_id", "length", "width", "thickness", "surface_area", "mass", "compactness", "hardness", "shell_top_radius", "water_content", "carbohydrate_content", "variety"] print(hazel_df.head())

all_features = hazel_df.drop(["variety","sample_id"],axis=1) target_feature = hazel_df["variety"] print(all_features.head())

Dataset preprocessing

X = all_features.values.astype(float) # returns a numpy array of type float y = target_feature.values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

scaler_X = MinMaxScaler() scaler_y = LabelEncoder()

X_train = scaler_X.fit_transform(X_train) X_test = scaler_X.transform(X_test) y_train = scaler_y.fit_transform(y_train) y_test = scaler_y.transform(y_test)

print(np.unique(y_train)) print(len(np.unique(y_test)))

KNN

model = KNeighborsClassifier(n_neighbors = 25)

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

cm = ClassificationMetric(y_test, y_pred, decimal=6)

print("Results of KNN!")

print(cm.get_metrics_by_list_names(["AS", "RS", "PS", "F1S"]))

elm_multi_class

from sklearn import preprocessing x = all_features.values.astype(float) #returns a numpy array of type float min_max_scaler = preprocessing.MinMaxScaler() x_scaled = min_max_scaler.fit_transform(x) scaled_features = pd.DataFrame(x_scaled) scaled_features.head()

opt_paras = {"name": "GA", "epoch": 100, "pop_size": 30}

opt_paras = {"epoch": 100, "pop_size": 30, "pc": 0.9, "pm" : 0.05}

model = MhaElmClassifier(hidden_size=10, act_name="elu", obj_name="BSL", optimizer="BaseGA", optimizer_paras=opt_paras, verbose=False) model.fit(X_train, y_train) dt_ga = model.predict(X_test) kfold = KFold(n_splits=10, random_state=22, shuffle=True) # k=10, split the data into 10 equal parts= result_ga=cross_val_score(model,scaled_features,target_feature,cv=kfold,scoring='accuracy')

print('The overall score for K Nearest Neighbors Classifier is:',round(result_knn.mean()*100,2))

y_pred = cross_val_predict(model,scaled_features,target_feature,cv=10) sns.heatmap(confusion_matrix(dt_ga,y_test),annot=True,cmap='summer') plt.title('KNN Confusion_matrix')`

/Library/Python/3.9/site-packages/sklearn/preprocessing/_encoders.py:975: FutureWarning:sparsewas renamed tosparse_outputin version 1.2 and will be removed in 1.4.sparse_outputis ignored unless you leavesparseto its default value. warnings.warn( Traceback (most recent call last): File "/Users/tangling/Downloads/IntelELM-main/examples/test.py", line 75, in <module> result_ga=cross_val_score(model,scaled_features,target_feature,cv=kfold,scoring='accuracy') File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 562, in cross_val_score cv_results = cross_validate( File "/Library/Python/3.9/site-packages/sklearn/utils/_param_validation.py", line 211, in wrapper return func(*args, **kwargs) File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 309, in cross_validate results = parallel( File "/Library/Python/3.9/site-packages/sklearn/utils/parallel.py", line 65, in __call__ return super().__call__(iterable_with_config) File "/Library/Python/3.9/site-packages/joblib/parallel.py", line 1863, in __call__ return output if self.return_generator else list(output) File "/Library/Python/3.9/site-packages/joblib/parallel.py", line 1789, in _get_sequential_output for func, args, kwargs in iterable: File "/Library/Python/3.9/site-packages/sklearn/utils/parallel.py", line 61, in <genexpr> iterable_with_config = ( File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 311, in <genexpr> clone(estimator), File "/Library/Python/3.9/site-packages/sklearn/base.py", line 75, in clone return estimator.__sklearn_clone__() File "/Library/Python/3.9/site-packages/sklearn/base.py", line 268, in __sklearn_clone__ return _clone_parametrized(self) File "/Library/Python/3.9/site-packages/sklearn/base.py", line 110, in _clone_parametrized new_object = klass(**new_object_params) File "/Library/Python/3.9/site-packages/intelelm/model/mha_elm.py", line 220, in __init__ super().__init__(hidden_size=hidden_size, act_name=act_name, obj_name=obj_name, optimizer=optimizer, optimizer_paras=optimizer_paras, verbose=verbose) File "/Library/Python/3.9/site-packages/intelelm/base_elm.py", line 386, in __init__ self.optimizer = self._set_optimizer(optimizer, optimizer_paras) File "/Library/Python/3.9/site-packages/intelelm/base_elm.py", line 400, in _set_optimizer return optimizer.set_parameters(optimizer_paras) File "/Users/tangling/Library/Python/3.9/lib/python/site-packages/mealpy/optimizer.py", line 92, in set_parameters raise ValueError(f"Invalid input parameters: {new_para_names} for {self.get_name()} optimizer. " ValueError: Invalid input parameters: {'name', 'epoch', 'pop_size'} for GA optimizer. Valid parameters are: {'pc', 'epoch', 'pm', 'pop_size'}.

I follow the message to change parameters opt_paras = {"epoch": 100, "pop_size": 30, "pc": 0.9, "pm" : 0.05} but raise an error /Library/Python/3.9/site-packages/sklearn/preprocessing/_encoders.py:975: FutureWarning:sparsewas renamed tosparse_outputin version 1.2 and will be removed in 1.4.sparse_outputis ignored unless you leavesparseto its default value. warnings.warn( Traceback (most recent call last): File "/Users/tangling/Downloads/IntelELM-main/examples/test.py", line 75, in <module> result_ga=cross_val_score(model,scaled_features,target_feature,cv=kfold,scoring='accuracy') File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 562, in cross_val_score cv_results = cross_validate( File "/Library/Python/3.9/site-packages/sklearn/utils/_param_validation.py", line 211, in wrapper return func(*args, **kwargs) File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 309, in cross_validate results = parallel( File "/Library/Python/3.9/site-packages/sklearn/utils/parallel.py", line 65, in __call__ return super().__call__(iterable_with_config) File "/Library/Python/3.9/site-packages/joblib/parallel.py", line 1863, in __call__ return output if self.return_generator else list(output) File "/Library/Python/3.9/site-packages/joblib/parallel.py", line 1789, in _get_sequential_output for func, args, kwargs in iterable: File "/Library/Python/3.9/site-packages/sklearn/utils/parallel.py", line 61, in <genexpr> iterable_with_config = ( File "/Library/Python/3.9/site-packages/sklearn/model_selection/_validation.py", line 311, in <genexpr> clone(estimator), File "/Library/Python/3.9/site-packages/sklearn/base.py", line 75, in clone return estimator.__sklearn_clone__() File "/Library/Python/3.9/site-packages/sklearn/base.py", line 268, in __sklearn_clone__ return _clone_parametrized(self) File "/Library/Python/3.9/site-packages/sklearn/base.py", line 123, in _clone_parametrized raise RuntimeError( RuntimeError: Cannot clone object MhaElmClassifier(obj_name='BSL', optimizer=<mealpy.evolutionary_based.GA.BaseGA object at 0x16aefc790>, optimizer_paras={'epoch': 100, 'pc': 0.9, 'pm': 0.05, 'pop_size': 30}), as the constructor either does not set or modifies parameter optimizer

Additional Information

No response

thieu1995 commented 1 year ago

Hi @tangling123,

There is nothing wrong. You just don't know how to use it in the right way yet. Please check it here. I do this example for you. https://github.com/thieu1995/IntelELM/blob/main/tutorials/example_multiclass_classification.ipynb

tangling123 commented 1 year ago

oh, thank you bro, I will recommend the tools to my friends and cite your paper, thank you again

thieu1995 commented 1 year ago

@tangling123,

Thank you, I will close this issue now. Feel free to re-open if it is necessary