(十)特征编码-Feature Encoding

  • 类别特征(categorial Feature)

    - 男(0,1)
    - 女(1,0)
    - one-hot encoding
    
  • 连续型特征

    - 身高、温度
    - A:直接用(归一化):0,1归一化、N(0,1)高斯归一化
    - B:离散化:one-hot encoding
    - 150-160  160-170  170-180
    -        1             2             3
    
  • ordinal feature(ranking)

    - 成绩:A   B    C     D
    - ordinal feature 只知道先后顺序,不能量化之间的区别
    - 连续型变量可以量化之间的区别
    - 处理:
    - 	- A:直接使用
    - 	- B:当中类别特征  A--> (1,0,0,0,0) 
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您编写一个基于决策树的最高天气预测的Python代码,但是需要先安装pandas和numpy库,您是否已经安装了这些库? 如果没有,请先通过以下命令安装: ``` pip install pandas numpy ``` 然后,您可以使用以下代码实现您的需求: ```python import pandas as pd import numpy as np # 加载数据集 data = pd.read_csv('temps.csv') # 将week列进行one-hot编码 data = pd.concat([data, pd.get_dummies(data['week'])], axis=1) data.drop(['week'], axis=1, inplace=True) # 分割数据集 train_data = data.iloc[:250, :] test_data = data.iloc[250:, :] # 定义特征列和标签列 feature_cols = ['year', 'month', 'day', 'temp_2', 'temp_1', 'average', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] label_cols = ['actual'] # 定义节点类 class Node: def __init__(self, feature=None, value=None, true_branch=None, false_branch=None, prediction=None): self.feature = feature self.value = value self.true_branch = true_branch self.false_branch = false_branch self.prediction = prediction # 定义决策树类 class DecisionTree: def __init__(self, min_samples_split=2, max_depth=5): self.min_samples_split = min_samples_split self.max_depth = max_depth # 计算数据集的方差 def variance(self, data): labels = data[label_cols].values mean = np.mean(labels) return np.mean((labels - mean) ** 2) # 选择最好的分割特征和分割值 def get_best_split(self, data): best_variance = float('inf') best_feature = None best_value = None n_features = len(feature_cols) for i in range(n_features): feature_values = data[feature_cols[i]].values unique_values = np.unique(feature_values) for value in unique_values: true_data = data[feature_values <= value] false_data = data[feature_values > value] if len(true_data) > 0 and len(false_data) > 0: variance = (len(true_data) / len(data)) * self.variance(true_data) + \ (len(false_data) / len(data)) * self.variance(false_data) if variance < best_variance: best_variance = variance best_feature = feature_cols[i] best_value = value return best_feature, best_value # 创建决策树 def create_tree(self, data, depth=0): n_samples, n_features = data.shape prediction = np.mean(data[label_cols].values) # 停止条件 if n_samples >= self.min_samples_split and depth <= self.max_depth: best_feature, best_value = self.get_best_split(data) if best_feature is not None and best_value is not None: true_data = data[data[best_feature] <= best_value] false_data = data[data[best_feature] > best_value] # 递归创建决策树 true_branch = self.create_tree(true_data, depth + 1) false_branch = self.create_tree(false_data, depth + 1) return Node(feature=best_feature, value=best_value, true_branch=true_branch, false_branch=false_branch) return Node(prediction=prediction) # 预测单个样本 def predict_sample(self, sample, node): if node.prediction is not None: return node.prediction if sample[node.feature] <= node.value: return self.predict_sample(sample, node.true_branch) else: return self.predict_sample(sample, node.false_branch) # 预测数据集 def predict(self, data, tree): predictions = [] for i in range(len(data)): predictions.append(self.predict_sample(data.iloc[i], tree)) return predictions # 创建决策树模型 tree = DecisionTree(min_samples_split=3, max_depth=3) tree_model = tree.create_tree(train_data) # 预测测试集并计算准确率 predictions = tree.predict(test_data[feature_cols], tree_model) actuals = test_data[label_cols].values accuracy = np.mean(predictions == actuals) print('Accuracy:', accuracy) ``` 这里我们使用了默认参数,即最小样本分割为2,最大深度为5,并且使用了默认的方差计算方法。您可以根据需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值