Train Test Split in Python with scikit-learn
Split a dataset into training and test sets with sklearn train_test_split, using test_size, random_state and stratify, and check the shapes.
View example →Split, scale and encode data, then train and evaluate regression, classification and clustering models with scikit-learn, in runnable examples.
Run Machine Learning with scikit-learn onlineSplit a dataset into training and test sets with sklearn train_test_split, using test_size, random_state and stratify, and check the shapes.
View example →Standardize features with sklearn StandardScaler so each column has mean 0 and standard deviation 1, fitting on the training set only.
View example →Turn text categories into 0/1 columns with pandas get_dummies and sklearn OneHotEncoder, and handle categories the model has never seen.
View example →Fit a straight line with sklearn LinearRegression, read the slope and intercept, measure R^2 and MAE, and predict new values.
View example →Classify tumours with sklearn LogisticRegression in a scaling pipeline, measure accuracy and read class probabilities with predict_proba.
View example →Classify wines with sklearn KNeighborsClassifier, see why scaling matters for distances, and compare accuracy for different values of k.
View example →Train a sklearn DecisionTreeClassifier, print its rules with export_text, and see how max_depth controls overfitting.
View example →Train a sklearn RandomForestClassifier on the breast cancer data, measure accuracy and list the most important features.
View example →Train a support vector machine with sklearn SVC, compare the linear and RBF kernels on curved and real data, with feature scaling.
View example →Group unlabeled points with sklearn KMeans, read the cluster centres and inertia, and pick the number of clusters with the elbow method.
View example →Reduce 13 wine features to 2 with sklearn PCA, read explained_variance_ratio_, and find how many components keep 90% of the variance.
View example →Build a confusion matrix with sklearn, read TP, FP, FN and TN, compute precision and recall, and print a classification report.
View example →Score a model with sklearn cross_val_score and StratifiedKFold, and report the mean and standard deviation over 5 folds.
View example →Tune hyperparameters with sklearn GridSearchCV, read best_params_ and best_score_, and compare every combination in cv_results_.
View example →