回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图编写的函数将获取提供的数据帧,计算F统计值,并将其作为输出</p>
<p>数据格式<code>Final</code></p>
<pre><code>Key Color Strength Fabric Sales
a 0 1 1 10
b 1 2 2 15
</code></pre>
<p>在这里,颜色、强度和织物是独立的,而销售是独立的</p>
<p>其思想是创建一个循环,为每个唯一的键值创建一个新的数据帧:
并在此数据帧上执行一个函数,然后创建一个新的数据帧,该数据帧是从唯一键值获得的所有新数据帧的集合</p>
<pre><code>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)
</code></pre>
<p>这是我想出的代码,但它抛出了一个错误</p>
<pre><code>---------------------------------------------------------------------------
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.
</code></pre>
<p>这个代码可能出了什么问题</p>