机器学习朴素贝叶斯作业
def fit(): # 求均值&方差
mean_var = [[np.mean(X[y == label], axis=0), np.var(X[y == label], axis=0)] for label in np.unique(y)]
return mean_var
import numpy as np
def fit(): # 求均值&方差
mean_var = [[np.mean(X[y == label], axis=0), np.var(X[y == label], axis=0)] for label in np.unique(y)]
return mean_var
if __name__ == '__main__':
# 读取数据
data = np.loadtxt("iris.csv", # 数据源
dtype='str', # 读取类型
delimiter=',', # 分割符号
skiprows=1)
# 数据预处理
X = data[::, 0:-1].astype('float32')
y = data[:, -1]
print(fit())
[[array([5.006, 3.428, 1.462, 0.246]), array([0.121764, 0.140816, 0.029556, 0.010884])],
[array([5.936, 2.77 , 4.26 , 1.326]), array([0.261104, 0.0965 , 0.2164 , 0.038324])],
[array([6.588, 2.974, 5.552, 2.026]), array([0.396256, 0.101924, 0.298496, 0.073924])]]
### array([5.006, 3.428, 1.462, 0.246])
### 为第一类(Setosa)在sepal.length sepal.width petal.length petal.width四个属性的均值
### array([0.121764, 0.140816, 0.029556, 0.010884])
### 为第一类(Setosa)在sepal.length sepal.width petal.length petal.width四个属性的方差
### array([5.936, 2.77 , 4.26 , 1.326])
### 为第二类(Versicolor)在sepal.length sepal.width petal.length petal.width四个属性的均值
### array([0.261104, 0.0965 , 0.2164 , 0.038324])
### 为第二类(Versicolor)在sepal.length sepal.width petal.length petal.width四个属性的方差
### array([6.588, 2.974, 5.552, 2.026])
### 为第三类(Virginica)在sepal.length sepal.width petal.length petal.width四个属性的均值
### array([0.396256, 0.101924, 0.298496, 0.073924])
### 为第三类(Virginica)在sepal.length sepal.width petal.length petal.width四个属性的方差
def score(self, feature, label): # 模型评估
return np.sum(np.array([[np.argmax(np.array([np.array(
self.GaussianProbability(sample, np.array([i[0] for i in self.fit()])[j],
np.array([i[1] for i in self.fit()])[j])).prod() for j in
range(len(np.unique(label)))]))] for sample in
feature]).ravel() == label) / label.size
### 注意为核心代码块 由外及里 暂时先不考虑内容
np.sum(np.array([<core> for sample in feature]).ravel() == label) / label.size
### 代码
np.argmax(np.array([np.array(
self.GaussianProbability(sample, np.array([i[0] for i in self.fit()])[j],
np.array([i[1] for i in self.fit()])[j])).prod() for j in
range(len(np.unique(label)))]))
import numpy as np
def GaussianProbability(x, mean, var): # 高斯概率密度函数
return np.array(
[1 / (np.sqrt(2 * np.pi) * var[i]) *
np.exp(-np.power(x[i] - mean[i], 2) / (2 * np.power(var[i], 2))) for i in range(len(x))])
def fit(): # 求均值&方差
mean_var = [[np.mean(X[y == label], axis=0), np.var(X[y == label], axis=0)] for label in np.unique(y)]
return mean_var
def score(feature, label): # 模型评估
return np.sum(np.array([[np.argmax(np.array([np.array(
GaussianProbability(sample, np.array([i[0] for i in fit()])[j],
np.array([i[1] for i in fit()])[j])
).prod() for j in range(len(np.unique(label)))]))] for sample in
feature]).ravel() == label) / label.size
if __name__ == '__main__':
# 读取数据
data = np.loadtxt("iris.csv", # 数据源
dtype='str', # 读取类型
delimiter=',', # 分割符号
skiprows=1)
# 数据预处理
X = data[::, 0:-1].astype('float32')
y = data[:, -1]
y = [0 if i == 'Setosa' else i for i in list(y)]
y = [1 if i == 'Versicolor' else i for i in list(y)]
y = [2 if i == 'Virginica' else i for i in list(y)]
print(f'模型准确率为:{score(X, np.array(y)) * 100}%')