Spaces:
Build error
Build error
| import pandas as pd | |
| import numpy as np | |
| import tensorflow as tf | |
| import streamlit as st | |
| # Input your X values for prediction (a NumPy array)-------------------- | |
| #TESTCODEDELETELATER | |
| options = ['Type', 'BST', 'HP', 'Attack', 'Defense', 'Sp. Attack', 'Sp. Defense', 'Speed'] | |
| # Create the multiselect | |
| selected_options = st.multiselect('Select which stats you already have (Pick 2-7):', options) | |
| input_feature_length = len(selected_options) | |
| loaded_model = tf.keras.models.load_model(f'Pokemon_Trained_Models/{input_feature_length}feature_model.HDF5') | |
| X_values = np.zeros((1,int(input_feature_length)+8)) | |
| options_to_positions = { | |
| 'Type': 0 | |
| 'BST': 1 | |
| 'HP': 2 | |
| 'Attack': 3 | |
| 'Defense': 4 | |
| 'Sp.Attack': 5 | |
| 'Sp.Defense': 6 | |
| 'Speed': 7 | |
| } | |
| #YOU NEED TO SPECIFY THE LOCATION OF EACH INPUT IN HERE VVVVV | |
| for i in selected_options: | |
| counter += 1 | |
| X_values[counter-1] = st.text_input(f'What is the value for {i}?') | |
| st.write('Your values:', X_values[(options_to_positions[i])]) | |
| #-==================================================================================================================================== | |
| types_to_numbers = { | |
| 'GRASS': 0.056, | |
| 'FIRE': 0.112, | |
| 'WATER':0.168, | |
| 'BUG': 0.224, | |
| 'NORMAL': 0.28, | |
| 'DARK': 0.336, | |
| 'POISON': 0.392, | |
| 'ELECTRIC': 0.448, | |
| 'GROUND': 0.504, | |
| 'ICE': 0.56, | |
| 'FAIRY': 0.616, | |
| 'STEEL': 0.672, | |
| 'FIGHTING': 0.728, | |
| 'PSYCHIC': 0.784, | |
| 'ROCK': 0.84, | |
| 'GHOST': 0.896, | |
| 'DRAGON': 0.952, | |
| 'FLYING': 1.008 | |
| } | |
| # Define the file path | |
| file_path = "pokemon.csv" | |
| df = pd.read_csv(file_path) | |
| columns_to_delete = ['Dex No', 'Name', 'Base Name', 'Type 2'] | |
| df = df.drop(columns=columns_to_delete) | |
| #Copy dataframe for future refernce: | |
| old_df = df.copy() | |
| #-==================================================================================================================================== | |
| # Extract indices of '1' values----------------------------------------- | |
| indices_of_ones = [index for index, value in enumerate(X_values[0]) if value == 1] | |
| # Changing values so they fit their appropriate column | |
| for i in range(len(indices_of_ones)): | |
| indices_of_ones[i] -= input_feature_length-1 | |
| print("Indices of '1' values:", indices_of_ones) | |
| # Use the '1' values to find what the export columns will be--- | |
| full_range = list(range(1, 9)) | |
| # Find the missing indices by taking the set difference | |
| missing_indices = list(set(full_range) - set(indices_of_ones)) | |
| missing_indices.sort() | |
| print(missing_indices) | |
| # Use the model to make predictions------------------------------------- | |
| Y_values = loaded_model.predict(X_values) | |
| List_Y_values = Y_values[0].tolist() | |
| print(f"Raw Output Values: {List_Y_values}") | |
| # Initializing 'converted_Y_values' to be same length as 'Y_values' | |
| converted_Y_values = [None] * (len(missing_indices)) | |
| #_------------ Making values readable (undoing min-max and other scaling) | |
| if 1 in indices_of_ones: | |
| converted_Y_values = [None] * (len(missing_indices)) | |
| #'len(missing_indices)' is the length of the 'missing_indices' | |
| for i in range(len(missing_indices)): | |
| # Defining Min-Max Values, using the indicies value to match elements | |
| min_max_values = { | |
| 2: [old_df['BST'].min(), old_df['BST'].max()], | |
| 3: [old_df['HP'].min(), old_df['HP'].max()], | |
| 4: [old_df['Attack'].min(), old_df['Attack'].max()], | |
| 5: [old_df['Defense'].min(), old_df['Defense'].max()], | |
| 6: [old_df['Sp. Attack'].min(), old_df['Sp. Attack'].max()], | |
| 7: [old_df['Sp. Defense'].min(), old_df['Sp. Defense'].max()], | |
| 8: [old_df['Speed'].min(), old_df['Speed'].max()] | |
| } | |
| temp_min_max = min_max_values[missing_indices[i]] | |
| # Undo the min-max scaling | |
| converted_Y_values[i] = List_Y_values[i] * (temp_min_max[1] - temp_min_max[0]) + temp_min_max[0] | |
| else: | |
| converted_Y_values = [None] * (len(missing_indices) - 1) | |
| # Converting 'Type 1' back to readable type | |
| type_output_indicator = True | |
| closest_type = None | |
| min_difference = float('inf') | |
| # Finding closest type for the value | |
| for pokemon_type, value in types_to_numbers.items(): | |
| difference = abs(Y_values[0,0] - value) | |
| if difference < min_difference: | |
| min_difference = difference | |
| closest_type = pokemon_type | |
| # Delete the '1' from missing indices because it's dealt with above | |
| del missing_indices[0] | |
| del List_Y_values[0] | |
| for i in range(len(missing_indices)): | |
| # Defining Min-Max Values, using the indicies value to match elements | |
| min_max_values = { | |
| 2: [old_df['BST'].min(), old_df['BST'].max()], | |
| 3: [old_df['HP'].min(), old_df['HP'].max()], | |
| 4: [old_df['Attack'].min(), old_df['Attack'].max()], | |
| 5: [old_df['Defense'].min(), old_df['Defense'].max()], | |
| 6: [old_df['Sp. Attack'].min(), old_df['Sp. Attack'].max()], | |
| 7: [old_df['Sp. Defense'].min(), old_df['Sp. Defense'].max()], | |
| 8: [old_df['Speed'].min(), old_df['Speed'].max()] | |
| } | |
| temp_min_max = min_max_values[missing_indices[i]] | |
| # Undo the min-max scaling | |
| converted_Y_values[i] = List_Y_values[i] * (temp_min_max[1] - temp_min_max[0]) + temp_min_max[0] | |
| print(f'Converted Pokemon Type: {closest_type}') | |
| print(f"Converted Output Values: {converted_Y_values}") |