Python Beginner level [closed]

I created a model that can predict the “decision” of a person in a game of RSP(rock, scissors,paper) by looking at the “gender” and “age”.
I have this excel file that consists of 3 columns(gender, age, decision).
So now I want this model to play RSP against a person and make correct decision considering their age and gender.

CAN SOMEONE PLEASE HELP ME??

DATA:

age gender  D
0   20  0   rock
1   18  0   rock
2   17  0   rock
3   16  0   rock
4   15  0   rock
5   14  0   rock
6   25  0   rock
7   30  0   paper
8   31  0   paper
9   32  0   paper
10  33  0   paper
11  34  0   paper
12  14  1   scissors
13  15  1   scissors
14  16  1   scissors
15  17  1   scissors
16  18  1   scissors
17  19  1   scissors
18  20  1   scissors
19  26  1   paper
20  30  1   paper
21  32  1   paper
22  35  1   paper
23  36  1   paper
24  37  1   paper

This is my code and i get error that gender is not defined:

`import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data from the Excel file
file_path="decision.csv.xlsx"
game_data = pd.read_excel(file_path)

# Separate features (X) and target variable (y)
X = game_data.drop(columns=['D'])
y = game_data['D']

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

# Initialize the Decision Tree model
model = DecisionTreeClassifier()

# Train the model on the training data
model.fit(X_train, y_train)

# Make predictions on the testing data
predictions = model.predict(X_test)

# Evaluate the model's accuracy
score = accuracy_score(y_test, predictions)
print(f'Model Accuracy: {score:.2%}')

# Function to play rock-paper-scissors against the trained model
def play_rps_against_model(age, gender):
    input_data = pd.DataFrame({'age': [age], 'gender': [gender]})
    
    # Make a prediction using the trained model
    prediction = model.predict(input_data)
    
    return prediction[0]

# User input for age and gender
while True:
    age = input("Enter your age: ")
    gender = input("Enter gender (0 for male, 1 for female): ")
    
    try:
        age = int(age)
        gender = int(gender)
        
        if gender not in [0, 1]:
            print("Please enter '0' for female or '1' for male.")
            continue
        
        break
    except ValueError:
        print("Invalid input. Please enter valid numeric values for age and gender.")

# Get the bot's move based on user's age and gender
bot_move = play_rps_against_model(age, gender)

print(f'The bot played: {bot_move}')

ValueError: The feature names should match those that were passed during fit.
Feature names unseen at fit time:

  • gender
    Feature names seen at fit time, yet now missing:
  • gender

  • Welcome to SO; please see how to create a minimal reproducible example.

    – 

  • Your code works for me. Please provide runnable code that reproduces the problem.

    – 




  • always put FULL error message (starting at word “Traceback”) in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback.

    – 

  • Your Title could be a little more descriptive of what your question is about.

    – 




Leave a Comment