#๐Ÿ”’ Help with Data split into train and test sets

19 messages ยท Page 1 of 1 (latest)

formal slate
#

I have an assignment due for data spliting intro train and test sets and i have to set them into three different parameters.

this is the task

You are requested to repeat all the experiments in Section 4 below using three splits of the data. You can do that by fixing the random_state parameter to 1, 20 and 40. This will generate 3 different train and test sets. Then:
a. Report the classification accuracies for each test split, as described in Section 4.
b. Report the average classification accuracies for all test splits, as described in Section 4.

ignore section four

i will post the code under here:

weary hemlockBOT
#

@formal slate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

formal slate
#

i am getting the accuracy to be 75%, is this correct?

#

the excel sheet that was provides had around 50k rows and 12 coloums

abstract dust
#

Please share your code, not screenshot

#

!code

weary hemlockBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

formal slate
#

ok

#

Step 2: Split the data into training and testing sets using different random states

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

random_states = [1, 20, 40]
accuracies = []

for random_state in random_states:
# Splitting the data
X = df[['Gender', 'Age', 'Insurance', 'Hypertension', 'Diabetes', 'Handcap', 'SMS_received', 'Year', 'Ldays']]
y = df['NoShow']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=random_state)

# Perform one-hot encoding for categorical variables
X_train = pd.get_dummies(X_train)
X_test = pd.get_dummies(X_test)

# Realign the columns in test data in case some categorical values are missing
X_test = X_test.reindex(columns=X_train.columns, fill_value=0)

# Step 3: Creating and training the Decision Tree classifier
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)

# Step 4: Making predictions
y_pred = clf.predict(X_test)

# Step 5: Evaluating the classifier's performance
accuracy = accuracy_score(y_test, y_pred)
accuracies.append(accuracy)
print(f"Classification accuracy for random state {random_state}: {accuracy}")

Step 6: Report the classification accuracies for each test split

average_accuracy = sum(accuracies) / len(accuracies)
print(f"Average classification accuracy across all test splits: {average_accuracy}")

weary hemlockBOT
#

Hey @formal slate!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
formal slate
#
import panda as pd
# Step 2: Split the data into training and testing sets using different random states
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

random_states = [1, 20, 40]
accuracies = []

for random_state in random_states:
    # Splitting the data
    X = df[['Gender', 'Age', 'Insurance', 'Hypertension', 'Diabetes', 'Handcap', 'SMS_received', 'Year', 'Ldays']]
    y = df['NoShow']
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=random_state)

    # Perform one-hot encoding for categorical variables
    X_train = pd.get_dummies(X_train)
    X_test = pd.get_dummies(X_test)

    # Realign the columns in test data in case some categorical values are missing
    X_test = X_test.reindex(columns=X_train.columns, fill_value=0)

    # Step 3: Creating and training the Decision Tree classifier
    clf = DecisionTreeClassifier()
    clf.fit(X_train, y_train)

    # Step 4: Making predictions
    y_pred = clf.predict(X_test)

    # Step 5: Evaluating the classifier's performance
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    print(f"Classification accuracy for random state {random_state}: {accuracy}")

# Step 6: Report the classification accuracies for each test split
average_accuracy = sum(accuracies) / len(accuracies)
print(f"Average classification accuracy across all test splits: {average_accuracy}")
    ```
#

but you wont be able to run the code without the excel file

sage onyx
formal slate
#

the thing is my code is mainly built out of the code that was provided from my teacher, but the only code that wasn't there was one-hot encoding and i got it from chatgpt

#

can anyone explain it to me, becuase when i was trying to run the code without it, i got an error about strings and i had to convert it to numerical so the machine can understand it

#

and if someone can even explain to me the realignment of the code under the one-hot encoding

weary hemlockBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.