Skip to content

Commit f939539

Browse files
authored
Merge pull request #2 from JEENB/models
working model
2 parents c9761fc + 6124291 commit f939539

File tree

9 files changed

+271
-61
lines changed

9 files changed

+271
-61
lines changed

client1.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import socket
3+
import time
4+
5+
6+
PORT = 2223
7+
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
client.connect((socket.gethostname(), PORT))
9+
print("[INFO] Connected to server")
10+
file_name = "original.jpeg"
11+
file_size = os.path.getsize(file_name)
12+
13+
print("[INFO] sending file")
14+
client.send(str(file_size).encode())
15+
16+
17+
# Opening file and sending data.
18+
with open(file_name, "rb") as file:
19+
c = 0
20+
# Starting the time capture.
21+
start_time = time.time()
22+
23+
# Running loop while c != file_size.
24+
while c <= file_size:
25+
data = file.read(1024)
26+
if not (data):
27+
break
28+
client.sendall(data)
29+
c += len(data)
30+
31+
# Ending the time capture.
32+
end_time = time.time()
33+
34+
print("[INFO] File Transfer Complete.Total time: ", end_time - start_time)
35+
# Closing the socket.
36+
37+
#ask client some questions regarding model
38+
degree = input("Enter the degree of regression. Eg- 3\n")
39+
client.send(degree.encode())
40+
training_ratio = input("Split Ratio: Mention the percentage for training dataset. Eg- 80\n")
41+
42+
#send answers to server
43+
44+
client.send(training_ratio.encode())
45+
46+
47+
48+
client.close()

