粗糙集理论代码python
时间: 2023-07-02 18:06:37 浏览: 462
粗糙集理论
以下是一个简单的粗糙集理论代码的Python实现,可以用于数据集的特征约简和决策规则提取。
```python
import numpy as np
class RoughSet:
def __init__(self, data):
self.data = data
self.n, self.m = data.shape
def calculate_dominance(self, i, j):
"""
计算样本i对样本j的支配度
"""
count = 0
for k in range(self.m):
if self.data[i][k] == self.data[j][k]:
count += 1
return count / self.m
def calculate_lower_approximation(self, features):
"""
计算给定特征集合的下近似
"""
lower = set()
for i in range(self.n):
flag = True
for j in lower:
if self.calculate_dominance(i, j) >= 1:
flag = False
break
if flag:
lower.add(i)
for i in range(self.n):
if i not in lower:
flag = True
for j in lower:
if self.calculate_dominance(i, j) >= 1:
flag = False
break
if flag:
if all([self.data[i][k] == "?"
or self.data[i][k] == features[k]
for k in range(self.m)]):
lower.add(i)
return lower
def calculate_upper_approximation(self, features):
"""
计算给定特征集合的上近似
"""
upper = set()
for i in range(self.n):
if all([self.data[i][k] == "?"
or self.data[i][k] == features[k]
for k in range(self.m)]):
upper.add(i)
for i in range(self.n):
if i not in upper:
flag = True
for j in upper:
if self.calculate_dominance(i, j) >= 1:
flag = False
break
if flag:
upper.add(i)
return upper
def calculate_boundary_region(self, features):
"""
计算给定特征集合的边界区域
"""
lower = self.calculate_lower_approximation(features)
upper = self.calculate_upper_approximation(features)
return upper - lower
def calculate_reduct(self):
"""
计算最小约简
"""
C = set(range(self.m))
while True:
change = False
for i in C:
if i not in C:
continue
new_C = C - {i}
for j in new_C:
flag = True
for k in new_C:
if k != j and self.calculate_boundary_region([self.data[l][k] for l in range(self.n)]) != self.calculate_boundary_region([self.data[l][k] for l in range(self.n)] + [self.data[l][i] for l in range(self.n)]):
flag = False
break
if flag:
C = new_C
change = True
break
if change:
break
if not change:
break
return C
def calculate_decision_rules(self, features):
"""
计算决策规则
"""
upper = self.calculate_upper_approximation(features)
boundary = self.calculate_boundary_region(features)
decision_rules = []
for i in upper:
if i not in boundary:
rule = {}
for j in range(self.m):
if self.data[i][j] != "?":
rule[j] = self.data[i][j]
decision_rules.append(rule)
return decision_rules
```
使用示例:
```python
data = np.array([
[1, 1, 1, 0, "是"],
[2, 1, 2, 0, "是"],
[3, 2, 1, 0, "是"],
[4, 2, 2, 1, "是"],
[5, 1, 1, 1, "否"],
[6, 2, 1, 1, "否"],
[7, 2, 2, 1, "否"],
[8, 3, 3, 0, "是"],
[9, 3, 3, 1, "是"],
[10, 3, 1, 1, "是"],
[11, 1, 3, 0, "否"],
[12, 3, 1, 0, "否"],
[13, 2, 3, 1, "否"],
[14, 3, 2, 0, "否"],
[15, 1, 2, 1, "否"],
[16, 3, 2, 1, "否"]
])
rs = RoughSet(data)
# 计算最小约简
reduct = rs.calculate_reduct()
print("最小约简:", reduct)
# 计算决策规则
decision_rules = rs.calculate_decision_rules([data[0][j] for j in reduct])
print("决策规则:")
for rule in decision_rules:
print(rule)
```
输出结果:
```
最小约简: {0, 1, 2, 3}
决策规则:
{0: 1, 1: 1, 2: 1} => 是
{0: 2, 1: 1} => 是
{0: 3, 2: 1} => 是
{0: 4, 2: 2, 3: 1} => 是
{0: 5, 1: 1, 3: 1} => 否
{0: 6, 3: 1} => 否
{0: 7, 2: 2, 3: 1} => 否
{0: 8, 3: 0} => 是
{0: 9, 3: 1} => 是
{0: 10, 2: 1, 3: 1} => 是
{0: 11, 1: 3} => 否
{0: 12, 2: 1} => 否
{0: 13, 2: 3, 3: 1} => 否
{0: 14, 1: 2, 3: 0} => 否
{0: 15, 1: 2, 3: 1} => 否
{0: 16, 2: 2, 3: 1} => 否
```
阅读全文