作者:Ahmed Gad 翻譯:張睿毅 校對(duì):丁楠雅 本文4700字,建議閱讀15分鐘。 本教程主要使用numpy和sklearn來(lái)討論如何使用遺傳算法(genetic algorithm,GA)來(lái)減少?gòu)膒ython中的Fruits360數(shù)據(jù)集提取的特征向量。 標(biāo)簽:深度學(xué)習(xí),特征工程,遺傳算法,神經(jīng)網(wǎng)絡(luò),numpy,python,scikit-learn 本教程主要使用numpy和sklearn來(lái)討論如何使用遺傳算法(genetic algorithm,GA)來(lái)減少?gòu)膒ython中的Fruits360數(shù)據(jù)集提取的特征向量。 ![]() 導(dǎo)言在某些情況下,使用原始數(shù)據(jù)訓(xùn)練機(jī)器學(xué)習(xí)算法可能不是合適的選擇。該算法在接受原始數(shù)據(jù)訓(xùn)練時(shí),必須進(jìn)行特征挖掘,以檢測(cè)不同組之間的差異。但這需要大量的數(shù)據(jù)來(lái)自動(dòng)執(zhí)行特征挖掘。對(duì)于小數(shù)據(jù)集,數(shù)據(jù)科學(xué)家最好自己進(jìn)行特征挖掘步驟,之后告訴機(jī)器學(xué)習(xí)算法要使用哪個(gè)特征集。 使用的特征集必須能代表數(shù)據(jù)樣本,因此我們必須注意選擇最佳特征。數(shù)據(jù)科學(xué)家建議使用一些類型的特征,這些特征似乎有助于根據(jù)以前的經(jīng)驗(yàn)來(lái)表示數(shù)據(jù)樣本。一些特征可以證明它們?cè)诒硎緲颖緯r(shí)的穩(wěn)健性,而其他特征則不能。 可能存在一些類型的特征,會(huì)降低分類問(wèn)題的準(zhǔn)確性或增加回歸問(wèn)題的誤差,進(jìn)而影響訓(xùn)練模型的結(jié)果。例如,特征向量中可能存在一些噪音元素,因此它們應(yīng)該被刪除。特征向量也可能包含2個(gè)或更多相關(guān)元素。只使用一個(gè)元素就可以替代另一個(gè)元素。為了刪除這些類型的元素,有兩個(gè)有用的步驟,即特征選擇和約簡(jiǎn)。本教程重點(diǎn)介紹特征約簡(jiǎn)。 假設(shè)有3個(gè)特征f1、f2和f3,每個(gè)特征都有3個(gè)特征元素。因此,特征向量長(zhǎng)度為3x3=9。特征選擇只選擇特定類型的特征,不包括其他類型的特征。例如,只需選擇f1和f3并刪除f3。特征向量長(zhǎng)度變成了6而不是9。在特征約簡(jiǎn)中,可以排除每個(gè)特征的特定元素。例如,此步驟可能會(huì)在保留第二個(gè)元素的同時(shí)從f3中刪除第一個(gè)和第三個(gè)元素。因此,特征向量長(zhǎng)度從9減少到7。 在開始本教程之前,值得一提的是,它是我的LinkedIn配置文件中先前發(fā)布的2個(gè)教程的擴(kuò)展。 第一個(gè)教程的標(biāo)題是“使用numpy的人工神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)Fruits360圖像數(shù)據(jù)集的分類”。它首先從Fruits360數(shù)據(jù)集的4個(gè)類中提取長(zhǎng)度為360的特征向量。然后,利用numpy從零開始構(gòu)建人工神經(jīng)網(wǎng)絡(luò)(ANN),對(duì)數(shù)據(jù)集進(jìn)行分類。 第一個(gè)教程可從以下網(wǎng)址獲?。?/strong>https://www.linkedin.com/pulse/artificial-neural-network-implementation-using-numpy-fruits360-gad其Github項(xiàng)目可從以下網(wǎng)址獲得:https://github.com/ahmedfgad/NumPyAN 第二個(gè)教程是“使用遺傳算法的人工神經(jīng)網(wǎng)絡(luò)優(yōu)化”。建立并使用遺傳算法對(duì)神經(jīng)網(wǎng)絡(luò)參數(shù)進(jìn)行優(yōu)化,以提高分類精度。 第二個(gè)教程可從以下網(wǎng)址獲?。?/strong>https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad。其Github項(xiàng)目也可從以下網(wǎng)址獲得:https://github.com/ahmedfgad/NeuralGeneti 本教程討論了如何使用遺傳算法來(lái)減少?gòu)拈L(zhǎng)度360的Fruits360數(shù)據(jù)集中提取的特征向量。本教程首先討論要遵循的步驟。其次通過(guò)使用NumPy和Sklearn在python實(shí)現(xiàn)這些步驟。 本教程的實(shí)現(xiàn)可在我的Github頁(yè)面中找到:https://github.com/ahmedfgad/FeatureReductionGeneti 遺傳算法從一個(gè)初始群體開始,該群體由若干染色體(即解決方法)組成,其中每個(gè)染色體都有一系列基因。使用適應(yīng)函數(shù),遺傳算法選擇最佳的解決方案作為父母來(lái)創(chuàng)建一個(gè)新的群體。在這樣一個(gè)新的群體中,通過(guò)在雙親上應(yīng)用兩個(gè)操作,即雜交和變異來(lái)創(chuàng)建新的解決方案。當(dāng)把遺傳算法應(yīng)用到一個(gè)給定的問(wèn)題上時(shí),我們必須確定基因的表示、合適的適應(yīng)函數(shù)以及雜交和變異是如何應(yīng)用的。接下來(lái)讓我們看看運(yùn)行原理。 更多關(guān)于遺傳算法的信息你可以從我準(zhǔn)備的如下資源中讀到關(guān)于遺傳算法的更多知識(shí): 1. 遺傳算法優(yōu)化介紹 https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad/https://www./2018/03/introduction-optimization-with-genetic-algorithm.htmlhttps:///introduction-to-optimization-with-genetic-algorithm-2f5001d9964b 2. 遺傳算法優(yōu)化-逐步示例 https://www./AhmedGadFCIT/genetic-algorithm-ga-optimization-stepbystep-example 3. python中的遺傳算法實(shí)現(xiàn) https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad/https://www./2018/07/genetic-algorithm-implementation-python.htmlhttps:///genetic-algorithm-implementation-in-python-5ab67bb124a6https://github.com/ahmedfgad/GeneticAlgorithmPython 我在2018年還寫了一本書,其中一章介紹了遺傳算法。這本書的標(biāo)題是“利用CNN進(jìn)行深度學(xué)習(xí)的實(shí)用計(jì)算機(jī)視覺(jué)應(yīng)用”,可在Springer上找到。 Springer鏈接:https://www./us/book/978148424166 染色體的表達(dá)遺傳算法中的基因是染色體的組成部分。首先,我們需要確定染色體內(nèi)的基因。為此,考慮到可能影響結(jié)果的每一種屬性都應(yīng)被視為一個(gè)基因。因?yàn)槲覀儐?wèn)題的目標(biāo)是選擇最好的一組特征元素,所以如果選擇或不選擇,每個(gè)特征元素都可能影響結(jié)果。因此,每個(gè)特征元素都被視為一個(gè)基因。染色體將由所有基因(即所有特征元素)組成。因?yàn)橛?60個(gè)特征元素,那么就有360個(gè)基因。一個(gè)很好的信息現(xiàn)在很清楚,染色體的長(zhǎng)度是360。 在確定所選基因是什么之后,下一步就是確定基因的表達(dá)。有不同的表示形式,如十進(jìn)制、二進(jìn)制、浮點(diǎn)、字符串等。我們的目標(biāo)是知道基因(即特征元素)是否在減少的特征集中被選擇。因此,分配給基因的值應(yīng)該反映它是否被選擇?;谶@種描述,很明顯每個(gè)基因有兩個(gè)可能的值。一個(gè)值表示該基因已被選中,另一個(gè)值表示未被選中。因此,二進(jìn)制表示是最佳選擇。當(dāng)基因值為1時(shí),將在減少的特征集中進(jìn)行選擇。當(dāng)為0時(shí),則忽略它。 總之,染色體將由360個(gè)基因組成,以二進(jìn)制表示。根據(jù)下一個(gè)圖,特征向量和染色體之間有一對(duì)一的映射。這是染色體中的第一個(gè)基因與特征向量中的第一個(gè)元素相連。當(dāng)該基因的值為1時(shí),這意味著選擇了特征向量中的第一個(gè)元素。 ![]() 適應(yīng)函數(shù)通過(guò)了解如何創(chuàng)建染色體,可以很容易地對(duì)初始種群進(jìn)行隨機(jī)初始化。初始化后,將選擇父級(jí)。遺傳算法基于達(dá)爾文的“適者生存”理論。這是目前選擇的最佳解決方案進(jìn)行組合,以產(chǎn)生更好的解決方案。通過(guò)保留好的解和消除壞的解,我們可以得到最優(yōu)或半最優(yōu)解。 選擇雙親的標(biāo)準(zhǔn)是與每個(gè)解決方案(即染色體)相關(guān)聯(lián)的適應(yīng)值。適合度越高,解決方案越好。使用適應(yīng)度函數(shù)計(jì)算適應(yīng)度值。那么,在我們的問(wèn)題中,最適合使用的功能是什么?我們問(wèn)題的目標(biāo)是創(chuàng)建一個(gè)約簡(jiǎn)的特征向量,以提高分類精度。因此,判斷一個(gè)解是否好的標(biāo)準(zhǔn)是分類的準(zhǔn)確性。因此,fitness函數(shù)將返回一個(gè)數(shù)字,指定每個(gè)解決方案的分類精度。精度越高,解決方案越好。 為了返回分類的準(zhǔn)確度,必須有一個(gè)機(jī)器學(xué)習(xí)模型來(lái)通過(guò)每個(gè)解決方案返回的特征元素進(jìn)行訓(xùn)練。對(duì)于這種情況,我們將使用支持向量分類器(SVC)。 數(shù)據(jù)集分為訓(xùn)練樣本和測(cè)試樣本。根據(jù)訓(xùn)練數(shù)據(jù),SVC將使用人群中每個(gè)解決方案選擇的特征元素進(jìn)行訓(xùn)練。經(jīng)過(guò)訓(xùn)練后,根據(jù)測(cè)試數(shù)據(jù)進(jìn)行測(cè)試。 根據(jù)每個(gè)解的適合度值,我們可以選擇其中最好的作為父母。這些父母被放在一起組合以產(chǎn)生后代,這將是下一代的新人口的成員。這種后代是通過(guò)對(duì)選定的親本應(yīng)用交叉和突變操作而產(chǎn)生的。讓我們按照下面討論的方式配置這些操作。 遺傳和變異基于適應(yīng)度函數(shù),我們可以篩選出當(dāng)前群體中的最優(yōu)解,即父輩。遺傳算法假設(shè)匹配2個(gè)好的解決方案將產(chǎn)生第三個(gè)更好的解決方案。組合意味著從兩個(gè)父母那里交換一些基因。使用遺傳操作交換基因。有不同的方法可以應(yīng)用這種操作。本教程使用單點(diǎn)交叉,其中一個(gè)點(diǎn)分割染色體。點(diǎn)前的基因取自一組解,點(diǎn)后的基因取自另一組解。 通過(guò)應(yīng)用遺傳,所有的基因都來(lái)自于以前的父母。在新的后代中沒(méi)有引入新的基因。如果所有的父母都有一個(gè)壞基因,那么這個(gè)基因就會(huì)轉(zhuǎn)移到后代身上。正因?yàn)槿绱耍瑸榱嗽诤蟠幸胄碌幕?,采用了突變操作。在基因的二元表示中,突變是通過(guò)翻轉(zhuǎn)一些隨機(jī)選擇的基因的值來(lái)實(shí)現(xiàn)的。如果基因值為1,則為0,反之亦然。 在產(chǎn)生后代之后,我們可以創(chuàng)造下一代的新種群。除了后代之外,這個(gè)群體還包括以前的父輩。 此時(shí),將討論所有步驟。接下來(lái)是用Python實(shí)現(xiàn)它們。注意,我以前寫過(guò)一篇題為“Python中的遺傳算法實(shí)現(xiàn)”的教程,用于在Python中實(shí)現(xiàn)遺傳算法,我將修改它的代碼來(lái)解決我們的問(wèn)題。最好讀一下。 利用Python實(shí)現(xiàn)該項(xiàng)目分為兩個(gè)文件。一個(gè)文件名為GA.py,它將遺傳算法步驟的實(shí)現(xiàn)保存為函數(shù)。另一個(gè)文件是主文件,它只導(dǎo)入這個(gè)文件,并在循環(huán)中調(diào)用它的函數(shù),該循環(huán)將迭代幾代。 根據(jù)下面的代碼,主文件首先讀取從Fruits360數(shù)據(jù)集提取的特性。這些特性返回到數(shù)據(jù)輸入變量中。有關(guān)提取這些功能的詳細(xì)信息,請(qǐng)參閱本教程開頭提到的2個(gè)教程。該文件還讀取與數(shù)據(jù)輸出變量中的樣本相關(guān)聯(lián)的類標(biāo)簽。 選擇一些樣本進(jìn)行訓(xùn)練,其索引存儲(chǔ)在train_indices變量中。同樣,測(cè)試樣本索引存儲(chǔ)在test_indices變量中。 import numpyimport GAimport pickleimport matplotlib.pyplotf = open('dataset_features.pkl', 'rb')data_inputs = pickle.load(f)f.close()f = open('outputs.pkl', 'rb')data_outputs = pickle.load(f)f.close()num_samples = data_inputs.shape[0]num_feature_elements = data_inputs.shape[1]train_indices = numpy.arange(1, num_samples, 4)test_indices = numpy.arange(0, num_samples, 4)print('Number of training samples: ', train_indices.shape[0])print('Number of test samples: ', test_indices.shape[0])'''Genetic algorithm parameters: Population size Mating pool size Number of mutations'''sol_per_pop = 8 # Population size.num_parents_mating = 4 # Number of parents inside the mating pool.num_mutations = 3 # Number of elements to mutate.# Defining the population shape.pop_shape = (sol_per_pop, num_feature_elements)# Creating the initial population.new_population = numpy.random.randint(low=0, high=2, size=pop_shape)print(new_population.shape)best_outputs = []num_generations = 100 它初始化了遺傳算法的所有參數(shù)。這包括根據(jù)sol_per_pop變量設(shè)置為8的每個(gè)群體的解的數(shù)量、num _parents_mating變量設(shè)置為4的子代數(shù)量以及num_mutations變量設(shè)置為3的突變數(shù)量。之后,它會(huì)在一個(gè)名為“new_population”的變量中隨機(jī)創(chuàng)建初始總體。 有一個(gè)名為best_outputs的空列表,它在每一代之后都保存著最好的結(jié)果。這有助于可視化遺傳算法在完成所有代之后的進(jìn)展。num_generations變量中的代數(shù)設(shè)置為100。請(qǐng)注意,您可以更改所有這些參數(shù),從而獲得更好的結(jié)果。 在準(zhǔn)備好特性、類標(biāo)簽和算法參數(shù)之后,我們可以根據(jù)下一個(gè)代碼對(duì)算法進(jìn)行迭代。首先,通過(guò)調(diào)用GA文件中定義的名為cal_pop_fitness()的適應(yīng)函數(shù)來(lái)計(jì)算所有解決方案的適應(yīng)值。此函數(shù)接受當(dāng)前總體、提取的特征、類標(biāo)簽、列車索引和測(cè)試索引。函數(shù)返回名為fitness的變量中所有解的適應(yīng)值。請(qǐng)記住,適合度值表示分類精度。最佳(即最高)分類精度保存在最佳輸出列表中。 根據(jù)計(jì)算出的適合度值,使用GA.py文件中定義的select_matching_pool()函數(shù)選擇分類精度最高的最佳解決方案作為匹配池中的父級(jí)。它接受當(dāng)前的人口、適合度值和要返回的父母人數(shù)。它將所選雙親返回到父級(jí)變量中。 for generation in range(num_generations): print('Generation : ', generation) # Measuring the fitness of each chromosome in the population. fitness = GA.cal_pop_fitness(new_population, data_inputs, data_outputs, train_indices, test_indices) best_outputs.append(numpy.max(fitness)) # The best result in the current iteration. print('Best result : ', best_outputs[-1]) # Selecting the best parents in the population for mating parents = GA.select_mating_pool(new_population, fitness, num_parents_mating) # Generating next generation using crossover offspring_crossover = GA.crossover(parents, offspring_size=(pop_shape[0]-parents.shape[0], num_feature_elements)) # Adding some variations to the offspring using mutation. offspring_mutation = GA.mutation(offspring_crossover, num_mutations=num_mutations) # Creating the new population based on the parents and offspring. new_population[0:parents.shape[0], :] = parents new_population[parents.shape[0]:, :] = offspring_mutation 接下來(lái)是對(duì)選定的父代應(yīng)用組合操作以創(chuàng)建子代。這是在GA.py文件中定義的crossover()函數(shù)內(nèi)完成的。它接受父數(shù)組和子數(shù)組的形狀,以便稍后返回到offspring_crossover變量中。然后,使用在GA.py文件中也可用的mutation()函數(shù)在該數(shù)組上應(yīng)用突變操作。除了交叉結(jié)果,這個(gè)函數(shù)接受突變的數(shù)量。 因?yàn)樾碌姆N群由選定的親本和后代組成,所以親本和offspring_crossover數(shù)組都保存到new_population變量中。在那之后,新一代被應(yīng)用于新的人口。 在所有代完成后,將執(zhí)行下一個(gè)代碼,以返回最佳選擇的功能元素集和所選元素的數(shù)量。在100代完成后,該算法使用174個(gè)特征元素,以達(dá)到99.59%的精度。 fitness = GA.cal_pop_fitness(new_population, data_inputs, data_outputs, train_indices, test_indices)# Then return the index of that solution corresponding to the best fitness.best_match_idx = numpy.where(fitness == numpy.max(fitness))[0]best_match_idx = best_match_idx[0]best_solution = new_population[best_match_idx, :]best_solution_indices = numpy.where(best_solution == 1)[0]best_solution_num_elements = best_solution_indices.shape[0]best_solution_fitness = fitness[best_match_idx]print('best_match_idx : ', best_match_idx)print('best_solution : ', best_solution)print('Selected indices : ', best_solution_indices)print('Number of selected elements : ', best_solution_num_elements)print('Best solution fitness : ', best_solution_fitness)matplotlib.pyplot.plot(best_outputs)matplotlib.pyplot.xlabel('Iteration')matplotlib.pyplot.ylabel('Fitness')matplotlib.pyplot.show() 上面的代碼展示了一張圖,顯示了算法在所有代中的進(jìn)度,如下所示。 以下是主文件中的完整代碼。 import numpyimport GAimport pickleimport matplotlib.pyplotf = open('dataset_features.pkl', 'rb')data_inputs = pickle.load(f)f.close()f = open('outputs.pkl', 'rb')data_outputs = pickle.load(f)f.close()num_samples = data_inputs.shape[0]num_feature_elements = data_inputs.shape[1]train_indices = numpy.arange(1, num_samples, 4)test_indices = numpy.arange(0, num_samples, 4)print('Number of training samples: ', train_indices.shape[0])print('Number of test samples: ', test_indices.shape[0])'''Genetic algorithm parameters: Population size Mating pool size Number of mutations'''sol_per_pop = 8 # Population sizenum_parents_mating = 4 # Number of parents inside the mating pool.num_mutations = 3 # Number of elements to mutate.# Defining the population shape.pop_shape = (sol_per_pop, num_feature_elements)# Creating the initial population.new_population = numpy.random.randint(low=0, high=2, size=pop_shape)print(new_population.shape)best_outputs = []num_generations = 100for generation in range(num_generations): print('Generation : ', generation) # Measuring the fitness of each chromosome in the population. fitness = GA.cal_pop_fitness(new_population, data_inputs, data_outputs, train_indices, test_indices) best_outputs.append(numpy.max(fitness)) # The best result in the current iteration. print('Best result : ', best_outputs[-1]) # Selecting the best parents in the population for mating. parents = GA.select_mating_pool(new_population, fitness, num_parents_mating) # Generating next generation using crossover. offspring_crossover = GA.crossover(parents, offspring_size=(pop_shape[0]-parents.shape[0], num_feature_elements)) # Adding some variations to the offspring using mutation. offspring_mutation = GA.mutation(offspring_crossover, num_mutations=num_mutations) # Creating the new population based on the parents and offspring. new_population[0:parents.shape[0], :] = parents new_population[parents.shape[0]:, :] = offspring_mutation # Getting the best solution after iterating finishing all generations.# At first, the fitness is calculated for each solution in the final generation.fitness = GA.cal_pop_fitness(new_population, data_inputs, data_outputs, train_indices, test_indices)# Then return the index of that solution corresponding to the best fitness.best_match_idx = numpy.where(fitness == numpy.max(fitness))[0]best_match_idx = best_match_idx[0]best_solution = new_population[best_match_idx, :]best_solution_indices = numpy.where(best_solution == 1)[0]best_solution_num_elements = best_solution_indices.shape[0]best_solution_fitness = fitness[best_match_idx]print('best_match_idx : ', best_match_idx)print('best_solution : ', best_solution)print('Selected indices : ', best_solution_indices)print('Number of selected elements : ', best_solution_num_elements)print('Best solution fitness : ', best_solution_fitness)matplotlib.pyplot.plot(best_outputs)matplotlib.pyplot.xlabel('Iteration')matplotlib.pyplot.ylabel('Fitness')matplotlib.pyplot.show() GA.py的實(shí)現(xiàn)GA.py文件的實(shí)現(xiàn)如下所示。在cal_pop_fitness()函數(shù)中,SVC根據(jù)每個(gè)解決方案選擇的特征元素進(jìn)行培訓(xùn)。在訓(xùn)練前,根據(jù)所選的基因值為1的元素過(guò)濾特征。這是在reduce_features()函數(shù)中完成的。除了所有示例的完整功能外,它還接受當(dāng)前的解決方案。 訓(xùn)練后,使用reduce_features()函數(shù)計(jì)算分類精度。此函數(shù)返回存儲(chǔ)在cal pop_fitness()函數(shù)中名為accuracies的數(shù)組中的精度。 crossover()和mutation()函數(shù)的實(shí)現(xiàn)與我之前的教程“Python中的遺傳算法實(shí)現(xiàn)”中討論的非常相似。一個(gè)主要的區(qū)別是,mutation()函數(shù)通過(guò)翻轉(zhuǎn)隨機(jī)選擇的基因的值來(lái)改變它們,因?yàn)槲覀兪褂玫氖嵌M(jìn)制表示。 import numpyimport sklearn.svmdef reduce_features(solution, features): selected_elements_indices = numpy.where(solution == 1)[0] reduced_features = features[:, selected_elements_indices] return reduced_featuresdef classification_accuracy(labels, predictions): correct = numpy.where(labels == predictions)[0] accuracy = correct.shape[0]/labels.shape[0] return accuracydef cal_pop_fitness(pop, features, labels, train_indices, test_indices): accuracies = numpy.zeros(pop.shape[0]) idx = 0 for curr_solution in pop: reduced_features = reduce_features(curr_solution, features) train_data = reduced_features[train_indices, :] test_data = reduced_features[test_indices, :] train_labels = labels[train_indices] test_labels = labels[test_indices] SV_classifier = sklearn.svm.SVC(gamma='scale') SV_classifier.fit(X=train_data, y=train_labels) predictions = SV_classifier.predict(test_data) accuracies[idx] = classification_accuracy(test_labels, predictions) idx = idx + 1 return accuraciesdef select_mating_pool(pop, fitness, num_parents): # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation. parents = numpy.empty((num_parents, pop.shape[1])) for parent_num in range(num_parents): max_fitness_idx = numpy.where(fitness == numpy.max(fitness)) max_fitness_idx = max_fitness_idx[0][0] parents[parent_num, :] = pop[max_fitness_idx, :] fitness[max_fitness_idx] = -99999999999 return parentsdef crossover(parents, offspring_size): offspring = numpy.empty(offspring_size) # The point at which crossover takes place between two parents. Usually, it is at the center. crossover_point = numpy.uint8(offspring_size[1]/2) for k in range(offspring_size[0]): # Index of the first parent to mate. parent1_idx = k%parents.shape[0] # Index of the second parent to mate. parent2_idx = (k+1)%parents.shape[0] # The new offspring will have its first half of its genes taken from the first parent. offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point] # The new offspring will have its second half of its genes taken from the second parent. offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:] return offspringdef mutation(offspring_crossover, num_mutations=2): mutation_idx = numpy.random.randint(low=0, high=offspring_crossover.shape[1], size=num_mutations) # Mutation changes a single gene in each offspring randomly. for idx in range(offspring_crossover.shape[0]): # The random value to be added to the gene. offspring_crossover[idx, mutation_idx] = 1 - offspring_crossover[idx, mutation_idx] return offspring_crossover 原文標(biāo)題: Feature Reduction using Genetic Algorithm with Python 原文鏈接: https://www./2019/03/feature-reduction-genetic-algorithm-python.html 譯者簡(jiǎn)介 ![]() 張睿毅,北京郵電大學(xué)大二物聯(lián)網(wǎng)在讀。我是一個(gè)愛(ài)自由的人。在郵電大學(xué)讀第一年書我就四處跑去蹭課,折騰整一年驚覺(jué),與其在當(dāng)下焦慮,不如在前輩中沉淀。于是在大二以來(lái),堅(jiān)持讀書,不敢稍歇。資本主義國(guó)家的科學(xué)觀不斷刷新我的認(rèn)知框架,同時(shí)因?yàn)槌鰢?guó)考試很早出分,也更早地感受到自己才是那個(gè)一直被束縛著的人。太多真英雄在社會(huì)上各自閃耀著光芒。這才開始,立志終身向遇到的每一個(gè)人學(xué)習(xí)。做一個(gè)純粹的計(jì)算機(jī)科學(xué)里面的小學(xué)生。 |
|
來(lái)自: flyk0tcfb46p9f > 《AI》