黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

模擬退火算法之特征選擇的python實現(xiàn)(二)

系統(tǒng) 2036 0

目錄

1.?模擬退火算法之特征選擇的python實現(xiàn)(類封裝)

2. 實驗結(jié)果

按照模擬退火算法基本流程的python實現(xiàn),可以參考模擬退火算法之特征選擇的python實現(xiàn)(一)

特此申明:代碼是作者辛辛苦苦碼的, 轉(zhuǎn)載請注明出處

1.?模擬退火算法之特征選擇的python實現(xiàn)(類封裝)

            
              import numpy as np
from sklearn.metrics import mean_squared_error
import numpy as np
from sklearn.neural_network import MLPRegressor
import scipy.io as sio
from sklearn.model_selection import train_test_split


class SimulatedAnnealing(object):
    """Feature selection with simulated annealing algorithm.

    parameters
    ----------
    initT: int or float, default: 100
        The maximum temperature
    minT: int or float, default: 1
        The minimum temperature
    alpha:float, default:0.98
        Decay coefficient of temperature
    iteration: int, default:50
        Balance times at present temperature
    features: int
        The number of attributes in the original data
    init_features_sel: int
        The index of selected fatures
    estimator: object
        A supervised learning estimator with a `fit` method.

    Attributes
    ----------
    temp_history: array
        record the temperatures
    best_cost_history: array
        record the MSEs
    best_solution_history: array
        record the solutions
    """

    def __init__(self, features, init_features_sel, estimator, initT=100, minT=1, alpha=0.98, iteration=50):

        self.initT = initT
        self.minT = minT
        self.alpha = alpha
        self.iteration = iteration
        self.feature_size = features
        self.init_feature_sel = init_features_sel
        self.estimator = estimator

    def get_initial_solution(self):
        sol = np.arange(self.feature_size - 1)
        np.random.shuffle(sol)
        return sol[:self.init_feature_sel]

    def get_cost(self, solution, x_train, x_test, y_train, y_test):
        """ compute the evaluated results of current solution

        :param solution: array of shape (selected, )
        :param x_train: array of shape (n_samples, n_features)
        :param x_test: array of shape (n_samples, n_features)
        :param y_train: array of shape (n_samples, )
        :param y_test: array of shape (n_samples, n_features)
        :return: mse
        """
        limited_train_data = self.get_data_subset(x_train, solution)
        limited_test_data = self.get_data_subset(x_test, solution)
        estimator = self.estimator.fit(limited_train_data, y_train)
        y_test_pred = estimator.predict(limited_test_data)
        return round(mean_squared_error(y_test, y_test_pred), 4)

    @staticmethod
    def get_data_subset(x_data, soln):
        return x_data[:, soln]

    def get_neighbor(self, current_solution, temperature):
        """

        :param current_solution: array of shape (selected, )
        :param temperature: int or float.
        :return: selected :the index of selected features, array of shape (selected, ).
        """
        all_features = range(self.feature_size-1)
        selected = current_solution
        not_selected = np.setdiff1d(all_features, selected)

        # swap one selected feature with one non-selected feature
        num_swaps = int(
            min(np.ceil(np.abs(np.random.normal(0, 0.1 * len(selected) * temperature))), np.ceil(0.1 * len(selected))))
        feature_out = np.random.randint(0, len(selected), num_swaps)  # 產(chǎn)生num_swaps個樣本索引(從range(len(selected))中)
        selected = np.delete(selected, feature_out)
        feature_in = np.random.randint(0, len(not_selected), num_swaps)  # 產(chǎn)生num_swaps個樣本索引(從range(len(not_selected))中)
        selected = np.append(selected, not_selected[feature_in])
        return selected

    @staticmethod
    def get_probability(temperature, delta_cost):
        return np.exp(delta_cost/temperature)

    def fit(self, x_train, x_test, y_train, y_test):
        """

        :param x_train: array of shape (n_samples, n_features)
        :param x_test: array of shape (n_samples, n_features)
        :param y_train: array of shape (n_samples, )
        :param y_test: array of shape (n_samples, )
        :return:
        best_solution: the index of final selected attributes, array of shape (selected, )
        best_cost : minimum mse
        """
        temperature = self.initT  # 當(dāng)前溫度
        solution = self.get_initial_solution()
        cost = self.get_cost(solution, x_train, x_test, y_train, y_test)

        temp_history = [temperature]
        best_cost_history = []
        best_solution_history = []

        best_cost = cost
        best_solution = solution

        while temperature > self.minT:
            for k in range(self.iteration):
                next_solution = self.get_neighbor(solution, temperature)
                next_cost = self.get_cost(next_solution, x_train, x_test, y_train, y_test)

                probability = 0
                if next_cost > cost:  # 計算向差方向移動的概率 (即移動后的解比當(dāng)前解要差)
                    probability = self.get_probability(temperature, cost-next_cost)
                if next_cost < cost or np.random.random() < probability:  # 朝著最優(yōu)解移動或以一定概率向差方向移動
                    cost = next_cost
                    solution = next_solution
                if next_cost < best_cost:  # 最優(yōu)值和最優(yōu)解
                    best_cost = cost
                    best_solution = solution

            print("當(dāng)前溫度:", round(temperature, 2))
            print("當(dāng)前溫度下最好的得分:", best_cost)
            print("當(dāng)前溫度下波長數(shù)量:", len(solution))

            temperature *= self.alpha
            temp_history.append(temperature)
            best_cost_history.append(best_cost)
            best_solution_history.append(best_solution)

        self.temp_history_ = temp_history
        self.best_cost_history_ = best_cost_history
        self.best_solution_history = best_solution_history
        return best_solution, best_cost


# 1.數(shù)據(jù)獲取
mat = sio.loadmat('NDFNDF_smote.mat')
data = mat['NDFNDF_smote']
x, y = data[:, :1050], data[:, 1050]
print('原始數(shù)據(jù)大?。?, x.shape, y.shape)

# 2.樣本集劃分和預(yù)處理
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)
print("訓(xùn)練集;", x_train.shape, y_train.shape)
print("測試集:", x_test.shape, y_test.shape)

# 3.使用模擬退火算法優(yōu)化特征數(shù)量
feature_size = x.shape[1]
sel_feature = 10
estimator = MLPRegressor(hidden_layer_sizes=43)
sa = SimulatedAnnealing(initT=100,
                        minT=1,
                        alpha=0.95,
                        iteration=50,
                        features=feature_size,
                        init_features_sel=sel_feature,
                        estimator=estimator)
sa.fit(x_train, x_test, y_train, y_test)

            
          

2. 實驗結(jié)果

模擬退火算法之特征選擇的python實現(xiàn)(二)_第1張圖片

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論