Anonymous
Можем ли мы написать код Python на любом другом языке? [закрыто]
Сообщение
Anonymous » 29 ноя 2024, 15:14
Код: Выделить всё
import pandas as pd
from scipy.stats import chi2_contingency
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny', 'Sunny', 'Rain', 'Sunny',
'Overcast', 'Overcast', 'Rain'],
'Temp.': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cold', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal',
'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak',
'Strong', 'Strong', 'Weak', 'Strong'],
'Target': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
def chaid_split(df, target_col, feature_col):
contingency_table = pd.crosstab(df[feature_col], df[target_col])
chi2, p, dof, expected = chi2_contingency(contingency_table, correction=False)
target_distribution = contingency_table.sum(axis=1) / contingency_table.sum().sum()
return chi2, p, target_distribution.to_dict()
node_counter = 0 # Initialize node counter
def build_chaid_tree(df, target_col, features, forced_root_attributes, min_samples_split=5):
global node_counter
node_counter += 1 # Increment counter for each new node
node_id = node_counter
unique_targets = df[target_col].unique()
if len(unique_targets) 0 else 0
print(f"{indent} {target}: ({percentage:.2f}%)")
else:
print(f"{indent}Attribute: {tree['attribute']}")
if 'chi2' in tree:
print(f"{indent}Chi-Square: {tree['chi2']:.3f}")
print(f"{indent}P-Value: {tree['p_value']:.3f}")
target_distribution = tree.get('target_distribution', {})
total_instances = sum(target_distribution.values())
print(f"{indent}Target Distribution:")
for target, count in target_distribution.items():
percentage = (count / total_instances) * 100 if total_instances > 0 else 0
print(f"{indent} {target}: ({percentage:.2f}%)")
for value, child in tree['children'].items():
print_tree(child, indent + " ")
tree = build_chaid_tree(df, 'Target', ['Outlook', 'Temp.', 'Humidity', 'Wind'], ['Outlook',"Wind","Humidity"], min_samples_split=3)
print_tree(tree)
Пока ничего не пробовал
Подробнее здесь:
https://stackoverflow.com/questions/792 ... r-language
1732882448
Anonymous
[code]import pandas as pd from scipy.stats import chi2_contingency data = { 'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'], 'Temp.': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cold', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'], 'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'], 'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'], 'Target': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No'] } df = pd.DataFrame(data) def chaid_split(df, target_col, feature_col): contingency_table = pd.crosstab(df[feature_col], df[target_col]) chi2, p, dof, expected = chi2_contingency(contingency_table, correction=False) target_distribution = contingency_table.sum(axis=1) / contingency_table.sum().sum() return chi2, p, target_distribution.to_dict() node_counter = 0 # Initialize node counter def build_chaid_tree(df, target_col, features, forced_root_attributes, min_samples_split=5): global node_counter node_counter += 1 # Increment counter for each new node node_id = node_counter unique_targets = df[target_col].unique() if len(unique_targets) 0 else 0 print(f"{indent} {target}: ({percentage:.2f}%)") else: print(f"{indent}Attribute: {tree['attribute']}") if 'chi2' in tree: print(f"{indent}Chi-Square: {tree['chi2']:.3f}") print(f"{indent}P-Value: {tree['p_value']:.3f}") target_distribution = tree.get('target_distribution', {}) total_instances = sum(target_distribution.values()) print(f"{indent}Target Distribution:") for target, count in target_distribution.items(): percentage = (count / total_instances) * 100 if total_instances > 0 else 0 print(f"{indent} {target}: ({percentage:.2f}%)") for value, child in tree['children'].items(): print_tree(child, indent + " ") tree = build_chaid_tree(df, 'Target', ['Outlook', 'Temp.', 'Humidity', 'Wind'], ['Outlook',"Wind","Humidity"], min_samples_split=3) print_tree(tree) [/code] Пока ничего не пробовал Подробнее здесь: [url]https://stackoverflow.com/questions/79237047/can-we-write-python-code-in-any-other-language[/url]