Machine Learning with Matminer
1. Matminer 介绍
随着材料数据集的规模和范围的扩大,数据挖掘和统计学习方法在分析这些材料数据集和建立预测模型方面的作用变得越来越重要。Matminer是一个开源的、基于python的软件平台,以促进数据驱动的方法来分析和预测材料的属性。Matminer提供了用于从外部数据库中检索大型数据集的模块,如the Materials Project, Citrination, Materials Data Facility, and Materials Platform for Data Science。它还提供了由材料社区开发的广泛的特征提取例程库的实现,包括47个特性类,这些类可以生成数千个单独的描述符,并将它们组合成数学函数。最后,matminer提供了一个可视化模块,用于生成交互式的、可共享的绘图。这些函数的设计方式与Python数据科学社区已经开发和使用的机器学习和数据分析包紧密集成。[1]
2. Matminer安装
pip install matminer
3. ML with Matminer
3.1 获取Matminer内置数据集
获取内置数据集名字
from matminer.datasets import get_available_datasets
print(get_available_datasets())# 获取内置数据集名字
下载内置数据集,并查看描述性统计信息
from matminer.datasets import load_dataset
df = load_dataset("dielectric_constant",data_home='.')print(df.head(5))print(df.columns)print(df.describe())print(df["band_gap"])# 查看数据带隙性质数据print(df["band_gap"].values)# 带隙性质数据转化为array数组print(df.iloc[20])# 查看索引为20的材料数据
数据清洗
cleaned_df = df.drop(["nsites","space_group"],axis=1)print(cleaned_df.head(3))
3.2 建立特征
1. 使用dielectric_constant数据集举例,首先下载数据集
df = load_dataset("dielectric_constant",data_home='.')print(df.columns)
输出结果如下
Index(['material_id','formula','nsites','space_group','volume','structure','band_gap','e_electronic','e_total','n','poly_electronic','poly_total','pot_ferroelectric','cif','meta','poscar'],
dtype='object')
2. 转化formula到Composition
from matminer.featurizers.conversions import StrToComposition
stc = StrToComposition()
df = stc.featurize_dataframe(df,'formula')print(df.columns)
输出结果如下
Index(['material_id','formula','nsites','space_group','volume','structure','band_gap','e_electronic','e_total','n','poly_electronic','poly_total','pot_ferroelectric','cif','meta','poscar','composition'],
dtype='object')
3. 添加composition特征
from matminer.featurizers.composition import ElementFraction
ele = ElementFraction()
df = ele.featurize_dataframe(df,'composition')print(df.columns)
输出结果如下
Index(['material_id','formula','nsites','space_group','volume','structure','band_gap','e_electronic','e_total','n',...'Pu','Am','Cm','Bk','Cf','Es','Fm','Md','No','Lr'],
dtype='object', length=120)
4. 添加structure特征
from matminer.featurizers.structure import DensityFeatures
den = DensityFeatures()
df = den.featurize_dataframe(df,"structure")print(df.columns)
输出结果如下
Index(['material_id','formula','nsites','space_group','volume','structure','band_gap','e_electronic','e_total','n',...'Bk','Cf','Es','Fm','Md','No','Lr','density','vpa','packing fraction'],
dtype='object', length=123)
3.3 使用随机森林对带隙性质进行预测
构造用于机器学习模型的数据特征和数据标签
y = df['band_gap'].values
X = df.drop(['material_id','formula','structure','band_gap','e_electronic','e_total','n','poly_electronic','poly_total','pot_ferroelectric','cif','meta','poscar','composition'],axis=1)print(X.columns)
划分训练集和测试集
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
使用随机森林拟合回归模型并对测试数据进行预测
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators=100)
rf.fit(X_train,y_train)
y_pred = rf.predict(X_test)print(y_pred)
计算拟合RMSE
from sklearn.metrics import mean_absolute_error
mse = mean_absolute_error(y_test,y_pred)import numpy as np
print(np.sqrt(mse))# 0.8358410068184292
使用10折交叉验证测试算法的准确性
from sklearn.model_selection import KFold
kfold = KFold(n_splits=10)from sklearn.model_selection import cross_val_score
scores = cross_val_score(rf,X_train,y_train,scoring='neg_mean_squared_error',cv=kfold)# 10个值代表10次交叉验证的结果print(scores)# [-1.50546588 -1.0717101 -0.93992207 -0.9835829 -0.74685609 -0.92841462# -0.99700837 -1.76563376 -0.76767876 -0.99548007]
rmse_scores =[np.sqrt(abs(s))for s in scores]# 计算10次交叉验证的平均值print(np.mean(rmse_scores))# 1.0252397819735934
参考
[1] Matminer: An open source toolkit for materials data mining
[2] 视频
版权归原作者 喝过期的拉菲 所有, 如有侵权,请联系我们删除。