用ChatGPT入门机器学习! + 机器学习助力料分析案例

使用ChatGPT学习机器学习

import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# 读取数据集
df = pd.read_csv("data.csv")

# 提取特征和标签
X = df.drop("label", axis=1)
y = df["label"]

# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化随机森林分类器
clf = RandomForestClassifier(n_estimators=100, random_state=42)

# 训练模型
clf.fit(X_train, y_train)

# 计算准确率
accuracy = clf.score(X_test, y_test)
print("Accuracy:", accuracy)

# 绘制特征重要性图
importances = clf.feature_importances_
indices = np.argsort(importances)[::-1]
plt.figure()
plt.title("Feature Importances")
plt.bar(range(X.shape[1]), importances[indices], color="r", align="center")
plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]])
plt.show()

import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# 读取数据集
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
df = pd.read_csv(url, names=names)

# 提取特征和标签
X = df.drop("class", axis=1)
y = df["class"]

# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化随机森林分类器
clf = RandomForestClassifier(n_estimators=100, random_state=42)

# 训练模型
clf.fit(X_train, y_train)

# 计算准确率
accuracy = clf.score(X_test, y_test)
print("Accuracy:", accuracy)

# 绘制特征重要性图
importances = clf.feature_importances_
indices = np.argsort(importances)[::-1]
plt.figure()
plt.title("Feature Importances")
plt.bar(range(X.shape[1]), importances[indices], color="r", align="center")
plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]])
plt.show()

体验 Nice!

进阶,mp-api做材料分析

结合一点前面学的mp-api做个材料分析

从 MaterialsProject网站选择Na Mg Al元素的材料,测试nisites与形成能的关系,看一下能否从nsites预测形成能

API_KEY = '这里输入自己的API KEY' ## 可以去MaterialProject官网注册获取
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from mp_api.client import MPRester
with MPRester(API_KEY) as mpr:
    docs = mpr.summary.search(elements=["Na", "Mg","Al"],  
                             fields = ['formation_energy_per_atom', 'nsites'])

可以看到,搜索到了35个材料

读取数据,X为nisites的变量,y为平均原子形成能数据,取20%作为测试

X=[[docs[i].nsites] for i in range(len(docs))]
y=[docs[i].formation_energy_per_atom for i in range(len(docs))]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train a random forest regressor on the training data
regressor = RandomForestRegressor()
regressor.fit(X_train, y_train)
score = regressor.score(X_test, y_test)
predictions = regressor.predict(X_test)
print(f"Test score: {score:.2f}")
import matplotlib.pyplot as plt

# Plot the predicted values against the actual values
plt.scatter(y_test, predictions)
plt.xlabel("Actual values")
plt.ylabel("Predicted values")
plt.title("Random forest regression")
plt.show()

可以看到,结果很不理想^_^,不过没关系,我们也可以预料到,因为格点数跟平均形成能本身也不会有啥关系,但是我们通过这个案例,基本可以入门机器学习分析材料。

展开阅读全文

页面更新:2024-04-02

标签:进阶   机器   准确率   助力   初始化   入门   模型   元素   特征   案例   测试   材料   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top