Difference between revisions of "Sklearn"

From Bitnami MediaWiki
Jump to navigation Jump to search
(Created page with "===Overview=== ===Summary of Syntax=== {| class="wikitable" |- ! Module ! Methods ! Module ! Methods |- | sklearn.metrics | precision_score, <br />recall_score, <br />f1_sco...")
 
 
(4 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
| cross_val_predict, <br />cross_val_score, <br />StratifiedKFold, <br />train_test_split, <br />StratifiedShuffleSplit,<br />GridSearchCV, <br />RandomizedSearchCV, <br />ShuffleSplit
 
| cross_val_predict, <br />cross_val_score, <br />StratifiedKFold, <br />train_test_split, <br />StratifiedShuffleSplit,<br />GridSearchCV, <br />RandomizedSearchCV, <br />ShuffleSplit
 
| sklearn.preprocessing
 
| sklearn.preprocessing
−
| OrdinalEncoder, <br />OneHotEncoder, <br />StandardScaler, <br />PolynomialFeatures, <br />MinMaxScaler
+
| OrdinalEncoder, <br />OneHotEncoder, <br />StandardScaler, <br />PolynomialFeatures, <br />MinMaxScaler, <br />LabelEncoder
 
|-
 
|-
 
| sklearn.linear_model
 
| sklearn.linear_model
Line 59: Line 59:
 
|  
 
|  
 
|}
 
|}
 +
 +
===A Simple Python Model Implementation===
 +
<syntaxhighlight lang=python>#import data
 +
all_data = pd.read_csv('my_data_file.csv')
 +
# drop nan rows (for both train & test)
 +
data = all_data.dropna(axis=0)
 +
# Split data into train and test sets
 +
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
 +
# instantiate the model
 +
model = MySpecialRegressor()
 +
# Fit the model to the training data
 +
model.fit(X_train, y_train) 
 +
# Use trained model to predict y_test values from X_test values
 +
y_pred = model.predict(X_test) 
 +
# Check results
 +
print(mean_absolute_error(y_test, y_pred))
 +
</syntaxhighlight>
 +
 +
===7 Steps of Machine Learning (from [https://www.kaggle.com/alexisbcook/intro-to-automl Kaggle])===
 +
# Gather the data
 +
# Prepare the data
 +
# Select a model
 +
# Train the model
 +
# Evaluate the model
 +
# Tune parameters
 +
# Make predictions

Latest revision as of 22:34, 2 March 2021

Overview[edit]

Summary of Syntax[edit]

Module Methods Module Methods
sklearn.metrics precision_score,
recall_score,
f1_score,
confusion_matrix,
precision_recall_curve,
mean_squared_error,
accuracy_score,
silhouette_score,
silhouette_samples
sklearn.impute SimpleImputer
sklearn.model_selection cross_val_predict,
cross_val_score,
StratifiedKFold,
train_test_split,
StratifiedShuffleSplit,
GridSearchCV,
RandomizedSearchCV,
ShuffleSplit
sklearn.preprocessing OrdinalEncoder,
OneHotEncoder,
StandardScaler,
PolynomialFeatures,
MinMaxScaler,
LabelEncoder
sklearn.linear_model SGDClassifier,
LinearRegression,
Ridge, Lasso,
LogisticRegression,
Perceptron
sklearn.pipeline Pipeline
sklearn.tree DecisionTreeRegressor,
DecisionTreeClassifier,
sklearn.compose ColumnTransformer
sklearn.ensemble RandomForestRegressor,
RandomForestClassifier,
VotingClassifier,
Bagging Classifier,
AdaBoostClassifier,
GradientBoostingRegressor,
ExtraTreesClassifier,
sklearn.svm SVR, SVC, LinearSVC,
LinearSVR
sklearn.base clone,
BaseEstimator,
TransformerMixin
sklearn.multiclass OneVsRestClassifier,
sklearn.neighbors KNeighborsClassifier, sklearn.neural_network MLPClassifier,
sklearn.decomposition PCA, IncrementalPCA,
KernelPCA,
sklearn.manifold LocallyLinearEmbedding,
MDS, Isomap, TSNE,
sklearn.discriminant_analysis LinearDiscriminantAnalysis,
sklearn.mixture GaussianMixture,
BayesianGaussianMixture
sklearn.cluster KMeans, MiniBatchKMeans,
DBSCAN, SpectralClustering,
AgglomerativeClustering

A Simple Python Model Implementation[edit]

#import data
all_data = pd.read_csv('my_data_file.csv')
# drop nan rows (for both train & test) 
data = all_data.dropna(axis=0)
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# instantiate the model
model = MySpecialRegressor()
# Fit the model to the training data
model.fit(X_train, y_train)  
# Use trained model to predict y_test values from X_test values
y_pred = model.predict(X_test)  
# Check results
print(mean_absolute_error(y_test, y_pred))

7 Steps of Machine Learning (from Kaggle)[edit]

  1. Gather the data
  2. Prepare the data
  3. Select a model
  4. Train the model
  5. Evaluate the model
  6. Tune parameters
  7. Make predictions