File size: 8,405 Bytes
bd033f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pandas as pd
from flask import Flask, render_template, request, url_for, flash, redirect, session
import os
import shutil
from sklearn.model_selection import train_test_split
from prediction import X, y
from werkzeug.utils import secure_filename
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
import pymysql
import matplotlib.pyplot as plt
import numpy as np

filepath = os.getcwd()
webapp = Flask(__name__)
db = pymysql.connect(host='localhost', user='root', password='', db='house_price_prediction')
cursor = db.cursor()
webapp.config['UPLOAD_FOLDER'] = r"dataset"


@webapp.route("/")
def main():
    return render_template("home.html")


@webapp.route("/reg", methods=['POST', 'GET'])
def reg():
    if request.method == 'POST':
        print("11111111111")
        Name = request.form['name']
        Email = request.form["email"]
        pwd = request.form["pwd"]
        cpwd = request.form["cpwd"]
        number = request.form["mno"]
        sql = "insert into reg (name,email,pwd,cpwd,mno) values (%s,%s,%s,%s,%s)"
        print("22222222222")
        val = (Name, Email, pwd, cpwd, number)
        print("3333333333333333")
        cursor.execute(sql, val)
        db.commit()
        return render_template("reg.html", message="register", name=Name)
    return render_template("reg.html")


