feat: fixed train.py and predictions.py

This commit is contained in:
Jakub Zaręba 2023-05-10 14:08:26 +02:00
parent 4a7ae9e7bc
commit 50e649b7fc
7 changed files with 97195 additions and 48742 deletions

48565
data.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,34 +0,0 @@
import tensorflow as tf
import pandas as pd
train_data = pd.read_csv('olympics-124-years-datasettill-2020/Athletes_winter_games.csv')
X_train = train_data[['Sex']]
y_train = train_data['Medal']
X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})
y_train = y_train.map({'Bronze': 0, 'Silver': 1, 'Gold': 1}).fillna(0).astype('float32')
X_train = X_train.astype('float32')
y_train = y_train.astype('float32')
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu', input_shape=(X_train.shape[1],)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10)
model.save('model.h5')
test_data = pd.read_csv('olympics-124-years-datasettill-2020/Athletes_winter_games.csv')
test_data.loc[:, 'Sex'] = test_data['Sex'].map({'M': 0, 'F': 1})
test_data = test_data[['Sex']].astype('float32')
predictions = model.predict(test_data)
pd.DataFrame(predictions).to_csv('predictions.csv', index=False, header=False)

View File

@ -1,144 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\kmjay\\AppData\\Local\\Temp\\ipykernel_17164\\3575846689.py:9: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})\n",
"C:\\Users\\kmjay\\AppData\\Local\\Temp\\ipykernel_17164\\3575846689.py:9: DeprecationWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`\n",
" X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1518/1518 [==============================] - 2s 758us/step - loss: 0.3609 - accuracy: 0.9112\n",
"Epoch 2/10\n",
"1518/1518 [==============================] - 1s 726us/step - loss: 0.2763 - accuracy: 0.9216\n",
"Epoch 3/10\n",
"1518/1518 [==============================] - 1s 731us/step - loss: 0.2751 - accuracy: 0.9216\n",
"Epoch 4/10\n",
"1518/1518 [==============================] - 1s 725us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 5/10\n",
"1518/1518 [==============================] - 1s 733us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 6/10\n",
"1518/1518 [==============================] - 1s 733us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 7/10\n",
"1518/1518 [==============================] - 1s 729us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 8/10\n",
"1518/1518 [==============================] - 1s 728us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 9/10\n",
"1518/1518 [==============================] - 1s 727us/step - loss: 0.2750 - accuracy: 0.9216\n",
"Epoch 10/10\n",
"1518/1518 [==============================] - 1s 755us/step - loss: 0.2750 - accuracy: 0.9216\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"import pandas as pd\n",
"\n",
"train_data = pd.read_csv('olympics-124-years-datasettill-2020/Athletes_winter_games.csv')\n",
"\n",
"X_train = train_data[['Sex']]\n",
"y_train = train_data['Medal']\n",
"\n",
"X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})\n",
"y_train = y_train.map({'Bronze': 0, 'Silver': 1, 'Gold': 1}).fillna(0).astype('float32')\n",
"\n",
"X_train = X_train.astype('float32')\n",
"y_train = y_train.astype('float32')\n",
"\n",
"model = tf.keras.Sequential([\n",
" tf.keras.layers.Dense(16, activation='relu', input_shape=(X_train.shape[1],)),\n",
" tf.keras.layers.Dense(1, activation='sigmoid')\n",
"])\n",
"\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, y_train, epochs=10)\n",
"\n",
"model.save('model.h5')"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 170/1518 [==>...........................] - ETA: 0s"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\kmjay\\AppData\\Local\\Temp\\ipykernel_17164\\2746302769.py:3: DeprecationWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`\n",
" test_data.loc[:, 'Sex'] = test_data['Sex'].map({'M': 0, 'F': 1})\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1518/1518 [==============================] - 1s 574us/step\n"
]
}
],
"source": [
"test_data = pd.read_csv('olympics-124-years-datasettill-2020/Athletes_winter_games.csv')\n",
"\n",
"test_data.loc[:, 'Sex'] = test_data['Sex'].map({'M': 0, 'F': 1})\n",
"test_data = test_data[['Sex']].astype('float32')\n",
"\n",
"predictions = model.predict(test_data)\n",
"\n",
"pd.DataFrame(predictions).to_csv('predictions.csv', index=False, header=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

BIN
model.h5 Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

19
predictions.py Normal file
View File

@ -0,0 +1,19 @@
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
model = tf.keras.models.load_model('model.h5')
data = pd.read_csv('data.csv', sep=';')
data = pd.get_dummies(data, columns=['Sex', 'Medal'])
data = data.drop(columns=['Name', 'Team', 'NOC', 'Games', 'Year', 'Season', 'City', 'Sport', 'Event'])
scaler = MinMaxScaler()
data = pd.DataFrame(scaler.fit_transform(data), columns=data.columns)
X_test = data.filter(regex='Sex|Age')
predictions = model.predict(X_test)
pd.DataFrame(predictions).to_csv('predictions.csv', index=False)

46
train.py Normal file
View File

@ -0,0 +1,46 @@
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=42)
data = pd.read_csv('data.csv', sep=';')
print('Total rows:', len(data))
print('Rows with medal:', len(data.dropna(subset=['Medal'])))
data = pd.get_dummies(data, columns=['Sex', 'Medal'])
data = data.drop(columns=['Name', 'Team', 'NOC', 'Games', 'Year', 'Season', 'City', 'Sport', 'Event'])
scaler = MinMaxScaler()
data = pd.DataFrame(scaler.fit_transform(data), columns=data.columns)
X = data.filter(regex='Sex|Age')
y = data.filter(regex='Medal')
y = pd.get_dummies(y)
X = X.fillna(0)
y = y.fillna(0)
y = y.values
X_resampled, y_resampled = smote.fit_resample(X, y)
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=42)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(64, input_dim=X_train.shape[1], activation='relu'))
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=32)
loss, accuracy = model.evaluate(X_test, y_test)
print('Test accuracy:', accuracy)
model.save('model.h5')