2023-06-30 19:12:47 +02:00
|
|
|
import pandas as pd
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
import tensorflow as tf
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
# Wczytanie danych z pliku CSV
|
2023-06-30 20:35:10 +02:00
|
|
|
data = pd.read_csv('data/winemag-data_first150k.csv')
|
2023-06-30 19:12:47 +02:00
|
|
|
|
|
|
|
# Podział danych na zbiory treningowe, deweloperskie (validation) i testowe
|
|
|
|
train_data, temp_data = train_test_split(data, test_size=0.2, random_state=42)
|
|
|
|
dev_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)
|
|
|
|
|
|
|
|
# Zapisanie danych do osobnych plików CSV
|
|
|
|
train_data.to_csv('train_data.csv', index=False)
|
|
|
|
dev_data.to_csv('dev_data.csv', index=False)
|
|
|
|
test_data.to_csv('test_data.csv', index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Wczytanie danych treningowych
|
|
|
|
# train_data = pd.read_csv('train_data.csv')
|
|
|
|
# dev_data = pd.read_csv('dev_data.csv')
|
|
|
|
|
|
|
|
# Przygotowanie danych wejściowych i wyjściowych dla treningu
|
|
|
|
train_input_data = train_data[['price', 'points']] # Wejście modelu (kolumny 'price' i 'points')
|
|
|
|
train_output_data = train_data['price'] # Oczekiwane wyjście modelu (kolumna 'price')
|
|
|
|
|
|
|
|
# Przygotowanie danych wejściowych dla predykcji
|
|
|
|
dev_input_data = dev_data[['price', 'points']] # Wejście modelu dla predykcji (kolumny 'price' i 'points')
|
|
|
|
|
|
|
|
# Konwersja danych do numpy arrays
|
|
|
|
train_input_array = train_input_data.values
|
|
|
|
train_output_array = train_output_data.values
|
|
|
|
dev_input_array = dev_input_data.values
|
|
|
|
|
|
|
|
# Normalizacja danych wejściowych
|
|
|
|
input_mean = np.mean(train_input_array, axis=0)
|
|
|
|
input_std = np.std(train_input_array, axis=0)
|
|
|
|
train_input_array = (train_input_array - input_mean) / input_std
|
|
|
|
dev_input_array = (dev_input_array - input_mean) / input_std
|
|
|
|
|
|
|
|
# Utworzenie modelu przy użyciu TensorFlow
|
|
|
|
input_size = train_input_array.shape[1]
|
|
|
|
output_size = 1 # Jeden wymiar dla wyjścia modelu (kolumna 'price')
|
|
|
|
|
|
|
|
model = tf.keras.models.Sequential([
|
|
|
|
tf.keras.layers.Dense(64, activation='relu', input_shape=(input_size,)),
|
|
|
|
tf.keras.layers.Dense(64, activation='relu'),
|
|
|
|
tf.keras.layers.Dense(output_size)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Kompilacja modelu
|
|
|
|
model.compile(optimizer='adam', loss='mse')
|
|
|
|
|
|
|
|
# Uczenie modelu
|
|
|
|
model.fit(train_input_array, train_output_array, epochs=100, batch_size=32, verbose=1)
|
|
|
|
|
|
|
|
model.save('trained_model_tf.h5')
|