带有索引的Scikit学习train_test_split

带有索引的Scikit学习train_test_split,第1张

带有索引的Scikit学习train_test_split

Scikit learning在Pandas上的表现非常好,因此我建议您使用它。这是一个例子:

In [1]: import pandas as pdimport numpy as npfrom sklearn.model_selection import train_test_splitdata = np.reshape(np.random.randn(20),(10,2)) # 10 training exampleslabels = np.random.randint(2, size=10) # 10 labelsIn [2]: # Giving columns in X a nameX = 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_testOut[4]:     Column_1    Column_22   -1.39       -1.868    0.48       -0.814   -0.10       -1.83In [5]: y_testOut[5]:2    18    14    1dtype: int32

您可以直接在Dataframe / Series上调用任何scikit函数,它将起作用

假设您要进行LogisticRegression,以下是一种以一种不错的方式检索系数的方法:

In [6]: from sklearn.linear_model import LogisticRegressionmodel = 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_coefsOut[6]: CoefficientColumn_1    0.076987Column_2    -0.352463


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5645181.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存