Trickkilled commited on
Commit
73f7ad5
·
1 Parent(s): bdd84ab

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.preprocessing import StandardScaler
5
+ from sklearn.impute import SimpleImputer
6
+ from tensorflow.keras.models import Sequential
7
+ from tensorflow.keras.layers import Dense
8
+ from tensorflow.keras.models import load_model
9
+
10
+ model = load_model("auto_mpg_model.h5")
11
+
12
+ imputer = SimpleImputer(strategy='median')
13
+ scaler = StandardScaler()
14
+
15
+ def predict_mpg(data):
16
+ input_data = np.array(data).reshape(1, -1)
17
+
18
+ input_scaled = scaler.fit_transform(input_data)
19
+
20
+ prediction = model.predict(input_scaled)
21
+
22
+ return prediction[0][0]
23
+
24
+ input_data = [4, 121, 110, 2800, 15.4, 81, 3]
25
+ predicted_mpg = predict_mpg(input_data)
26
+ print("Predicted MPG:", predicted_mpg)
27
+
28
+ auto_mpg_data_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
29
+ column_names = ['MPG', 'Cylinders', 'Displacement', 'Horsepower', 'Weight', 'Acceleration', 'Model Year', 'Origin']
30
+ data = pd.read_csv(auto_mpg_data_url, names=column_names, na_values='?', comment='\t', sep=' ', skipinitialspace=True)
31
+
32
+ data = data.dropna()
33
+
34
+ X = data.drop('MPG', axis=1)
35
+ y = data['MPG']
36
+
37
+ imputer = SimpleImputer(strategy='median')
38
+ X_imputed = imputer.fit_transform(X)
39
+
40
+ scaler = StandardScaler()
41
+ X_scaled = scaler.fit_transform(X_imputed)
42
+
43
+ X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
44
+
45
+ model = Sequential([
46
+ Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
47
+ Dense(32, activation='relu'),
48
+ Dense(1)
49
+ ])
50
+
51
+ model.compile(optimizer='adam', loss='mean_squared_error')
52
+
53
+ model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=2)
54
+
55
+ model.save("auto_mpg_model.h5")
56
+
57
+ model = load_model("auto_mpg_model.h5")
58
+
59
+ imputer = SimpleImputer(strategy='median')
60
+ scaler = StandardScaler()
61
+
62
+ def predict_mpg(data):
63
+ input_data = np.array(data).reshape(1, -1)
64
+
65
+ input_scaled = scaler.fit_transform(input_data)
66
+
67
+ prediction = model.predict(input_scaled)
68
+
69
+ return prediction[0][0]
70
+
71
+ input_data = input()
72
+ predicted_mpg = predict_mpg(input_data)
73
+ print("Predicted MPG:", predicted_mpg)