Make datasets
Generate
synthetic datasets for machine learning tasks.
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.datasets import make_classification
from sklearn.datasets import make_regression
features1, target1 = make_blobs(
n_samples = 100,
n_features = 2,
centers = 3,
cluster_std = 0.5,
shuffle = True,
random_state = 1
)
features2, target2 = make_classification(
n_samples = 100,
n_features = 2,
n_informative = 2,
n_redundant = 0,
n_classes = 2,
weights = [.25, .75],
random_state = 1
)
features3, target3, coef3 = make_regression(
n_samples = 100,
n_features = 3,
n_informative = 3,
n_targets = 1,
noise = 0,
coef = True,
random_state = 1
)
plt.scatter(features1[:, 0], features1[:, 1], c=target1)
plt.title('Make blob - Simultated dataset')
plt.show()
plt.scatter(features2[:, 0], features2[:, 1], c=target2)
plt.title('Make classification - Simultated dataset')
plt.show()
plt.scatter(features3[:, 0], features3[:, 1], c=target3)
plt.title('Make regression - Simultated dataset')
plt.show()
print("Blob / Features[0:3]:\n", features1[0:3])
print("Target[:10]:", target1[:10], '\n')
print("Classification / Features[0:3]:\n", features2[0:3])
print("Target[:10]:", target2[:10], '\n')
print("Regression / Features[0:3]:\n", features3[0:3])
print("Target[:10]:\n", target3[:10], '\n')
Survey Simulation
Suppose, we have a
survey among the employees of a company.
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression
import numpy as np
import pandas as pd
X, y = make_regression(
n_samples = 100,
n_features = 1,
n_informative = 1,
n_targets = 1,
noise = 10,
coef = False,
random_state = 0
)
X = np.interp(X, (X.min(), X.max()), (0, 20))
y = np.interp(y, (y.min(), y.max()), (10000, 200000))
reg = LinearRegression().fit(X, y)
plt.scatter(X, y, label='training data')
plt.title('Simultated dataset (Experience / Salary)')
x_line = np.linspace(np.min(X), np.max(X), 100)
y_line = reg.intercept_ + x_line * reg.coef_[0]
plt.plot(x_line, y_line, color='red', label='prediction')
plt.text(10, 25000, r'y = %0.2f + %0.2f x' % (reg.intercept_, reg.coef_[0]))
plt.xlabel('Years of experience')
plt.ylabel('Salary')
plt.legend()
plt.show()
df = pd.DataFrame(data={'Experience': X.flatten(), 'Salary': y})
print(df.head(10))