forked from applefunaf/finalwork_json-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM_Delete_data_row.py
More file actions
111 lines (91 loc) · 4.01 KB
/
SVM_Delete_data_row.py
File metadata and controls
111 lines (91 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import json
import pandas as pd
from rdkit import Chem
from rdkit.Chem import Descriptors
from rdkit.Chem.AllChem import GetMorganFingerprintAsBitVect
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR # 使用 SVR 而不是 SVC
from sklearn.metrics import mean_squared_error, r2_score
# 指定包含 JSON 文件的本地文件夹路径
folder_path = 'C:\\Users\\windows\\OneDrive\\Desktop\\ordjson'
# 初始化一个空的列表来存储数据
data_list = []
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.json'):
file_path = os.path.join(folder_path, filename)
# 读取 JSON 文件
with open(file_path, 'r') as file:
data = json.load(file)
# 提取 SMILES、reagent、catalyst 和 yield 信息
smiles_list = []
reagents = []
catalysts = []
yield_value = None
for input_type, input_data in data['inputs'].items():
for component in input_data['components']:
for identifier in component['identifiers']:
if identifier['type'] == 'SMILES':
smiles_list.append(identifier['value'])
if input_type == 'Base':
reagents.append(component['amount']['moles']['value'])
elif input_type == 'metal and ligand':
catalysts.append(component['amount']['moles']['value'])
for outcome in data.get('outcomes', []):
for product in outcome.get('products', []):
if product.get('is_desired_product', False):
for measurement in product.get('measurements', []):
if measurement['type'] == 'YIELD':
yield_value = measurement['percentage']['value']
# 计算分子指纹和描述符
for smiles in smiles_list:
molecule = Chem.MolFromSmiles(smiles)
if molecule:
fingerprint = GetMorganFingerprintAsBitVect(molecule, radius=2, nBits=2048)
fingerprint_bits = list(fingerprint)
# 计算分子描述符
descriptors = {desc_name: desc_func(molecule) for desc_name, desc_func in Descriptors.descList}
# 将数据添加到列表中
data_list.append({
'fingerprint': fingerprint_bits,
'reagent': sum(reagents),
'catalyst': sum(catalysts),
'yield': yield_value,
**descriptors
})
else:
print(f"Invalid SMILES: {smiles}")
# 将数据转换为 pandas 数据框
df = pd.DataFrame(data_list)
# 将分子指纹展开为单独的列
fingerprint_df = pd.DataFrame(df['fingerprint'].tolist())
df = df.drop(columns=['fingerprint']).join(fingerprint_df)
# 删除包含 NaN 值的行
df.dropna(inplace=True)
# 将所有列名转换为字符串类型
df.columns = df.columns.astype(str)
# 分离特征和目标变量
X = df.drop(columns=['yield'])
y = df['yield']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 标准化特征
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 初始化并训练支持向量回归模型
svr_classifier = SVR(kernel='linear')
svr_classifier.fit(X_train, y_train)
# 预测测试集
y_pred = svr_classifier.predict(X_test)
# 评估模型性能
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Mean Squared Error: {mse}')
print(f'R² Score: {r2}')
'''
Mean Squared Error: 881.9533834528651
R² Score: -0.022689262137703725
'''