使用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)
female viagra in india price viagra for woman viagra price comparison
keflex on empty stomach how long can you take cephalexin after expiration date keflex drug class
cephalexin and alchol missed antibiotic dose keflex what generation is cephalexin
keflex eye infection how long does it take for keflex to start working is keflex safe if allergic to penicillin
cephalexin dosage dog cephalexin and doxycycline together does keflex kill h. pylori
amoxicillin dosage calculator can you take melatonin with amoxicillin will amoxicillin treat gonorrhea
amoxicillin 250mg/5ml dosage for child amoxicillin 500 mg tablet reaction to amoxicillin rash
amoxicillin potassium clavulanate dosage how often can you take amoxicillin amoxicillin for ear infection dosage
amoxicillin buy online methotrexate and amoxicillin can you overdose on amoxicillin
amoxicillin pink pill amoxicillin 500 mg dosage for strep throat amoxicillin for humans
augmentin una volta al giorno augmentin nyquil augmentin tablet sizes
augmentin duo 400/57 augmentin ceny augmentin ciclo mestruale
how much does prednisone cost prednisone makes me tired prednisone pregnancy
ciprofloxacin generic can you drink alcohol while taking ciprofloxacin tablets ciprofloxacin ear infection
5 day prednisone dosage 20mg heart palpitations prednisone withdrawal can i drink alcohol while taking prednisone
ciprofloxacin pneumonia ciprofloxacin and tums ciprofloxacin klebsiella
doxycycline para que es can doxycycline make you dizzy lying down after doxycycline
doxycycline for skin infection how fast does doxycycline work for chlamydia does doxycycline treat std
how long after taking azithromycin can you take azithromycin if you are allergic to penicillin azithromycin for covid pneumonia
does cephalexin treat ear infections in dogs can cephalexin be taken with milk cephalexin alternative over the counter
cheap tadalafil 20mg daily tadalafil cialis manufacturer coupon lilly
buy viagra 100mg purchase viagra with paypal buy sildenafil us
sildenafil tablets india cheap sildenafil 50mg order sildenafil india
generic viagra online pharmacy usa where to buy cheap viagra in canada sildenafil soft tabs
what are the side effects of cialis cialis for bph insurance coverage buy cialis online
tadalafil (megalis-macleods) 10 mg cialis 20mg price buy black cialis
essay on leader service to others essay argument essay rubric
how to write a compare and contrast essay how to end a college essay online essay editing services
how to quote in an essay how to write a scholarship essay 100 word essay
five paragraph essay essay service how to start an introduction for an essay
essay transition words what can i write my essay on cheapest essay writing services
persuasive essay topics need help writing scholarship essay best custom essay writing services
the crucible essay essay sentence starters how many paragraphs are in a essay
common app essay questions professional grad school essay writers abortion pro life essay
college essay outline how to structure an essay write my college essay
compare and contrast essay help how to write a thesis statement for an argumentative essay racial profiling essay
peoples pharmacy mexican pharmacy weight loss viagra tesco pharmacy
envision rx pharmacy wegmans pharmacy free atorvastatin mobic online pharmacy
rx online pharmacy animal pharmacy online best online pharmacy viagra review
Apcalis SX buy concerta online pharmacy hong kong pharmacy ambien
thrifty drug store internet pharmacy manitoba viagra cost pharmacy
generic viagra india pharmacy indian pharmacy vicodin vicodin cheap pharmacy
online pharmacy india phentermine pfizer lipitor pharmacy Cardizem
microdosing tadalafil tadalafil skin rash tadalafil/sildenafil combo
tadalafil maximum daily dose tadalafil side effects liver tadalafil effects on kidneys
what is the difference between tadalafil and cialis ambrisentan plus tadalafil how to take tadalafil liquid
tadalafil 20 mg reviews prix tadalafil 5mg evolution peptides tadalafil review
benefits of tadalafil daily will tadalafil show up on a drug test evolution peptides tadalafil review
tadalafil before workout compounded tadalafil can tadalafil cause heartburn
potenzmittel tadalafil 5mg half life of tadalafil tadalafil increase blood pressure
tadalafil buy uk what is tadalafil 5mg used for how long is tadalafil in your system
can you take nitric oxide with tadalafil generic tadalafil 20mg can tadalafil cause high blood pressure
can you cut a 20mg tadalafil in half tadalafil review forum free tadalafil
how fast does tadalafil work does tadalafil increase libido tadalafil ingredients
tadalafil after prostate surgery how long does tadalafil 20mg last tadalafil deutschland
difference between viagra and tadalafil rasa research tadalafil reviews tadalafil oxytocin pt-141
tadalafil tab 20mg can you take 10 mg of tadalafil daily can i buy tadalafil over the counter
which is better tadalafil or vardenafil tadalafil brands how many tadalafil should i take
tadalafil 20mg dosage can tamsulosin and tadalafil be taken together donde comprar tadalafil sin receta
goodrx tadalafil prices tadalafil and lisinopril tadalafil 36 hours
tadalafil 10 mg daily tadalafil 5mg generic tadalafil vs sildenafil pulmonary hypertension