Python Keras | Transformation to multilabel classification

Hi. I’m trying to implement a neural network in Python (Keras) that will predict the probability of multiple outcomes. There is no need to delve into the essence of the classes themselves, so I reduced the code to 3 input and 3 outgoing, in fact there …


This content originally appeared on DEV Community and was authored by F1-bot

Hi. I'm trying to implement a neural network in Python (Keras) that will predict the probability of multiple outcomes. There is no need to delve into the essence of the classes themselves, so I reduced the code to 3 input and 3 outgoing, in fact there are more of them.

At the moment I have the following code:

import keras as k
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data_frame = pd.read_csv("123.csv")
input_names = ["Sex", "Age", "IQ"]
output_names = ["OUTPUT1", "OUTPUT2", "OUTPUT3"]

raw_input_data = data_frame[input_names]
raw_output_data = data_frame[output_names]

max_age = 100
encoders = {"Age": lambda age: [age/max_age],
"Sex": lambda gen: {"male": [0], "female": [1]}.get(gen),
"IQ": lambda iq_value: [iq_value],
"OUTPUT1": lambda output1_value: [output1_value],
"OUTPUT2": lambda output2_value: [output2_value],
"OUTPUT3": lambda output3_value: [output3_value]}

def dataframe_to_dict(df):
result = dict()
for column in df.columns:
values = data_frame[column].values
result[column] = values
return result

def make_supervised(df):
raw_input_data = data_frame[input_names]
raw_output_data = data_frame[output_names]
return {"inputs": dataframe_to_dict(raw_input_data),
"outputs": dataframe_to_dict(raw_output_data)}

def encode(data):
vectors = []
for data_name, data_values in data.items():
encoded = list(map(encoders[data_name], data_values))
vectors.append(encoded)
formatted = []
for vector_raw in list(zip(*vectors)):
vector = []
for element in vector_raw:
for e in element:
vector.append(e)
formatted.append(vector)
return formatted

supervised = make_supervised(data_frame)
encoded_inputs = np.array(encode(supervised["inputs"]))
encoded_outputs = np.array(encode(supervised["outputs"]))

train_x = encoded_inputs[:300]
train_y = encoded_outputs[:300]

test_x = encoded_inputs[300:]
test_y = encoded_outputs[300:]

model = k.Sequential()
model.add(k.layers.Dense(units=5, activation="relu"))
model.add(k.layers.Dense(units=1, activation="sigmoid"))
model.compile(loss="mse", optimizer="sgd", metrics=["accuracy"])

fit_results = model.fit(x=train_x, y=train_y, epochs=100, validation_split=0.2)

plt.title("Losses train/validation")
plt.plot(fit_results.history["loss"], label="Train")
plt.plot(fit_results.history["val_loss"], label="Validation")
plt.legend()
plt.show()

plt.title("Accuracies train/validation")
plt.plot(fit_results.history["accuracy"], label="Train")
plt.plot(fit_results.history["val_accuracy"], label="Validation")
plt.legend()
plt.show()

predicted_test = model.predict(test_x)
real_data = data_frame.iloc[300:][input_names+output_names]
real_data["POUTPUT1", "POUTPUT2", "POUTPUT3"] = predicted_test
print(real_data)
real_data.to_csv('C:/***/133.csv')

Help implement the output of probabilities for all 3 outcomes [POUTPUT1, POUTPUT2, POUTPUT3] to the console (currently outputs only 1 predicate). Saving to a new table should also save 3 new predicates.

Visually, I'm counting on this representation of the result
Image description

I will be very grateful for your help.


This content originally appeared on DEV Community and was authored by F1-bot


Print Share Comment Cite Upload Translate Updates
APA

F1-bot | Sciencx (2021-12-19T21:47:39+00:00) Python Keras | Transformation to multilabel classification. Retrieved from https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/

MLA
" » Python Keras | Transformation to multilabel classification." F1-bot | Sciencx - Sunday December 19, 2021, https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/
HARVARD
F1-bot | Sciencx Sunday December 19, 2021 » Python Keras | Transformation to multilabel classification., viewed ,<https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/>
VANCOUVER
F1-bot | Sciencx - » Python Keras | Transformation to multilabel classification. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/
CHICAGO
" » Python Keras | Transformation to multilabel classification." F1-bot | Sciencx - Accessed . https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/
IEEE
" » Python Keras | Transformation to multilabel classification." F1-bot | Sciencx [Online]. Available: https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/. [Accessed: ]
rf:citation
» Python Keras | Transformation to multilabel classification | F1-bot | Sciencx | https://www.scien.cx/2021/12/19/python-keras-transformation-to-multilabel-classification/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.