1#import libraries
2import numpy as np
3from flask import Flask, render_template,request
4import pickle#Initialize the flask App
5app = Flask(__name__)
6model = pickle.load(open('model.pkl', 'rb'))
1#To use the predict button in our web-app
2@app.route('/predict',methods=['POST'])
3def predict():
4 #For rendering results on HTML GUI
5 int_features = [float(x) for x in request.form.values()]
6 final_features = [np.array(int_features)]
7 prediction = model.predict(final_features)
8 output = round(prediction[0], 2)
9 return render_template('index.html', prediction_text='CO2 Emission of the vehicle is :{}'.format(output))
1#default page of our web-app
2@app.route('/')
3def home():
4 return render_template('index.html')
1import pandas as pd
2from sklearn.linear_model import LinearRegression
3import pickle
4
5df = pd.read_csv("FuelConsumption.csv")
6#use required features
7cdf = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
8
9#Training Data and Predictor Variable
10# Use all data for training (tarin-test-split not used)
11x = cdf.iloc[:, :3]
12y = cdf.iloc[:, -1]
13regressor = LinearRegression()
14
15#Fitting model with trainig data
16regressor.fit(x, y)
17
18# Saving model to current directory
19# Pickle serializes objects so they can be saved to a file, and loaded in a program again later on.
20pickle.dump(regressor, open('model.pkl','wb'))
21
22'''
23#Loading model to compare the results
24model = pickle.load(open('model.pkl','rb'))
25print(model.predict([[2.6, 8, 10.1]]))
26'''