ium_487187/lab5.ipynb
2023-05-07 21:10:11 +02:00

5.2 KiB

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')
Epoch 1/10
C:\Users\kmjay\AppData\Local\Temp\ipykernel_17164\3575846689.py:9: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})
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)`
  X_train.loc[:, 'Sex'] = X_train['Sex'].map({'M': 0, 'F': 1})
1518/1518 [==============================] - 2s 758us/step - loss: 0.3609 - accuracy: 0.9112
Epoch 2/10
1518/1518 [==============================] - 1s 726us/step - loss: 0.2763 - accuracy: 0.9216
Epoch 3/10
1518/1518 [==============================] - 1s 731us/step - loss: 0.2751 - accuracy: 0.9216
Epoch 4/10
1518/1518 [==============================] - 1s 725us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 5/10
1518/1518 [==============================] - 1s 733us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 6/10
1518/1518 [==============================] - 1s 733us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 7/10
1518/1518 [==============================] - 1s 729us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 8/10
1518/1518 [==============================] - 1s 728us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 9/10
1518/1518 [==============================] - 1s 727us/step - loss: 0.2750 - accuracy: 0.9216
Epoch 10/10
1518/1518 [==============================] - 1s 755us/step - loss: 0.2750 - accuracy: 0.9216
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)
 170/1518 [==>...........................] - ETA: 0s
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)`
  test_data.loc[:, 'Sex'] = test_data['Sex'].map({'M': 0, 'F': 1})
1518/1518 [==============================] - 1s 574us/step