df_test = pd.DataFrame(
data=np.array([
np.repeat(['class' + str(i+1) for i in range(2)], 10),
list(range(10, 0, -1)) * 2,
list(range(1, 11)) * 2
]).T,
columns=['class'] + ['x'+str(i+1) for i in range(2)]
) # np.array强制内部元素格式统一
df_test[df_test.columns.drop(['class'])] = df_test[df_test.columns.drop(['class'])].astype('int32')
df_test['y'] = df_test.apply(
func=lambda x: 10 + np.exp(x['x1']) + np.exp(2 * x['x2']) + 10
if x['class'] == 'class1'
else (
20 + np.exp(10 * x['x1']) + np.exp(-2 * x['x2']) - 10
if x['class'] == 'class2'
else np.nan
),
axis=1
)
df_test
标签:10,df,test,exp,np,格式,array,class
From: https://blog.51cto.com/u_16055028/7845729