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))
根据您的描述,似乎您想在响应中添加一些差异.这可以通过调整噪声参数来完成.让我们对其进行绘制以使其更加明显:
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)