dataset.csv

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,101 @@
1-
x, y
2-
1, 2
3-
2, 3
4-
3, 4
5-
4, 3
6-
2, 3
7-
12, 12.123233
8-
2, 12.21
9-
4, 343.212121
10-
444.2323, 234234.1231231
11-
342.1231, 1231.3532
12-
3, 1
13-
2, 3
14-
2, 3.4444444469
15-
3, 2
1+
x,y
2+
32.502345269453031,31.70700584656992
3+
53.426804033275019,68.77759598163891
4+
61.530358025636438,62.562382297945803
5+
47.475639634786098,71.546632233567777
6+
59.813207869512318,87.230925133687393
7+
55.142188413943821,78.211518270799232
8+
52.211796692214001,79.64197304980874
9+
39.299566694317065,59.171489321869508
10+
48.10504169176825,75.331242297063056
11+
52.550014442733818,71.300879886850353
12+
45.419730144973755,55.165677145959123
13+
54.351634881228918,82.478846757497919
14+
44.164049496773352,62.008923245725825
15+
58.16847071685779,75.392870425994957
16+
56.727208057096611,81.43619215887864
17+
48.955888566093719,60.723602440673965
18+
44.687196231480904,82.892503731453715
19+
60.297326851333466,97.379896862166078
20+
45.618643772955828,48.847153317355072
21+
38.816817537445637,56.877213186268506
22+
66.189816606752601,83.878564664602763
23+
65.41605174513407,118.59121730252249
24+
47.48120860786787,57.251819462268969
25+
41.57564261748702,51.391744079832307
26+
51.84518690563943,75.380651665312357
27+
59.370822011089523,74.765564032151374
28+
57.31000343834809,95.455052922574737
29+
63.615561251453308,95.229366017555307
30+
46.737619407976972,79.052406169565586
31+
50.556760148547767,83.432071421323712
32+
52.223996085553047,63.358790317497878
33+
35.567830047746632,41.412885303700563
34+
42.436476944055642,76.617341280074044
35+
58.16454011019286,96.769566426108199
36+
57.504447615341789,74.084130116602523
37+
45.440530725319981,66.588144414228594
38+
61.89622268029126,77.768482417793024
39+
33.093831736163963,50.719588912312084
40+
36.436009511386871,62.124570818071781
41+
37.675654860850742,60.810246649902211
42+
44.555608383275356,52.682983366387781
43+
43.318282631865721,58.569824717692867
44+
50.073145632289034,82.905981485070512
45+
43.870612645218372,61.424709804339123
46+
62.997480747553091,115.24415280079529
47+
32.669043763467187,45.570588823376085
48+
40.166899008703702,54.084054796223612
49+
53.575077531673656,87.994452758110413
50+
33.864214971778239,52.725494375900425
51+
64.707138666121296,93.576118692658241
52+
38.119824026822805,80.166275447370964
53+
44.502538064645101,65.101711570560326
54+
40.599538384552318,65.562301260400375
55+
41.720676356341293,65.280886920822823
56+
51.088634678336796,73.434641546324301
57+
55.078095904923202,71.13972785861894
58+
41.377726534895203,79.102829683549857
59+
62.494697427269791,86.520538440347153
60+
49.203887540826003,84.742697807826218
61+
41.102685187349664,59.358850248624933
62+
41.182016105169822,61.684037524833627
63+
50.186389494880601,69.847604158249183
64+
52.378446219236217,86.098291205774103
65+
50.135485486286122,59.108839267699643
66+
33.644706006191782,69.89968164362763
67+
39.557901222906828,44.862490711164398
68+
56.130388816875467,85.498067778840223
69+
57.362052133238237,95.536686846467219
70+
60.269214393997906,70.251934419771587
71+
35.678093889410732,52.721734964774988
72+
31.588116998132829,50.392670135079896
73+
53.66093226167304,63.642398775657753
74+
46.682228649471917,72.247251068662365
75+
43.107820219102464,57.812512976181402
76+
70.34607561504933,104.25710158543822
77+
44.492855880854073,86.642020318822006
78+
57.50453330326841,91.486778000110135
79+
36.930076609191808,55.231660886212836
80+
55.805733357942742,79.550436678507609
81+
38.954769073377065,44.847124242467601
82+
56.901214702247074,80.207523139682763
83+
56.868900661384046,83.14274979204346
84+
34.33312470421609,55.723489260543914
85+
59.04974121466681,77.634182511677864
86+
57.788223993230673,99.051414841748269
87+
54.282328705967409,79.120646274680027
88+
51.088719898979143,69.588897851118475
89+
50.282836348230731,69.510503311494389
90+
44.211741752090113,73.687564318317285
91+
38.005488008060688,61.366904537240131
92+
32.940479942618296,67.170655768995118
93+
53.691639571070056,85.668203145001542
94+
68.76573426962166,114.85387123391394
95+
46.230966498310252,90.123572069967423
96+
68.319360818255362,97.919821035242848
97+
50.030174340312143,81.536990783015028
98+
49.239765342753763,72.111832469615663
99+
50.039575939875988,85.232007342325673
100+
48.149858891028863,66.224957888054632
101+
25.128484647772304,53.454394214850524

models.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@
99
import numpy as np
1010
from random import randint, sample
1111
import pandas as pd
12+
import matplotlib.pyplot as plt
13+
import pandas as pd
1214

13-
14-
def reg_training_model(df, degree = 1):
15+
def reg_training_model(df, degree = 1, split_ratio = 0.2 ):
1516
'''
1617
function: poly_reg (Gives the polynomial regression for a set of data)
1718
Using PolymnomialFeatures and LinearRegression functions, fits a polynomial of degree n.
1819
1920
parm:
2021
1> degree: degree of the polynomial you want to fit
21-
2> x_train, y_train : training dataset 1d array
22-
3> x_test, y_test: testing dataset 1d array
22+
2> df: pandas df with x and y
23+
3> split_ratio: testing and training
2324
2425
retun:
2526
1> training error and testing error
2627
2728
Note: This function will also plot n = degree number of plots
2829
'''
30+
y = df['y']
2931
x = df.drop('y', axis = 1)
30-
y = df["y"]
31-
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
32+
33+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = split_ratio)
3234

3335

3436
poly_features = PolynomialFeatures(degree)
@@ -44,5 +46,19 @@ def reg_training_model(df, degree = 1):
4446
test_error = mean_squared_error(y_test, y_test_pred)
4547
training_error = mean_squared_error(y_train, y_train_pred)
4648

