Python-创建具有相关数值变量的数据集

时间:2018-07-11 08:43:51

标签: python random

我想创建一个数据集,其中我的经验从1年到10年不等,薪水从3万到10万不等。我希望这些薪水是随机的并遵循多年的经验。有时,经验丰富的人可能比经验较少的人做得更少。

例如:

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行

5 个答案:

答案 0 :(得分:2)

您可以将薪水定义为等于某个系数乘以某个常数值再加上一些随机值的年数。

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)

enter image description here

答案 1 :(得分:1)

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=80enter image description here

或使用noise=250enter image description here

作为一个补充说明:这将为“多年的经验”生成连续的值。如果您希望将它们四舍五入为整数,则可以使用X = np.rint(X)

答案 2 :(得分:1)

在这种情况下,我将更改行:

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)

随着薪水的增长,随机因素的差异更大。

答案 3 :(得分:0)

random.uniform(-1,1)*5000意味着您的薪水值将在-5k到+ 5k的范围内变化,但是由于其输出是连续的,因此很可能薪水变化很小。 看到没有随机元素的薪水如何随着经验的增加而变化7777.77 ...,获得更高经验的人不太可能获得较低的薪水。我建议您增加随机元素背后的因子。 例如尝试random.uniform(-1,1) * 10000。您对随机性的掌握程度有多高,取决于让一个经验丰富的高薪人得到应有的可能性。

答案 4 :(得分:0)

import numpy as np
import random
import pandas as pd
years = np.linspace(1.0, 10.0, num=10)
salary = np.random.randint(30000.0, 100000.0, 10)
data = pd.DataFrame({'experience' : years, 'salary': salary})
print (data)