首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将用户定义的函数应用于数据访问

将用户定义的函数应用于数据访问
EN

Stack Overflow用户
提问于 2020-06-11 14:55:44
回答 2查看 242关注 0票数 0

函数im试图写入,将提供的数据,并计算F统计值,并提供这些作为输出。

数据格式Final

代码语言:javascript
运行
复制
Key      Color   Strength   Fabric  Sales
a         0         1         1       10
b         1         2         2       15

在这里,颜色、强度和织物是独立的,而销售是独立的。

其思想是创建一个循环,为每个唯一的键值创建一个新的dataframe :并在这个dataframe上执行一个函数,然后创建一个新的dataframe,它是从唯一键值获得的所有新数据文件的连接。

代码语言:javascript
运行
复制
def regression():
    X=Final1.copy()
    y=Final1[['Sales']].copy()
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2, random_state=0)
    sel=f_classif(X_train, y_train)
    p_values=pd.Series(sel[0], index=X_train.columns)
    p_values=p_values.reset_index()
    pd.options.display.float_format = "{:,.2f}".format
    return p_values

Finals=[]
Finals=pd.DataFrame(Finals)
for group in Final.groupby('Key'): 
    # group is a tuple where the first value is the Key and the second is the dataframe
    Final1=group[1]
    Final1=pd.DataFrame(Final1)
    result=regression()
    Finals=pd.concat([Finals, result], axis=1)


# do xyz with result

print(Finals)

这是我想出的代码,但它抛出了一个错误

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-131-c3a3b53971d5> in <module>
      5     Final1=group[1]
      6     Final1=pd.DataFrame(Final1)
----> 7     result=regression()
      8     Finals=pd.concat([Finals, result], axis=1)
      9 

<ipython-input-120-d5c718baaba8> in regression()
      2     X=Final1.iloc[:,7:-1].copy()
      3     y=Final1[['Sale Rate']].copy()
----> 4     X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2, random_state=0)
      5     sel=f_classif(X_train, y_train)
      6     p_values=pd.Series(sel[0], index=X_train.columns)

~\anaconda3\lib\site-packages\sklearn\model_selection\_split.py in train_test_split(*arrays, **options)
   2120     n_samples = _num_samples(arrays[0])
   2121     n_train, n_test = _validate_shuffle_split(n_samples, test_size, train_size,
-> 2122                                               default_test_size=0.25)
   2123 
   2124     if shuffle is False:

~\anaconda3\lib\site-packages\sklearn\model_selection\_split.py in _validate_shuffle_split(n_samples, test_size, train_size, default_test_size)
   1803             'resulting train set will be empty. Adjust any of the '
   1804             'aforementioned parameters.'.format(n_samples, test_size,
-> 1805                                                 train_size)
   1806         )
   1807 

ValueError: With n_samples=1, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.

这段代码可能出了什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-12 06:39:34

一个简单的解决办法是:

代码语言:javascript
运行
复制
for group in Final.groupby('Key'): 
    # group is a tuple where the first value is the Key and the second is the dataframe
    result = regression(group[1])
    # do xyz with result

编辑:

您不必再次将组转换为数据帧,并且可以直接使用它,因为它已经以正确的格式存在。

代码语言:javascript
运行
复制
# this line is not necessary
Final1 = pd.DataFrame(Final1)

从错误判断,您传递给train_test_split函数的train_test_split显然没有足够的记录。这一点在错误消息中非常明显。您将不得不使用try来处理这样的错误,除了。

票数 0
EN

Stack Overflow用户

发布于 2020-06-12 10:32:30

一旦我用少于10个观察值过滤掉所有键,代码就可以工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62327251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档