{"id":22950,"date":"2024-11-07T10:50:57","date_gmt":"2024-11-07T02:50:57","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=22950"},"modified":"2024-11-07T10:50:57","modified_gmt":"2024-11-07T02:50:57","slug":"%e5%80%9f%e5%8a%a9llm%e5%ae%9e%e7%8e%b0%e6%a8%a1%e5%9e%8b%e9%80%89%e6%8b%a9%e5%92%8c%e8%af%95%e9%aa%8c%e8%87%aa%e5%8a%a8%e5%8c%96","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2024\/11\/07\/22950\/","title":{"rendered":"\u501f\u52a9LLM\u5b9e\u73b0\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u81ea\u52a8\u5316"},"content":{"rendered":"<p><img data-dominant-color=\"11558f\" data-has-transparency=\"false\" style=\"--dominant-color: #11558f;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-22952\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/11\/ca5cfa60-0ff6-46a2-be52-629f7ae0b05a-300x167-1.png\" width=\"300\" height=\"167\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/11\/ca5cfa60-0ff6-46a2-be52-629f7ae0b05a-300x167-1.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/11\/ca5cfa60-0ff6-46a2-be52-629f7ae0b05a-300x167-1-150x84.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"font-weight: 400;\">\u4ee3\u7801\u751f\u6210\u548c\u8bc4\u4f30\u6700\u8fd1\u5df2\u7ecf\u6210\u4e3a\u8bb8\u591a\u5546\u4e1a\u4ea7\u54c1\u63d0\u4f9b\u7684\u91cd\u8981\u529f\u80fd\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5904\u7406\u4ee3\u7801\u3002LLM\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7528\u4e8e\u5904\u7406\u6570\u636e\u79d1\u5b66\u5de5\u4f5c\uff0c\u5c24\u5176\u662f\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5c06\u81ea\u52a8\u5316\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u5df2\u6210\u4e3a\u4e00\u79cd\u5de5\u5177\uff0c\u4ece\u56de\u7b54\u95ee\u9898\u5230\u751f\u6210\u4efb\u52a1\u5217\u8868\uff0c\u5b83\u4eec\u5728\u8bb8\u591a\u65b9\u9762\u7b80\u5316\u4e86\u6211\u4eec\u7684\u5de5\u4f5c\u3002\u5982\u4eca\u4e2a\u4eba\u548c\u4f01\u4e1a\u5df2\u7ecf\u4f7f\u7528LLM\u6765\u5e2e\u52a9\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<p>\u4ee3\u7801\u751f\u6210\u548c\u8bc4\u4f30\u6700\u8fd1\u5df2\u7ecf\u6210\u4e3a\u8bb8\u591a\u5546\u4e1a\u4ea7\u54c1\u63d0\u4f9b\u7684\u91cd\u8981\u529f\u80fd\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5904\u7406\u4ee3\u7801\u3002LLM\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7528\u4e8e\u5904\u7406\u6570\u636e\u79d1\u5b66\u5de5\u4f5c\uff0c\u5c24\u5176\u662f\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u3002<\/p>\n<p>\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5c06\u81ea\u52a8\u5316\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u3002<\/p>\n<h4>\u501f\u52a9LLM\u5b9e\u73b0\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u81ea\u52a8\u5316<\/h4>\n<p>\u6211\u4eec\u5c06\u8bbe\u7f6e\u7528\u4e8e\u6a21\u578b\u8bad\u7ec3\u7684\u6570\u636e\u96c6\u548c\u7528\u4e8e\u81ea\u52a8\u5316\u7684\u4ee3\u7801\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u6765\u81eaKaggle\u7684\u4fe1\u7528\u6c7d\u8f66\u6b3a\u8bc8\u6570\u636e\u96c6\u3002\u4ee5\u4e0b\u662f\u6211\u4e3a\u9884\u5904\u7406\u8fc7\u7a0b\u6240\u505a\u7684\u51c6\u5907\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>import pandas as pd df = pd.read_csv(&#8216;fraud_data.csv&#8217;) df = df.drop([&#8216;trans_date_trans_time&#8217;, &#8216;merchant&#8217;, &#8216;dob&#8217;, &#8216;trans_num&#8217;, &#8216;merch_lat&#8217;, &#8216;merch_long&#8217;], axis =1) df = df.dropna().reset_index(drop = True) df.to_csv(&#8216;fraud_data.csv&#8217;, index = False)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5c06\u53ea\u4f7f\u7528\u4e00\u4e9b\u6570\u636e\u96c6\uff0c\u4e22\u5f03\u6240\u6709\u7f3a\u5931\u7684\u6570\u636e\u3002\u8fd9\u4e0d\u662f\u6700\u4f18\u7684\u8fc7\u7a0b\uff0c\u4f46\u6211\u4eec\u5173\u6ce8\u7684\u662f\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4e3a\u6211\u4eec\u7684\u9879\u76ee\u51c6\u5907\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u5c06\u6240\u6709\u76f8\u5173\u6587\u4ef6\u653e\u5728\u90a3\u91cc\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u4e3a\u73af\u5883\u521b\u5efarequirements.txt\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u8f6f\u4ef6\u5305\u6765\u586b\u5145\u5b83\u4eec\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>openai pandas scikit-learn pyyaml<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4e3a\u6240\u6709\u76f8\u5173\u7684\u5143\u6570\u636e\u4f7f\u7528YAML\u6587\u4ef6\u3002\u8fd9\u5c06\u5305\u62ecOpenAI API\u5bc6\u94a5\u3001\u8981\u6d4b\u8bd5\u7684\u6a21\u578b\u3001\u8bc4\u4f30\u5ea6\u91cf\u6307\u6807\u548c\u6570\u636e\u96c6\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>llm_api_key: &#8220;YOUR-OPENAI-API-KEY&#8221; default_models: &#8211; LogisticRegression &#8211; DecisionTreeClassifier &#8211; RandomForestClassifier metrics: [&#8220;accuracy&#8221;, &#8220;precision&#8221;, &#8220;recall&#8221;, &#8220;f1_score&#8221;] dataset_path: &#8220;fraud_data.csv&#8221;<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5bfc\u5165\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u8f6f\u4ef6\u5305\u3002\u6211\u4eec\u5c06\u4f9d\u9760Scikit-Learn\u7528\u4e8e\u5efa\u6a21\u8fc7\u7a0b\uff0c\u5e76\u4f7f\u7528OpenAI\u7684GPT-4\u4f5c\u4e3aLLM\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>import pandas as pd import yaml import ast import re import sklearn from openai import OpenAI from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0c\u6211\u4eec\u5c06\u8bbe\u7f6e\u8f85\u52a9\uff08helper\uff09\u51fd\u6570\u548c\u4fe1\u606f\u6765\u5e2e\u52a9\u8be5\u8fc7\u7a0b\u3002\u4ece\u6570\u636e\u96c6\u52a0\u8f7d\u5230\u6570\u636e\u9884\u5904\u7406\uff0c\u914d\u7f6e\u52a0\u8f7d\u5668\u5728\u5982\u4e0b\u7684\u51fd\u6570\u4e2d\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>model_mapping = { &#8220;LogisticRegression&#8221;: LogisticRegression, &#8220;DecisionTreeClassifier&#8221;: DecisionTreeClassifier, &#8220;RandomForestClassifier&#8221;: RandomForestClassifier } def load_config(config_path=&#8217;config.yaml&#8217;): with open(config_path, &#8216;r&#8217;) as file: config = yaml.safe_load(file) return config def load_data(dataset_path): return pd.read_csv(dataset_path) def preprocess_data(df): label_encoders = {} for column in df.select_dtypes(include=[&#8216;object&#8217;]).columns: le = LabelEncoder() df[column] = le.fit_transform(df[column]) label_encoders[column] = le return df, label_encoders<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<\/ul>\n<p>\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06LLM\u8bbe\u7f6e\u4e3a\u626e\u6f14\u673a\u5668\u5b66\u4e60\u89d2\u8272\u7684\u4e13\u5bb6\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u542f\u52a8\u5b83\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>def call_llm(prompt, api_key): client = OpenAI(api_key=api_key) response = client.chat.completions.create( model=&#8221;gpt-4&#8243;, messages=[ {&#8220;role&#8221;: &#8220;system&#8221;, &#8220;content&#8221;: &#8220;You are an expert in machine learning and able to evaluate the model well.&#8221;}, {&#8220;role&#8221;: &#8220;user&#8221;, &#8220;content&#8221;: prompt} ] ) return response.choices[0].message.content.strip()<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u5c06LLM\u6a21\u578b\u66f4\u6539\u4e3a\u6240\u9700\u7684\u6a21\u578b\uff0c\u6bd4\u5982\u6765\u81eaHugging\u00a0Face\u7684\u5f00\u6e90\u6a21\u578b\uff0c\u4f46\u6211\u4eec\u5efa\u8bae\u6682\u4e14\u575a\u6301\u4f7f\u7528OpenAI\u3002<\/p>\n<p>\u6211\u5c06\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\u51c6\u5907\u4e00\u4e2a\u51fd\u6570\u6765\u6e05\u7406LLM\u7ed3\u679c\u3002\u8fd9\u786e\u4fdd\u4e86\u8f93\u51fa\u53ef\u4ee5\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u6b65\u9aa4\u7684\u540e\u7eed\u8fc7\u7a0b\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>def clean_hyperparameter_suggestion(suggestion): pattern = r&#8217;\\{.*?\\}&#8217; match = re.search(pattern, suggestion, re.DOTALL) if match: cleaned_suggestion = match.group(0) return cleaned_suggestion else: print(&#8220;Could not find a dictionary in the hyperparameter suggestion.&#8221;) return None def extract_model_name(llm_response, available_models): for model in available_models: pattern = r&#8217;\\b&#8217; + re.escape(model) + r&#8217;\\b&#8217; if re.search(pattern, llm_response, re.IGNORECASE): return model return None def validate_hyperparameters(model_class, hyperparameters): valid_params = model_class().get_params() invalid_params = [] for param, value in hyperparameters.items(): if param not in valid_params: invalid_params.append(param) else: if param == &#8216;max_features&#8217; and value == &#8216;auto&#8217;: print(f&#8221;Invalid value for parameter &#8216;{param}&#8217;: &#8216;{value}'&#8221;) invalid_params.append(param) if invalid_params: print(f&#8221;Invalid hyperparameters for {model_class.__name__}: {invalid_params}&#8221;) return False return True def correct_hyperparameters(hyperparameters, model_name): corrected = False if model_name == &#8220;RandomForestClassifier&#8221;: if &#8216;max_features&#8217; in hyperparameters and hyperparameters[&#8216;max_features&#8217;] == &#8216;auto&#8217;: print(&#8220;Correcting &#8216;max_features&#8217; from &#8216;auto&#8217; to &#8216;sqrt&#8217; for RandomForestClassifier.&#8221;) hyperparameters[&#8216;max_features&#8217;] = &#8216;sqrt&#8217; corrected = True return hyperparameters, corrected<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5c06\u9700\u8981\u8be5\u51fd\u6570\u6765\u542f\u52a8\u6a21\u578b\u548c\u8bc4\u4f30\u8bad\u7ec3\u8fc7\u7a0b\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u7528\u4e8e\u901a\u8fc7\u63a5\u53d7\u5206\u5272\u5668\u6570\u636e\u96c6\u3001\u6211\u4eec\u8981\u6620\u5c04\u7684\u6a21\u578b\u540d\u79f0\u4ee5\u53ca\u8d85\u53c2\u6570\u6765\u8bad\u7ec3\u6a21\u578b\u3002\u7ed3\u679c\u5c06\u662f\u5ea6\u91cf\u6307\u6807\u548c\u6a21\u578b\u5bf9\u8c61\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>def train_and_evaluate(X_train, X_test, y_train, y_test, model_name, hyperparameters=None): if model_name not in model_mapping: print(f&#8221;Valid model names are: {list(model_mapping.keys())}&#8221;) return None, None model_class = model_mapping.get(model_name) try: if hyperparameters: hyperparameters, corrected = correct_hyperparameters(hyperparameters, model_name) if not validate_hyperparameters(model_class, hyperparameters): return None, None model = model_class(**hyperparameters) else: model = model_class() except Exception as e: print(f&#8221;Error instantiating model with hyperparameters: {e}&#8221;) return None, None try: model.fit(X_train, y_train) except Exception as e: print(f&#8221;Error during model fitting: {e}&#8221;) return None, None y_pred = model.predict(X_test) metrics = { &#8220;accuracy&#8221;: accuracy_score(y_test, y_pred), &#8220;precision&#8221;: precision_score(y_test, y_pred, average=&#8217;weighted&#8217;, zero_division=0), &#8220;recall&#8221;: recall_score(y_test, y_pred, average=&#8217;weighted&#8217;, zero_division=0), &#8220;f1_score&#8221;: f1_score(y_test, y_pred, average=&#8217;weighted&#8217;, zero_division=0) } return metrics, model<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<\/ul>\n<p>\u51c6\u5907\u5c31\u7eea\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5316\u8fc7\u7a0b\u4e86\u3002\u6709\u51e0\u4e2a\u6b65\u9aa4\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\uff0c\u5176\u4e2d\u5305\u62ec\uff1a<\/p>\n<p>1.\u8bad\u7ec3\u548c\u8bc4\u4f30\u6240\u6709\u6a21\u578b<\/p>\n<ol start=\"2\">\n<li>LLM\u9009\u62e9\u6700\u4f73\u6a21\u578b<\/li>\n<li>\u68c0\u67e5\u6700\u4f73\u6a21\u578b\u7684\u8d85\u53c2\u6570\u8c03\u4f18<\/li>\n<li>\u5982\u679cLLM\u5efa\u8bae\uff0c\u81ea\u52a8\u8fd0\u884c\u8d85\u53c2\u6570\u8c03\u4f18<\/li>\n<\/ol>\n<p>\u590d\u5236<\/p>\n<p>def run_llm_based_model_selection_experiment(df, config): #Model Training X = df.drop(&#8220;is_fraud&#8221;, axis=1) y = df[&#8220;is_fraud&#8221;] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) available_models = config[&#8216;default_models&#8217;] model_performance = {} for model_name in available_models: print(f&#8221;Training model: {model_name}&#8221;) metrics, _ = train_and_evaluate(X_train, X_test, y_train, y_test, model_name) model_performance[model_name] = metrics print(f&#8221;Model: {model_name} | Metrics: {metrics}&#8221;) #LLM selecting the best model sklearn_version = sklearn.__version__ prompt = ( f&#8221;I have trained the following models with these metrics: {model_performance}. &#8221; &#8220;Which model should I select based on the best performance?&#8221; ) best_model_response = call_llm(prompt, config[&#8216;llm_api_key&#8217;]) print(f&#8221;LLM response for best model selection:\\n{best_model_response}&#8221;) best_model = extract_model_name(best_model_response, available_models) if not best_model: print(&#8220;Error: Could not extract a valid model name from LLM response.&#8221;) return print(f&#8221;LLM selected the best model: {best_model}&#8221;) #Check for hyperparameter tuning prompt_tuning = ( f&#8221;The selected model is {best_model}. Can you suggest hyperparameters for better performance? &#8221; &#8220;Please provide them in Python dictionary format, like {&#8216;max_depth&#8217;: 5, &#8216;min_samples_split&#8217;: 4}. &#8221; f&#8221;Ensure that all suggested hyperparameters are valid for scikit-learn version {sklearn_version}, &#8221; &#8220;and avoid using deprecated or invalid values such as &#8216;max_features&#8217;: &#8216;auto&#8217;. &#8221; &#8220;Don&#8217;t provide any explanation or return in any other format.&#8221; ) tuning_suggestion = call_llm(prompt_tuning, config[&#8216;llm_api_key&#8217;]) print(f&#8221;Hyperparameter tuning suggestion received:\\n{tuning_suggestion}&#8221;) cleaned_suggestion = clean_hyperparameter_suggestion(tuning_suggestion) if cleaned_suggestion is None: suggested_params = None else: try: suggested_params = ast.literal_eval(cleaned_suggestion) if not isinstance(suggested_params, dict): print(&#8220;Hyperparameter suggestion is not a valid dictionary.&#8221;) suggested_params = None except (ValueError, SyntaxError) as e: print(f&#8221;Error parsing hyperparameter suggestion: {e}&#8221;) suggested_params = None #Automatically run hyperparameter tuning if suggested if suggested_params: print(f&#8221;Running {best_model} with suggested hyperparameters: {suggested_params}&#8221;) tuned_metrics, _ = train_and_evaluate( X_train, X_test, y_train, y_test, best_model, hyperparameters=suggested_params ) print(f&#8221;Metrics after tuning: {tuned_metrics}&#8221;) else: print(&#8220;No valid hyperparameters were provided for tuning.&#8221;)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<li>45.<\/li>\n<li>46.<\/li>\n<li>47.<\/li>\n<li>48.<\/li>\n<li>49.<\/li>\n<li>50.<\/li>\n<li>51.<\/li>\n<li>52.<\/li>\n<li>53.<\/li>\n<li>54.<\/li>\n<li>55.<\/li>\n<li>56.<\/li>\n<li>57.<\/li>\n<li>58.<\/li>\n<li>59.<\/li>\n<li>60.<\/li>\n<li>61.<\/li>\n<li>62.<\/li>\n<li>63.<\/li>\n<\/ul>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u6307\u5b9a\u4e86LLM\u5982\u4f55\u6839\u636e\u8bd5\u9a8c\u8bc4\u4f30\u6211\u4eec\u7684\u6bcf\u4e2a\u6a21\u578b\u3002\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u63d0\u793a\u6839\u636e\u6a21\u578b\u7684\u6027\u80fd\u6765\u9009\u62e9\u8981\u4f7f\u7528\u7684\u6a21\u578b\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>prompt = ( f&#8221;I have trained the following models with these metrics: {model_performance}. &#8221; &#8220;Which model should I select based on the best performance?&#8221;)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<p>\u4f60\u59cb\u7ec8\u53ef\u4ee5\u66f4\u6539\u63d0\u793a\uff0c\u4ee5\u5b9e\u73b0\u6a21\u578b\u9009\u62e9\u7684\u4e0d\u540c\u89c4\u5219\u3002<\/p>\n<p>\u4e00\u65e6\u9009\u62e9\u4e86\u6700\u4f73\u6a21\u578b\uff0c\u6211\u5c06\u4f7f\u7528\u4ee5\u4e0b\u63d0\u793a\u6765\u5efa\u8bae\u5e94\u8be5\u4f7f\u7528\u54ea\u4e9b\u8d85\u53c2\u6570\u7528\u4e8e\u540e\u7eed\u8fc7\u7a0b\u3002\u6211\u8fd8\u6307\u5b9a\u4e86Scikit-Learn\u7248\u672c\uff0c\u56e0\u4e3a\u8d85\u53c2\u6570\u56e0\u7248\u672c\u7684\u4e0d\u540c\u800c\u6709\u53d8\u5316\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>prompt_tuning = ( f&#8221;The selected model is {best_model}. Can you suggest hyperparameters for better performance? &#8221; &#8220;Please provide them in Python dictionary format, like {&#8216;max_depth&#8217;: 5, &#8216;min_samples_split&#8217;: 4}. &#8221; f&#8221;Ensure that all suggested hyperparameters are valid for scikit-learn version {sklearn_version}, &#8221; &#8220;and avoid using deprecated or invalid values such as &#8216;max_features&#8217;: &#8216;auto&#8217;. &#8221; &#8220;Don&#8217;t provide any explanation or return in any other format.&#8221;)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u4ee5\u4efb\u4f55\u60f3\u8981\u7684\u65b9\u5f0f\u66f4\u6539\u63d0\u793a\uff0c\u6bd4\u5982\u901a\u8fc7\u66f4\u5927\u80c6\u5730\u5c1d\u8bd5\u8c03\u4f18\u8d85\u53c2\u6570\uff0c\u6216\u6dfb\u52a0\u53e6\u4e00\u79cd\u6280\u672f\u3002<\/p>\n<p>\u6211\u628a\u4e0a\u9762\u7684\u6240\u6709\u4ee3\u7801\u653e\u5728\u4e00\u4e2a\u540d\u4e3aautomated_model_llm.py\u7684\u6587\u4ef6\u4e2d\u3002\u6700\u540e\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u8fd0\u884c\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>def main(): config = load_config() df = load_data(config[&#8216;dataset_path&#8217;]) df, _ = preprocess_data(df) run_llm_based_model_selection_experiment(df, config) if __name__ == &#8220;__main__&#8221;: main()<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<\/ul>\n<p>\u4e00\u65e6\u4e00\u5207\u51c6\u5907\u5c31\u7eea\uff0c\u4f60\u5c31\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u6765\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>python automated_model_llm.py<\/p>\n<ul>\n<li>1.<\/li>\n<\/ul>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p>\u590d\u5236<\/p>\n<p>LLM selected the best model: RandomForestClassifier Hyperparameter tuning suggestion received: { &#8216;n_estimators&#8217;: 100, &#8216;max_depth&#8217;: None, &#8216;min_samples_split&#8217;: 2, &#8216;min_samples_leaf&#8217;: 1, &#8216;max_features&#8217;: &#8216;sqrt&#8217;, &#8216;bootstrap&#8217;: True } Running RandomForestClassifier with suggested hyperparameters: {&#8216;n_estimators&#8217;: 100, &#8216;max_depth&#8217;: None, &#8216;min_samples_split&#8217;: 2, &#8216;min_samples_leaf&#8217;: 1, &#8216;max_features&#8217;: &#8216;sqrt&#8217;, &#8216;bootstrap&#8217;: True} Metrics after tuning: {&#8216;accuracy&#8217;: 0.9730041532071989, &#8216;precision&#8217;: 0.9722907483489197, &#8216;recall&#8217;: 0.9730041532071989, &#8216;f1_score&#8217;: 0.9724045530119824}<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<\/ul>\n<p>\u8fd9\u662f\u6211\u8bd5\u9a8c\u5f97\u5230\u7684\u793a\u4f8b\u8f93\u51fa\u3002\u5b83\u53ef\u80fd\u548c\u4f60\u7684\u4e0d\u4e00\u6837\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u63d0\u793a\u548c\u751f\u6210\u53c2\u6570\uff0c\u4ee5\u83b7\u5f97\u66f4\u52a0\u591a\u53d8\u6216\u4e25\u683c\u7684LLM\u8f93\u51fa\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u6b63\u786e\u6784\u5efa\u4e86\u4ee3\u7801\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c06LLM\u8fd0\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u81ea\u52a8\u5316\u3002<\/p>\n<h4>\u7ed3\u8bba<\/h4>\n<p>LLM\u5df2\u7ecf\u5e94\u7528\u4e8e\u8bb8\u591a\u4f7f\u7528\u573a\u666f\uff0c\u5305\u62ec\u4ee3\u7801\u751f\u6210\u3002\u901a\u8fc7\u8fd0\u7528LLM\uff08\u6bd4\u5982OpenAI GPT\u6a21\u578b\uff09\uff0c\u6211\u4eec\u5c31\u5f88\u5bb9\u6613\u59d4\u6d3eLLM\u5904\u7406\u6a21\u578b\u9009\u62e9\u548c\u8bd5\u9a8c\u8fd9\u9879\u4efb\u52a1\uff0c\u53ea\u8981\u6211\u4eec\u6b63\u786e\u5730\u6784\u5efa\u8f93\u51fa\u7684\u7ed3\u6784\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6837\u672c\u6570\u636e\u96c6\u5bf9\u6a21\u578b\u8fdb\u884c\u8bd5\u9a8c\uff0c\u8ba9LLM\u9009\u62e9\u548c\u8bd5\u9a8c\u4ee5\u6539\u8fdb\u6a21\u578b\u3002<\/p>\n<p>\u6587\u7ae0\u6765\u81ea\uff1a51CTO<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_22950\" class=\"pvc_stats total_only  \" data-element-id=\"22950\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u751f\u6210\u548c\u8bc4\u4f30\u6700\u8fd1\u5df2\u7ecf\u6210\u4e3a\u8bb8\u591a\u5546\u4e1a\u4ea7\u54c1\u63d0\u4f9b\u7684\u91cd\u8981\u529f\u80fd\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5904\u7406\u4ee3\u7801\u3002LLM\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7528\u4e8e\u5904\u7406\u6570\u636e\u79d1 [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_22950\" class=\"pvc_stats total_only  \" data-element-id=\"22950\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":22952,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,20,80],"tags":[986,968,1415],"class_list":["post-22950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-23","category-20","category-80","tag-llm","tag-968","tag-1415"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/22950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=22950"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/22950\/revisions"}],"predecessor-version":[{"id":22953,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/22950\/revisions\/22953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/22952"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=22950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=22950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=22950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}