栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有索引的Scikit学习train_test_split

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有索引的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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号