-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
160 lines (118 loc) · 4 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from collections import Counter
import math
import socket
class TreeNode:
def __init__(self, attribute=None, label=None):
self.attribute = attribute
self.label = label
self.children = {}
def receive_data(server_socket):
received_data = server_socket.recv(4096).decode()
return [row.split(",") for row in received_data.split("\n")]
# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server's IP address and port
server_ip = '10.0.0.2'
port = 12345
# Connect to the server
s.connect((server_ip, port))
# Receive data from the server (contains the test data)
data = receive_data(s)
def calculate_entropy(data):
target_labels = [row[-1] for row in data]
label_counts = {}
for label in target_labels:
if label in label_counts:
label_counts[label] += 1
else:
label_counts[label] = 1
entropy = 0.0
total_samples = len(target_labels)
for count in label_counts.values():
probability = float(count) / total_samples
entropy -= probability * math.log(probability, 2)
return entropy
def split_data(data, attribute_index, value):
return [row for row in data if row[attribute_index] == value]
def create_tree(data, depth):
root = TreeNode()
if depth <= 0 or not data or len(data[0]) <= 1:
labels = [row[-1] for row in data]
root.label = max(set(labels), key=labels.count)
return root
if all(row[-1] == data[0][-1] for row in data):
root.label = data[0][-1]
return root
entropy = calculate_entropy(data)
num_attributes = len(data[0]) - 1
max_info_gain = 0
best_attribute = None
for attribute_index in range(num_attributes):
values = set([row[attribute_index] for row in data])
info_gain = 0
for value in values:
sub_data = split_data(data, attribute_index, value)
prob = len(sub_data) / float(len(data))
info_gain += prob * calculate_entropy(sub_data)
info_gain = entropy - info_gain
if info_gain > max_info_gain:
max_info_gain = info_gain
best_attribute = attribute_index
root.attribute = best_attribute
values = set([row[best_attribute] for row in data])
for value in values:
sub_data = split_data(data, best_attribute, value)
root.children[value] = create_tree(sub_data, depth - 1)
return root
# (Previous code)
def serialize_tree(root):
if root:
serialized = []
if root.label:
serialized.append("Label:" + root.label)
else:
serialized.append("Attribute:" + str(root.attribute))
for value, child_node in root.children.items():
serialized.append(value)
serialized.extend(serialize_tree(child_node))
return serialized
decision_tree_depth = 5
tree_root = create_tree(data, decision_tree_depth)
serialized_tree = serialize_tree(tree_root)
# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server's IP address and port
server_ip = '10.0.0.2'
port = 12345
# Connect to the server
s.connect((server_ip, port))
serialized_tree_str = '\n'.join(serialized_tree)
# Send the serialized tree to the server
s.sendall(serialized_tree_str.encode())
# Close the connection
s.close()
"""
import socket
# Create a socket object
s = socket.socket()
# Define the server's IP address and port
server_ip = '10.0.0.2'
port = 12345
# Connect to the server
s.connect((server_ip, port))
# Receive data from the server
data = s.recv(4096).decode()
# Split the CSV data into rows
rows = data.split("\n")
for row in rows:
columns = row.split(",")
print(columns)
# Extract the first column of data
first_column = [row.split(",")[0] for row in rows]
# Convert the first column data to a comma-separated string
first_column_data = ",".join(first_column)
# Send the first column data back to the server
s.sendall(first_column_data.encode())
# Close the connection
s.close()
"""