## 一文读懂遗传算法工作原理算法

| |
[ 所属分类 商业智能 | 发布者 店小二05 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

tm

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

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、结语

QQ群：326600878

tags: Item,train,算法,Outlet,tpot,遗传,染色体,Fat,Identifier,TPOT,sub1,Content,Sales

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。