├── README.md ├── 1_regression ├── 1_1_random_forest_regressor.py └── 2_1_iris_dataset.py └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # sklearn-playground -------------------------------------------------------------------------------- /1_regression/1_1_random_forest_regressor.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from sklearn.linear_model import LinearRegression 4 | import seaborn; seaborn.set() 5 | 6 | model = LinearRegression(normalize=True) 7 | x = np.arange(10) 8 | y = 2 * x + 1 9 | 10 | np.random.seed(0) 11 | X = np.random.random(size=(20, 1)) 12 | y = 3 * X.squeeze() + 2 + np.random.randn(20) 13 | 14 | plt.plot(X.squeeze(), y, 'o'); 15 | 16 | from sklearn.ensemble import RandomForestRegressor 17 | model = RandomForestRegressor() 18 | model.fit(X, y) 19 | 20 | # Plot the data and the model prediction 21 | X_fit = np.linspace(0, 1, 100)[:, np.newaxis] 22 | y_fit = model.predict(X_fit) 23 | 24 | plt.plot(X.squeeze(), y, 'o') 25 | plt.plot(X_fit.squeeze(), y_fit); 26 | plt.show() -------------------------------------------------------------------------------- /1_regression/2_1_iris_dataset.py: -------------------------------------------------------------------------------- 1 | from sklearn.datasets import load_iris 2 | iris = load_iris() 3 | 4 | iris.keys() 5 | print(iris.keys()) 6 | 7 | n_samples, n_features = iris.data.shape 8 | print((n_samples, n_features)) 9 | print(iris.data[0]) 10 | 11 | print(iris.data.shape) 12 | print(iris.target.shape) 13 | 14 | print(iris.target) 15 | print(iris.target_names) 16 | 17 | import numpy as np 18 | import matplotlib.pyplot as plt 19 | 20 | x_index = 0 21 | y_index = 1 22 | 23 | # This formatter will label the colorbar with the correct target names. 24 | formatter = plt.FuncFormatter(lambda i, *args: iris.target_names[int(i)]) 25 | 26 | plt.scatter(iris.data[:, x_index], iris.data[:, y_index], 27 | c=iris.target, cmap=plt.cm.get_cmap('RdYlBu', 3)) 28 | plt.colorbar(ticks=[0, 1, 2], format=formatter) 29 | plt.clim(-0.5, 2.5) 30 | plt.xlabel(iris.feature_names[x_index]) 31 | plt.ylabel(iris.feature_names[y_index]); 32 | 33 | plt.show() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Andrew Stepin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------