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

xiaoxiao2021-02-28  8

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

1、遗传算法理论的由来

2、生物学的启发

3、遗传算法定义

4、遗传算法具体步骤

5、遗传算法的应用

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 import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline from sklearn import preprocessing from sklearn.metrics import mean_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'] = 0 combi = train.append(test) for i in col: 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 from tpot import TPOTRegressor X_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、结语