File size: 1,881 Bytes
ab360d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03548cb
ab360d8
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
import gradio as gr
import warnings
warnings.filterwarnings("ignore")

url='https://drive.google.com/file/d/1VfCaU5vFVWsSYrvKQF2x9iS6I7KoWEVH/view?usp=share_link'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
df = pd.read_csv(url)

df['cf_rating']=df['cf_rating'].astype(str)
df['cc_rating']=df['cc_rating'].astype(str)
df['cf_username']=df['cf_username'].astype(str)
df['cc_username']=df['cc_username'].astype(str)
df['ss_username']=df['ss_username'].astype(str)

only_cc = df[(df['cf_username'] == "nan")]
only_cf = df[(df['cc_username'] == "nan")]
both_cc_cf = df[(df['cf_username'] != "nan") & (df['cf_rating'] != "nan") & (df['cc_username'] != "nan") & (df['cc_rating'] != "nan")]
both_cc_cf.drop(columns=['ss_username','cf_username','cc_username'],inplace=True)


def linear_regression(X, y):
    #calculating mean
    mean_x = np.mean(X)
    mean_y = np.mean(y)
    
    n = 0
    d = 0
    for i in range(len(X)):
        n += (X[i] - mean_x) * (y[i] - mean_y)
        d += (X[i] - mean_x) ** 2
    #calculating weights and bias
    w = n/d
    b = mean_y-(w * mean_x)
  
    return (b[0], w[0])

X=both_cc_cf['cc_rating'].values.reshape(-1,1).astype(float)
Y=both_cc_cf['cf_rating'].values.reshape(-1,1).astype(float)

b_cc, w_cc = linear_regression(X,Y)
b_cf, w_cf = linear_regression(Y,X)

def predict_rating(platform, current_rating):
    if platform == "CodeChef":
        predicted_rating = current_rating * w_cc + b_cc
    else:
        predicted_rating = current_rating * w_cf + b_cf
    return int(predicted_rating)

interface = gr.Interface(
    fn=predict_rating,
    inputs=[
        gr.Radio(choices=["CodeChef", "Codeforces"], label="Choose your CP platform"),
        gr.Number(label="Current Rating")
    ],
    outputs="number",
    title="CodeChef and Codeforces Rating Predictor",
)

interface.launch()