49+
50+
51+
##@Plotting
52+
x_test_1d = np.ravel(x_train)
53+
df = pd.DataFrame({"x_test": x_test_1d, "y_test_pred": y_train_pred})
54+
df.sort_values(by=["x_test"], inplace = True)
55+
56+
plt.scatter(x = x_train, y = y_train)
57+
plt.plot(df.x_test, df.y_test_pred, label = "Polynomial degree = {}".format(degree), color='r')
58+
plt.legend(loc='upper left')
59+
plt.savefig("server_data/fitting.png")
60+
61+
62+
4763
return test_error, training_error, y_test_pred
4864

neworignial.jpeg

112 KB
Loading

original.jpeg

112 KB
Loading

receivedfile.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ x, y
1212
3, 1
1313
2, 3
1414
2, 3.4444444469
15-
3, 2
15+
3, 1
16+
222, 222.1

receiver.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
1+
# This file will be used for recieving files over socket connection.
22
import os
33
import socket
44
import time
55

6-
PORT = 2223
7-
SERVER = socket.gethostname()
8-
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9-
server.bind((SERVER, PORT))
6+
host = input("Host Name: ")
7+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
108

9+
# Trying to connect to socket.
10+
try:
11+
sock.connect((host, 22222))
12+
print("Connected Successfully")
13+
except:
14+
print("Unable to connect")
15+
exit(0)
1116

12-
while True:
13-
server.listen(5)
14-
print(f"[LISTENING] Server is listening on {SERVER}")
15-
conn, addr = server.accept()
16-
print("Connection established with " + str(addr[0]) + ", " + str(addr[1]))
17+
# Send file details.
18+
file_name = sock.recv(100).decode()
19+
file_size = sock.recv(100).decode()
1720

18-
file_name = 'receivedfile.csv'
19-
file_size = conn.recv(100).decode()
21+
# Opening and reading file.
22+
with open("./1" + file_name, "wb") as file:
23+
c = 0
24+
# Starting the time capture.
25+
start_time = time.time()
2026

21-
# Opening and reading file.
22-
with open(file_name, "wb") as file:
23-
c = 0
24-
# Starting the time capture.
25-
start_time = time.time()
27+
# Running the loop while file is recieved.
28+
while c <= int(file_size):
29+
data = sock.recv(1024)
30+
if not (data):
31+
break
32+
file.write(data)
33+
c += len(data)
2634

27-
# Running the loop while file is recieved.
28-
while c <= int(file_size):
29-
data = conn.recv(1024)
30-
if not (data):
31-
break
32-
file.write(data)
33-
c += len(data)
35+
# Ending the time capture.
36+
end_time = time.time()
3437

35-
# Ending the time capture.
36-
end_time = time.time()
38+
print(file)
3739

38-
print("File transfer Complete.Total time: ", end_time - start_time)
40+
print("File transfer Complete.Total time: ", end_time - start_time)
3941

40-
# Closing the socket.
41-
conn.close()
42+
# Closing the socket.
43+
sock.close()

sender.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
import socket
33
import time
44

5+
# Creating a socket.
6+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7+
sock.bind((socket.gethostname(), 22222))
8+
sock.listen(5)
9+
print("Host Name: ", sock.getsockname())
510

6-
PORT = 2223
7-
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8-
client.connect((socket.gethostname(), PORT))
9-
print("[INFO] Connected to server")
10-
file_name = "dataset.csv"
11+
# Accepting the connection.
12+
client, addr = sock.accept()
13+
14+
# Getting file details.
15+
file_name = input("File Name:")
1116
file_size = os.path.getsize(file_name)
1217

13-
print("[INFO] sending file")
18+
# Sending file_name and detail.
19+
client.send(file_name.encode())
1420
client.send(str(file_size).encode())
1521

16-
1722
# Opening file and sending data.
1823
with open(file_name, "rb") as file:
1924
c = 0
@@ -31,6 +36,6 @@
3136
# Ending the time capture.
3237
end_time = time.time()
3338

34-
print("[INFO] File Transfer Complete.Total time: ", end_time - start_time)
35-
# Closing the socket.
36-
client.close()
39+
print("File Transfer Complete.Total time: ", end_time - start_time)
40+
# Cloasing the socket.
41+
sock.close()

0 commit comments

Comments
 (0)