我试图在以下数据集上应用scikit学习决策树,目的是对数据进行分类:
感官数据:
到目前为止,我已经试着用潘达斯系列来训练我的模特。它可以工作,但决策树不能区分特征/传感器。熊猫系列是分析这样的数据的正确方法吗?还是有人有解决这个问题的办法?
发布于 2019-04-07 20:25:22
为了便于使用,您需要将2D原始传感器数据压平成一维功能。下面的代码演示了基本知识。
什么样的特征工程应用到最佳预测效果完全取决于你的传感器的性质和问题。在所提供的问题或数据中没有这方面的细节。
总的过程是:
下面是一些你应该尝试的事情:
首先关注每个传感器的单变量特征。决策树将很好地将它们结合在一起。
的拟合
import numpy
import pandas
from sklearn.ensemble import RandomForestClassifier
def get_sensor_data():
timesteps = 10
times = numpy.linspace(0.1, 1.0, timesteps)
df = pandas.DataFrame({
'time': times,
'sensor1': numpy.random.random(timesteps),
'sensor2': numpy.random.random(timesteps),
'sensor3': numpy.random.random(timesteps),
'sensor4': numpy.random.random(timesteps),
})
return df
samples = [ get_sensor_data() for _ in range(100) ]
labels = [ int(numpy.random.random() > 0.5) for _ in range(100) ]
assert len(samples) == len(labels)
print('sample from CSV file:\n', samples[0], '\nlabel', labels[0], '\n')
def to_features(data):
# remove time column
feature_columns = list(set(data.columns) - set(['time']))
# TODO: do smarter feature engineering here
sensor_values = data[feature_columns].values
# Note: the features must be 1D for scikit-learn classifiers
features = sensor_values.flatten()
assert len(features.shape) == 1, features.shape
return features
features = numpy.stack([ to_features(d) for d in samples ])
assert features.shape[0] == len(samples)
print('Features:', features.shape, '\n', features[0])
# XXX: do train/test splits etc
est = RandomForestClassifier(n_estimators=10, min_samples_leaf=0.01)
est.fit(features, labels)
示例输出
sample from CSV file:
time sensor1 sensor2 sensor3 sensor4
0 0.1 0.820667 0.346542 0.625512 0.774050
1 0.2 0.821934 0.241652 0.485608 0.188131
2 0.3 0.264697 0.780841 0.137018 0.117096
3 0.4 0.464143 0.457126 0.972894 0.600710
4 0.5 0.530302 0.027401 0.876191 0.563788
5 0.6 0.598231 0.291814 0.588032 0.143753
6 0.7 0.627435 0.036549 0.276131 0.311099
7 0.8 0.527908 0.197046 0.580293 0.123796
8 0.9 0.068682 0.880533 0.956394 0.787993
9 1.0 0.244478 0.306716 0.586049 0.373013
label 1
Features: (100, 40)
[0.82066682 0.62551234 0.77405 0.34654243 0.82193414 0.48560828
0.18813108 0.24165186 0.26469686 0.1370181 0.11709553 0.78084136
0.46414318 0.97289382 0.60070974 0.45712632 0.53030219 0.8761905
0.5637877 0.02740072 0.59823073 0.58803188 0.14375282 0.29181434
0.62743516 0.27613083 0.31109894 0.03654882 0.52790773 0.58029298
0.1237963 0.19704597 0.06868206 0.95639405 0.78799333 0.88053276
0.24447754 0.5860489 0.37301339 0.30671624]
```
https://datascience.stackexchange.com/questions/48534
复制相似问题