2021-04-25 21:54:59 +02:00
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
import tensorflow as tf
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
from tensorflow import keras
|
|
|
|
from tensorflow.keras import layers
|
|
|
|
from tensorflow.keras.layers.experimental import preprocessing
|
|
|
|
|
|
|
|
pd.set_option("display.max_columns", None)
|
|
|
|
|
|
|
|
# Wczytanie danych
|
2021-05-14 01:22:28 +02:00
|
|
|
train_data = pd.read_csv("./MoviesOnStreamingPlatforms_updated.train")
|
2021-05-15 03:38:17 +02:00
|
|
|
test_data = pd.read_csv("./MoviesOnStreamingPlatforms_updated.test")
|
2021-04-25 21:54:59 +02:00
|
|
|
|
|
|
|
# Stworzenie modelu
|
|
|
|
columns_to_use = ['Year', 'Runtime', 'Netflix']
|
|
|
|
train_X = tf.convert_to_tensor(train_data[columns_to_use])
|
|
|
|
train_Y = tf.convert_to_tensor(train_data[["IMDb"]])
|
|
|
|
test_X = tf.convert_to_tensor(test_data[columns_to_use])
|
|
|
|
test_Y = tf.convert_to_tensor(test_data[["IMDb"]])
|
|
|
|
|
|
|
|
normalizer = preprocessing.Normalization(input_shape=[3,])
|
|
|
|
normalizer.adapt(train_X)
|
|
|
|
|
2021-04-25 22:19:56 +02:00
|
|
|
if os.path.isdir('linear_regression'):
|
2021-04-25 21:54:59 +02:00
|
|
|
model = keras.models.load_model('linear_regression')
|
|
|
|
else:
|
|
|
|
model = keras.Sequential([
|
|
|
|
keras.Input(shape=(len(columns_to_use),)),
|
|
|
|
normalizer,
|
|
|
|
layers.Dense(30, activation='relu'),
|
|
|
|
layers.Dense(10, activation='relu'),
|
|
|
|
layers.Dense(25, activation='relu'),
|
|
|
|
layers.Dense(1)
|
|
|
|
])
|
|
|
|
|
|
|
|
model.compile(loss='mean_absolute_error',
|
|
|
|
optimizer=tf.keras.optimizers.Adam(0.001))
|
|
|
|
|
|
|
|
model.fit(train_X, train_Y, verbose=0, epochs=100)
|
|
|
|
|
|
|
|
model.save('linear_regression')
|
|
|
|
|
|
|
|
# Predykcja na danych testowych
|
|
|
|
results = model.predict(test_X)
|
|
|
|
|
|
|
|
# Zapis danych do pliku
|
2021-05-15 03:22:57 +02:00
|
|
|
with open("results_lab5.txt", 'w') as file:
|
2021-04-25 21:54:59 +02:00
|
|
|
for result in results:
|
|
|
|
file.writelines(str(result[0]) + "\n")
|