我有以下功能
import math
def f(k,a,x):
return (1/(1+math.exp(-k*x))^a)
和以下Pandas 数据帧
import pandas as pd
df = pd.DataFrame({'x': list(range(1,100))})
我如何将上面的function
应用于df
的x
列,比如说k=1
和a=2
?
我有以下功能
import math
def f(k,a,x):
return (1/(1+math.exp(-k*x))^a)
和以下Pandas 数据帧
import pandas as pd
df = pd.DataFrame({'x': list(range(1,100))})
我如何将上面的function
应用于df
的x
列,比如说k=1
和a=2
?
使用您的功能:
def f(k,a,x):
return (1/(1+math.exp(-k*x))**a)
df['out'] = df['x'].apply(lambda x: f(k=1, a=2, x=x))
But better vectorize with numpy:
import numpy as np
def f_vectorized(k, a, x):
return (1/(1+np.exp(-k*x))**a)
df['out'] = f_vectorized(k=1, a=2, x=df['x'])
输出:
x out
0 1 0.534447
1 2 0.775803
2 3 0.907397
3 4 0.964351
4 5 0.986659
.. .. ...
94 95 1.000000
95 96 1.000000
96 97 1.000000
97 98 1.000000
98 99 1.000000
[99 rows x 2 columns]