• 周四. 12 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Scikitlearn列车试验与指数分割

King Wang

3 月 7, 2023

使用train_test_split()时,如何获取数据的原始索引

我有以下几点

from sklearn.cross_validation import train_test_split
import numpy as np
data = np.reshape(np.randn(20),(10,2)) # 10 training examples
labels = np.random.randint(2, size=10) # 10 labels
x1, x2, y1, y2 = train_test_split(data, labels, size=0.2)

但这并没有给出原始数据的指数。
一种解决方法是将索引添加到数据中(例如data = [(i, d) for i, d in enumerate(data)]),然后将它们传递到train_test_split中,然后再次展开。
有没有更清洁的解决方案

Tags:

fromtestimportnumpydatasizelabelsasnptrainsklearnvalidationsplitcrossreshape3条回答网友

1楼 ·

编辑于 2023-03-06 23:31:12

这是一个最简单的解决方案(Jibwa在另一个答案中让它看起来很复杂),无需自己生成索引——只需使用ShuffleSplit对象生成一个分割

import numpy as np 
from sklearn.model_selection import ShuffleSplit # or StratifiedShuffleSplit
sss = ShuffleSplit(n_splits=1, test_size=0.1)

data_size = 100
X = np.reshape(np.random.rand(data_size*2),(data_size,2))
y = np.random.randint(2, size=data_size)

sss.get_n_splits(X, y)
train_index, test_index = next(sss.split(X, y)) 

X_train, X_test = X[train_index], X[test_index] 
y_train, y_test = y[train_index], y[test_index]

网友

2楼 ·

编辑于 2023-03-06 23:31:12

Scikit learn非常适合熊猫,所以我建议你使用它。下面是一个例子:

In [1]: 
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
data = np.reshape(np.random.randn(20),(10,2)) # 10 training examples
labels = np.random.randint(2, size=10) # 10 labels

In [2]: # Giving columns in X a name
X = pd.DataFrame(data, columns=['Column_1', 'Column_2'])
y = pd.Series(labels)

In [3]:
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.2, 
                                                    random_state=0)

In [4]: X_test
Out[4]:

     Column_1    Column_2
2   -1.39       -1.86
8    0.48       -0.81
4   -0.10       -1.83

In [5]: y_test
Out[5]:

2    1
8    1
4    1
dtype: int32

您可以直接调用DataFrame/Series上的任何scikit函数,它将正常工作

假设您想进行逻辑回归,下面是如何以一种好的方式检索系数:

In [6]: 
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model = model.fit(X_train, y_train)

# Retrieve coefficients: index is the feature name (['Column_1', 'Column_2'] here)
df_coefs = pd.DataFrame(model.coef_[0], index=X.columns, columns = ['Coefficient'])
df_coefs
Out[6]:
            Coefficient
Column_1    0.076987
Column_2    -0.352463

网友

3楼 ·

编辑于 2023-03-06 23:31:12

正如Julien所说,您可以使用熊猫数据帧或系列,但如果您想将自己限制为numpy,则可以传递额外的索引数组:

from sklearn.model_selection import train_test_split
import numpy as np
n_samples, n_features, n_classes = 10, 2, 2
data = np.random.randn(n_samples, n_features)  # 10 training examples
labels = np.random.randint(n_classes, size=n_samples)  # 10 labels
indices = np.arange(n_samples)
(
    data_train,
    data_test,
    labels_train,
    labels_test,
    indices_train,
    indices_test,
) = train_test_split(data, labels, indices, test_size=0.2)

发表回复