我想创建一个数据集,其中我有1到10年的经验,工资从30k到100k。我希望这些薪水是随机的,并遵循多年的经验。有时,经验丰富的人可能比经验较少的人赚得少。
例如:
years of experience | Salary
1 | 30050
2 | 28500
3 | 36000
...
10 | 100,500以下是我到目前为止所做的工作:
import numpy as np
import random
import pandas as pd
years = np.linspace(1.0, 10.0, num=10)
salary = np.linspace(30000.0, 100000.0, num=10) + random.uniform(-1,1)*5000#plus/minus 5k
data = pd.DataFrame({'experience' : years, 'salary': salary})
print (data)这给了我:
experience salary
0 1.0 31060.903965
1 2.0 38838.681742
2 3.0 46616.459520
3 4.0 54394.237298
4 5.0 62172.015076
5 6.0 69949.792853
6 7.0 77727.570631
7 8.0 85505.348409
8 9.0 93283.126187
9 10.0 101060.903965我们可以看到,我们没有得到一些记录,其中经验较高的人比经验较少的人少。我怎么才能解决这个问题呢?当然,我希望将其扩展到1000行
发布于 2018-07-11 17:19:16
scikit learn提供了一些有用的函数来生成相关数据,例如make_regression。
例如,您可以这样做:
import numpy as np
import pandas as pd
from sklearn.datasets import make_regression
np.random.seed(0)
n_samples = 1000
X, y = make_regression(n_samples=n_samples, n_features=1, n_informative=1,
noise=80, random_state=0)
# Scale X (years of experience) to 0..10 range
X = np.interp(X, (X.min(), X.max()), (0, 10))
# Scale y (salary) to 30000..100000 range
y = np.interp(y, (y.min(), y.max()), (30000, 100000))
# To dataframe
df = pd.DataFrame({'experience': X.flatten(), 'salary': y}
print(df.head(10))从您的描述来看,您似乎想要在响应中添加一些差异。这可以通过调整noise参数来完成。让我们将其绘制出来,以使其更明显:
from matplotlib import pyplot as plt
plt.scatter(X, y, color='blue', marker='.', label='Salary')
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()例如,使用noise=80:

或者使用noise=250:

作为附注:这会为“经验年限”生成连续的值。如果您希望将它们四舍五入为整数,则可以使用X = np.rint(X)来实现
发布于 2018-07-11 17:33:43
您可以将工资定义为等于年数乘以某个系数,加上某个常量值,再加上某个随机值。
import numpy as np
import random
import pandas as pd
N = 1000
intercept = 30000
coeff = 7000
years = np.random.uniform(low=1, high=10, size=N)
salary = intercept + years*coeff + np.random.normal(loc=0, scale=10000, size=N)
data = pd.DataFrame({'experience' : years, 'salary': salary})
data.plot.scatter(x='experience', y='salary', alpha=0.3)

发布于 2018-07-11 17:22:47
在本例中,我将更改行:
salary = np.linspace(30000.0, 100000.0, num=10) + random.uniform(-1,1)*5000#plus/minus 5k我认为最好是将随机部分分开,这样你就可以很容易地改变它,并根据你想要达到的值进行所有的修改。
下面是我做的一些事情:
import numpy as np
import random
import pandas as pd
years = np.linspace(1.0, 10.0, num=10)
random_list = [random.random()*1000*_*5 for _ in range(10)]
print(random_list)
salary = np.linspace(30000.0, 100000.0, num=10)- random_list
data = pd.DataFrame({'experience' : years, 'salary': salary})
print (data)当工资增长时,随机成分的方差更大。
https://stackoverflow.com/questions/51280994
复制相似问题