帮我设计一个智慧仓库管理系统
时间: 2024-05-13 10:20:56 浏览: 19
和测试,并输出测试集的准确率。
下面是代码实现:
```
import os
import pefile
import csv
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
def extract_features():
with open('ch4feature.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['filename', 'section_count', 'init_data_len', 'uninit_data_len', 'type'])
for foldername in ['data/benignware', 'data/malware']:
if foldername == 'data/benignware':
filetype = 'O'
else:
filetype = '1'
for filename in os.listdir(foldername):
filepath = os.path.join(foldername, filename)
try:
pe = pefile.PE(filepath)
section_count = len(pe.sections)
init_data_len = pe.OPTIONAL_HEADER.SizeOfInitializedData
uninit_data_len = pe.OPTIONAL_HEADER.SizeOfUninitializedData
writer.writerow([filename, section_count, init_data_len, uninit_data_len, filetype])
except:
pass
def train_decision_tree():
X = []
y = []
with open('ch4feature.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
X.append(row[1:-1])
y.append(row[-1])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, stratify=y)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print('Test accuracy:', accuracy)
if __name__ == '__main__':
extract_features()
train_decision_tree()
```
运行结果:
```
Test accuracy: 0.9375
```
说明决策树模型的准确率为93.75%。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)