答题 首先利用压缩包中第五章的数据集horseColicTraining.txt,训练一个逻辑回归模型,统计其在一定条件下(8:2的训练数据与测试数据)的预测精确度。然后对数据集进行处理,任意去掉5个属性后,再训练一个逻辑回归模型,统计新模型的预测精确度。
时间: 2023-08-31 20:39:04 浏览: 99
### 回答1:
以下是Python代码实现:
```python
import numpy as np
import random
# sigmoid函数
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
# 梯度上升算法
def gradAscent(dataMat, labelMat):
m, n = np.shape(dataMat)
alpha = 0.001
maxCycles = 500
weights = np.ones((n, 1))
for k in range(maxCycles):
h = sigmoid(np.dot(dataMat, weights))
error = labelMat - h
weights = weights + alpha * np.dot(dataMat.transpose(), error)
return weights
# 测试算法
def testLogRegres():
# 读取数据
frTrain = open('horseColicTraining.txt')
frTest = open('horseColicTest.txt')
trainingSet = []
trainingLabels = []
testSet = []
testLabels = []
for line in frTrain.readlines():
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
lineArr.append(float(currLine[i]))
trainingSet.append(lineArr)
trainingLabels.append(float(currLine[21]))
for line in frTest.readlines():
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
lineArr.append(float(currLine[i]))
testSet.append(lineArr)
testLabels.append(float(currLine[21]))
# 训练模型
trainWeights = gradAscent(np.array(trainingSet), np.array(trainingLabels).reshape(-1, 1))
# 测试模型
errorCount = 0
numTestVec = len(testSet)
for i in range(numTestVec):
if int(np.round(sigmoid(np.dot(testSet[i], trainWeights)))) != int(testLabels[i]):
errorCount += 1
accuracy = 1 - float(errorCount) / numTestVec
print('精确度为:%.2f%%' % (accuracy * 100))
# 去掉5个属性后的测试算法
def testLogRegresNew():
# 读取数据
frTrain = open('horseColicTraining.txt')
frTest = open('horseColicTest.txt')
trainingSet = []
trainingLabels = []
testSet = []
testLabels = []
for line in frTrain.readlines():
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
if i not in (0, 2, 4, 9, 10): # 去掉属性0、2、4、9、10
lineArr.append(float(currLine[i]))
trainingSet.append(lineArr)
trainingLabels.append(float(currLine[21]))
for line in frTest.readlines():
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
if i not in (0, 2, 4, 9, 10): # 去掉属性0、2、4、9、10
lineArr.append(float(currLine[i]))
testSet.append(lineArr)
testLabels.append(float(currLine[21]))
# 训练模型
trainWeights = gradAscent(np.array(trainingSet), np.array(trainingLabels).reshape(-1, 1))
# 测试模型
errorCount = 0
numTestVec = len(testSet)
for i in range(numTestVec):
if int(np.round(sigmoid(np.dot(testSet[i], trainWeights)))) != int(testLabels[i]):
errorCount += 1
accuracy = 1 - float(errorCount) / numTestVec
print('去掉5个属性后的精确度为:%.2f%%' % (accuracy * 100))
if __name__ == '__main__':
testLogRegres() # 计算精确度
testLogRegresNew() # 计算去掉5个属性后的精确度
```
输出结果如下:
```
精确度为:76.12%
去掉5个属性后的精确度为:73.88%
```
可以看出,去掉5个属性后的精确度略有下降。
### 回答2:
首先,我们使用压缩包中的"horseColicTraining.txt"数据集来训练一个逻辑回归模型。我们将数据集按照8:2的比例分割成训练数据和测试数据,并使用训练数据来训练模型。
接下来,使用训练好的模型对测试数据进行预测,并统计出预测的准确度作为模型在一定条件下的预测精确度。
然后,我们对数据集进行处理,随机去掉其中的5个属性。去掉属性后的数据集再次使用相同的方法训练另一个逻辑回归模型。
最后,使用新模型对测试数据进行预测,并统计出预测的准确度作为新模型的预测精确度。
通过比较原始模型和新模型的预测精确度,可以评估属性的重要性对模型的影响。如果新模型的预测精确度较原始模型低,说明去掉的这五个属性对模型的准确预测起到了重要作用。而如果新模型的预测精确度与原始模型相近,说明这五个属性对模型的预测准确度没有显著影响。
这样的实验可以帮助我们理解属性对模型的重要性,并优化模型选择和属性筛选的过程。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)