关注“科研代码”,提升科研认知
最近国产3A大作,黑神话:悟空,终于上线了,不禁让我想到西游记这个文化宝藏里简直有数不尽的财富,对于机器学习领域甚至也有借鉴价值。在《西游记》中,有一段著名的故事:真假美猴王。孙悟空与六耳猕猴在外貌、声音、甚至法力上都极其相似,连唐长老和紧箍咒都无法分辨出谁是真正的孙悟空。为了分辨出真身,他们只能求助于如来佛祖。最终,通过如来的智慧,真相大白,孙悟空得以继续西行取经,而六耳猕猴则被揭露并消灭。
1. 判别分析(Discriminant Analysis)
2. 回归分析(Regression Analysis)
3. 预测分析(Predictive Analytics)
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import accuracy_score
# 生成虚拟数据
np.random.seed(42)
# 孙悟空的数据
sun_wukong = pd.DataFrame({
'Power_Level': np.random.randint(80, 101, 50),
'Magic_Fluctuation': np.random.randint(0, 21, 50),
'Response_Ability': np.random.randint(70, 101, 50),
'Expression_Stability': np.random.randint(80, 101, 50),
'Label': ['Sun Wukong'] * 50
})
# 六耳猕猴的数据
liu_er_mihou = pd.DataFrame({
'Power_Level': np.random.randint(60, 81, 50),
'Magic_Fluctuation': np.random.randint(50, 101, 50),
'Response_Ability': np.random.randint(40, 71, 50),
'Expression_Stability': np.random.randint(50, 81, 50),
'Label': ['Liu Er Mihou'] * 50
})
# 合并数据集
data = pd.concat([sun_wukong, liu_er_mihou])
# 特征和标签
X = data[['Power_Level', 'Magic_Fluctuation', 'Response_Ability', 'Expression_Stability']]
y = data['Label']
# 分割数据为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 建立线性判别分析模型
lda = LinearDiscriminantAnalysis()lda.fit(X_train, y_train)
# 预测测试集 y_pred = lda.predict(X_test) # 模型准确率 accuracy = accuracy_score(y_test, y_pred) print(f"模型准确率: {accuracy:.2f}")
总结
感谢关注!