forked from Open-Deep-ML/DML-OpenProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.json
More file actions
34 lines (34 loc) · 3.45 KB
/
Copy path31.json
File metadata and controls
34 lines (34 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"id": "31",
"title": "Divide Dataset Based on Feature Threshold",
"difficulty": "medium",
"category": "Machine Learning",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/eriklindernoren/ML-From-Scratch",
"name": "Erik Linder-Norén"
}
],
"description": "Write a Python function to divide a dataset based on whether the value of a specified feature is greater than or equal to a given threshold. The function should return two subsets of the dataset: one with samples that meet the condition and another with samples that do not.",
"learn_section": "\n## Understanding Dataset Division Based on Feature Threshold\n\nDividing a dataset based on a feature threshold is a common operation in machine learning, especially in decision tree algorithms. This technique helps in creating splits that can be used for further processing or model training.\n\n### Problem Overview\nIn this problem, you will write a function to split a dataset based on whether the value of a specified feature is greater than or equal to a given threshold. You'll need to create two subsets:\n- One for samples that meet the condition (values greater than or equal to the threshold).\n- Another for samples that do not meet the condition.\n\n### Importance\nThis method is crucial for algorithms that rely on data partitioning, such as decision trees and random forests. By splitting the data, the model can create rules to make predictions based on the threshold values of certain features.",
"starter_code": "import numpy as np\n\ndef divide_on_feature(X, feature_i, threshold):\n\t# Your code here\n\tpass",
"solution": "import numpy as np\n\ndef divide_on_feature(X, feature_i, threshold):\n # Define the split function based on the threshold type\n split_func = None\n if isinstance(threshold, int) or isinstance(threshold, float):\n # For numeric threshold, check if feature value is greater than or equal to the threshold\n split_func = lambda sample: sample[feature_i] >= threshold\n else:\n # For non-numeric threshold, check if feature value is equal to the threshold\n split_func = lambda sample: sample[feature_i] == threshold\n\n # Create two subsets based on the split function\n X_1 = np.array([sample for sample in X if split_func(sample)])\n X_2 = np.array([sample for sample in X if not split_func(sample)])\n\n # Return the two subsets\n return [X_1, X_2]\n ",
"example": {
"input": "X = np.array([[1, 2], \n [3, 4], \n [5, 6], \n [7, 8], \n [9, 10]])\n feature_i = 0\n threshold = 5",
"output": "[array([[ 5, 6],\n [ 7, 8],\n [ 9, 10]]), \n array([[1, 2],\n [3, 4]])]",
"reasoning": "The dataset X is divided based on whether the value in the 0th feature (first column) is greater than or equal to 5. Samples with the first column value >= 5 are in the first subset, and the rest are in the second subset."
},
"test_cases": [
{
"test": "print(divide_on_feature(np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]), 0, 5))",
"expected_output": "[array([[ 5, 6], [ 7, 8], [ 9, 10]]), array([[1, 2], [3, 4]])]"
},
{
"test": "print(divide_on_feature(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]), 1, 3))",
"expected_output": "[array([[3, 3], [4, 4]]), array([[1, 1], [2, 2]])]"
}
]
}