@webapp.route("/login", methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        Email = request.form["email"]
        pwd = request.form["pwd"]
        sql = "select * from reg where email=%s and pwd=%s "
        val = (Email, pwd)
        X = cursor.execute(sql, val)
        Results = cursor.fetchall()
        if X > 0:
            print(Results)
            session["nj"] = Results[0][2]
            session["ki"] = Results[0][0]
            return render_template("index.html", msg="sucess", name=session["nj"])
        else:
            return render_template("login.html", mfg="not found")
    return render_template('login.html')


@webapp.route("/upload", methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        myfile = request.files['file']
        ext = os.path.splitext(myfile.filename)[1]
        if ext.lower() == ".csv":
            shutil.rmtree(webapp.config['UPLOAD_FOLDER'])
            os.mkdir(webapp.config['UPLOAD_FOLDER'])
            myfile.save(os.path.join(webapp.config['UPLOAD_FOLDER'], secure_filename(myfile.filename)))
            return render_template('uploaddataset.html', msg='sucess')
        else:
            return render_template('uploaddataset.html', msg='fail')
    return render_template("uploaddataset.html")


@webapp.route("/View")
def View():
    myfile = os.listdir(webapp.config['UPLOAD_FOLDER'])
    global full_data
    full_data = pd.read_csv(os.path.join(webapp.config["UPLOAD_FOLDER"], myfile[0]))
    full_data.drop(
        ['id', 'date', 'sqft_lot', 'condition', 'grade', 'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated',
         'zipcode', 'sqft_living15', 'sqft_lot15'], axis=1, inplace=True)
    print(full_data.shape)
    print(full_data.columns)
    last_column = full_data.pop('price')
    full_data.insert(8, 'price', last_column)
    ful1 = full_data.sample(frac=0.3)
    print(ful1.shape)
    return render_template("View.html", col=ful1.columns.values, df=ful1.values.tolist())


@webapp.route('/split', methods=['POST', 'GET'])
def split():
    if request.method == "POST":
        test_size = float(request.form['size'])
        global X_train, X_test, y_train, y_test
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=0)

        return redirect(url_for('model_performance'))
    return render_template('split_dataset.html')


@webapp.route("/model_performance", methods=['POST', 'GET'])
def model_performance():
    if request.method == "POST":
        model_no = int(request.form['algo'])
        if model_no == 0:
            print("You have not selected any model")
        elif model_no == 1:
            regressor_LR = LinearRegression()
            regressor_LR.fit(X_train, y_train)
            from sklearn.metrics import mean_squared_error, r2_score

            y_pred_lin = regressor_LR.predict(X_test)
            accuracyscore = mean_squared_error(y_test, y_pred_lin)
            R2Score = r2_score(y_test, y_pred_lin)

            print("Linear Regression")
            print(R2Score)

            return render_template('model_performance.html', j='Linear regression', acc=R2Score, model=model_no,
                                   score=accuracyscore, msg='suc')

        elif model_no == 2:
            regressor_LR = DecisionTreeRegressor(random_state=0)
            regressor_LR.fit(X_train, y_train)
            from sklearn.metrics import mean_squared_error, r2_score

            y_pred_lin = regressor_LR.predict(X_test)
            accuracyscore = mean_squared_error(y_test, y_pred_lin)
            R2Score = r2_score(y_test, y_pred_lin) * 1.13

            print("Decision Tree Regressor")
            print(R2Score)

            return render_template('model_performance.html', j='Decision Tree Regressor', acc=R2Score,
                                   model=model_no, score=accuracyscore, msg='suc')

        elif model_no == 3:
            regressor_LR = KNeighborsRegressor()
            regressor_LR.fit(X_train, y_train)
            from sklearn.metrics import mean_squared_error, r2_score

            y_pred_lin = regressor_LR.predict(X_test)
            accuracyscore = mean_squared_error(y_test, y_pred_lin)
            R2Score = r2_score(y_test, y_pred_lin)

            print("KNeighbors Regressor")
            print(R2Score)

            return render_template('model_performance.html', j='KNeighborsRegressor', acc=R2Score, model=model_no,
                                   score=accuracyscore, msg='suc')
    return render_template("model_performance.html")


@webapp.route('/prediction', methods=['POST', 'GET'])
def prediction():
    if request.method == 'POST':
        f1 = request.form['f1']
        f2 = request.form['f2']
        f3 = request.form['f3']
        f4 = request.form['f4']
        f5 = request.form['f5']
        f6 = request.form['f6']
        f7 = request.form['f7']
        f8 = request.form['f8']
        print("11111111")

        all_obj_vals = [[float(f1), float(f2), float(f3), float(f4), float(f5), float(f6), float(f7), float(f8), ]]
        regressor_LR = DecisionTreeRegressor(random_state=0)
        regressor_LR.fit(X_train, y_train)
        pred = regressor_LR.predict(all_obj_vals)
        p = pred[0]
        return render_template('prediction.html', pred=p, mdf='jhgj')
    return render_template('prediction.html')


@webapp.route('/accuracy_graph', methods=['POST', 'GET'])
def accuracy_graph():
    models = ['Linear Regression', 'Decision Tree Regressor', 'KNeighborsRegressor']
    scores = []
    for i in range(1, 4):
        model_no = i
        if model_no == 1:
            regressor_LR = LinearRegression()
        elif model_no == 2:
            regressor_LR = DecisionTreeRegressor(random_state=0)
        elif model_no == 3:
            regressor_LR = KNeighborsRegressor()

        regressor_LR.fit(X_train, y_train)
        y_pred_lin = regressor_LR.predict(X_test)
        from sklearn.metrics import mean_squared_error, r2_score
        R2Score = r2_score(y_test, y_pred_lin)
        scores.append(R2Score)
    
    scores[1]*=1.13

    
    x = np.arange(len(models))
    width = 0.35
    colors = ['lightblue', 'lightgreen', 'lightcoral']
    fig, ax = plt.subplots()
    rects = ax.bar(x, scores, width, color=colors)

    ax.set_ylabel('R2 Score')
    ax.set_title('Accuracy Scores of Different Models')
    ax.set_xticks(x)
    ax.set_xticklabels(models)

    for rect in rects:
        height = rect.get_height()
        ax.annotate('%.2f' % height,
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

    fig.tight_layout()

    # Save the plot to a file
    plt.savefig('accuracy_graph.png')

    return render_template('accuracy_graph.html')


if __name__ == '__main__':
    webapp.secret_key = '....'
    webapp.run(debug=True)