一文读懂遗传算法工作原理（附Python实现）

1、遗传算法理论的由来

2、生物学的启发

3、遗传算法定义

4、遗传算法具体步骤

• 初始化
• 适应度函数
• 选择
• 交叉
• 变异

5、遗传算法的应用

• 特征选取
• 使用 TPOT 库实现

6、实际应用

7、结语

1、遗传算法理论的由来

2、生物学的启发

3、遗传算法定义

4、遗传算法具体步骤

4.1 初始化

4.2 适应度函数

4.3 选择

4.4 交叉

4.5 变异

5、遗传算法的应用

5.1 特征选取

5.2 用 TPOT 库来实现

# installing DEAP, update_checker and tqdm

pip install deap update_checker tqdm

# installling TPOT

pip install tpot

# import basic libraries

importnumpy asnp importpandas aspd importmatplotlib.pyplot asplt %matplotlib inline fromsklearn importpreprocessing fromsklearn.metrics importmean_squared_error ## preprocessing

### mean imputations

train[ ‘Item_Weight’].fillna((train[ ‘Item_Weight’].mean()), inplace= True)test[ ‘Item_Weight’].fillna((test[ ‘Item_Weight’].mean()), inplace= True)

### reducing fat content to only two categories

train[ ‘Item_Fat_Content’] = train[ ‘Item_Fat_Content’].replace([ ‘low fat’, ‘LF’], [ ‘Low Fat’, ‘Low Fat’]) train[ ‘Item_Fat_Content’] = train[ ‘Item_Fat_Content’].replace([ ‘reg’], [ ‘Regular’]) test[ ‘Item_Fat_Content’] = test[ ‘Item_Fat_Content’].replace([ ‘low fat’, ‘LF’], [ ‘Low Fat’, ‘Low Fat’]) test[ ‘Item_Fat_Content’] = test[ ‘Item_Fat_Content’].replace([ ‘reg’], [ ‘Regular’]) train[ ‘Outlet_Establishment_Year’] = 2013- train[ ‘Outlet_Establishment_Year’] test[ ‘Outlet_Establishment_Year’] = 2013- test[ ‘Outlet_Establishment_Year’] train[ ‘Outlet_Size’].fillna( ‘Small’,inplace= True)test[ ‘Outlet_Size’].fillna( ‘Small’,inplace= True)train[ ‘Item_Visibility’] = np.sqrt(train[ ‘Item_Visibility’])test[ ‘Item_Visibility’] = np.sqrt(test[ ‘Item_Visibility’])col = [ ‘Outlet_Size’, ‘Outlet_Location_Type’, ‘Outlet_Type’, ‘Item_Fat_Content’]test[ ‘Item_Outlet_Sales’] = 0combi = train.append(test) fori incol: combi[i] = number.fit_transform(combi[i].astype( ‘str’)) combi[i] = combi[i].astype( ‘object’)train = combi[:train.shape[ 0]]test = combi[train.shape[ 0]:]test.drop( ‘Item_Outlet_Sales’,axis= 1,inplace= True)

## removing id variables

tpot_train = train.drop([ ‘Outlet_Identifier’, ‘Item_Type’, ‘Item_Identifier’],axis= 1)tpot_test = test.drop([ ‘Outlet_Identifier’, ‘Item_Type’, ‘Item_Identifier’],axis= 1)target = tpot_train[ ‘Item_Outlet_Sales’]tpot_train.drop( ‘Item_Outlet_Sales’,axis= 1,inplace= True)

# finally building model using tpot library

fromtpot importTPOTRegressorX_train, X_test, y_train, y_test = train_test_split(tpot_train, target, train_size= 0.75, test_size= 0.25)tpot = TPOTRegressor(generations= 5, population_size= 50, verbosity= 2)tpot.fit(X_train, y_train)print(tpot.score(X_test, y_test))tpot.export( ‘tpot_boston_pipeline.py’)

## predicting using tpot optimised pipeline

tpot_pred = tpot.predict(tpot_test)sub1 = pd.DataFrame(data=tpot_pred)

#sub1.index = np.arange(0, len(test)+1)

sub1 = sub1.rename(columns = { ‘0’: ‘Item_Outlet_Sales’})sub1[ ‘Item_Identifier’] = test[ ‘Item_Identifier’]sub1[ ‘Outlet_Identifier’] = test[ ‘Outlet_Identifier’]sub1.columns = [ ‘Item_Outlet_Sales’, ‘Item_Identifier’, ‘Outlet_Identifier’]sub1 = sub1[[ ‘Item_Identifier’, ‘Outlet_Identifier’, ‘Item_Outlet_Sales’]]sub1.to_csv( ‘tpot.csv’,index= False)

6、 实际应用

6.1 工程设计

6.2 交通与船运路线（Travelling Salesman Problem，巡回售货员问题）

6.3 机器人

7、结语

END.

2119 篇文章110 人订阅

0 条评论

相关文章

用金庸、古龙群侠名称训练 LSTM，会生成多么奇葩的名字？

AI 研习社按：本文转载自 Magicly 博客，获作者授权。阅读原文请见：http://magicly.me/2017/04/07/rnn-lstm-gene...

40111

机器学习研究和开发所需的组件列表

Here is a list of components that are needed for the successful machine learning...

1022

叫一声lncRNA你敢答应么[男女不限]

lncRNA 的全称是long noncoding RNA。即又长又表达且还不能编码翻译成蛋白质的一类RNA。

2221

1K5

4636

81710

731

45712

3328

5037