ium_434765/neural_network.py

62 lines
1.7 KiB
Python
Raw Normal View History

2021-04-24 21:18:57 +02:00
import pandas as pd
2021-04-24 22:23:04 +02:00
import numpy as np
2021-05-17 19:24:30 +02:00
from keras import optimizers
2021-04-24 22:23:04 +02:00
from tensorflow import keras
2021-04-24 21:18:57 +02:00
2021-05-17 19:24:30 +02:00
def normalize_data(data):
return (data - np.min(data)) / (np.max(data) - np.min(data))
2021-04-24 22:23:04 +02:00
data = pd.read_csv("data_train", sep=',', error_bad_lines=False).dropna()
X = data.loc[:,data.columns == "2805317"].astype(int)
y = data.loc[:,data.columns == "198909"].astype(int)
2021-05-17 19:24:30 +02:00
min_val_sub = np.min(X)
max_val_sub = np.max(X)
X = (X - min_val_sub) / (max_val_sub - min_val_sub)
print(min_val_sub)
print(max_val_sub)
2021-04-24 22:23:04 +02:00
2021-05-17 19:24:30 +02:00
min_val_like = np.min(y)
max_val_like = np.max(y)
y = (y - min_val_like) / (max_val_like - min_val_like)
2021-04-24 22:23:04 +02:00
2021-05-17 19:24:30 +02:00
print(min_val_like)
print(max_val_like)
2021-04-24 22:23:04 +02:00
model = keras.Sequential([
2021-05-17 18:56:39 +02:00
keras.layers.Dense(32,input_dim = X.shape[1], activation='relu'),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(1,activation='relu'),
2021-04-24 22:23:04 +02:00
])
2021-05-17 19:24:30 +02:00
model.compile(loss='mean_absolute_error', optimizer="Adam", metrics=['mean_absolute_error'])
2021-04-24 22:23:04 +02:00
2021-05-17 18:48:14 +02:00
model.fit(X, y, epochs=15, validation_split = 0.3)
2021-04-24 22:23:04 +02:00
data = pd.read_csv("data_test", sep=',', error_bad_lines=False).dropna()
X_test = data.loc[:,data.columns == "2805317"].astype(int)
y_test = data.loc[:,data.columns == "198909"].astype(int)
2021-05-17 19:24:30 +02:00
min_val_sub = np.min(X_test)
max_val_sub = np.max(X_test)
X_test = (X_test - min_val_sub) / (max_val_sub - min_val_sub)
print(min_val_sub)
print(max_val_sub)
min_val_like = np.min(y_test)
max_val_like = np.max(y_test)
y_test = (y_test - min_val_like) / (max_val_like - min_val_like)
print(min_val_like)
print(max_val_like)
2021-04-24 22:23:04 +02:00
prediction = model.predict(X_test)
f = open("predictions.txt", "w")
for (pred, test) in zip(prediction, y_test.values):
f.write("predicted: %s expected: %s\n" % (str(pred), str(test)))