简单上手scikit-learn 02 训练模型

Odetta ·
更新时间:2024-11-13
· 911 次阅读

Load Data # load iris data from sklearn.datasets import load_iris iris = load_iris() # store feature matrix in "X" X = iris.data # store response vector in "X" y = iris.target print(X.shape) print(y.shape) (150, 4) (150,) KNN from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors=1) Name of the object does not matter Can specify tuning parameters (aka “hyperparameters”) during this step All parameters not specified are set to their defaults print(knn) KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform') knn.fit(X,y) KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform') X_new = [[3, 5, 4, 2], [5, 4, 3, 2]] knn.predict(X_new) array([2, 1]) Using different value of K knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X,y) KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=5, p=2, weights='uniform') knn.predict(X_new) array([1, 1]) Using Logistic Regression from sklearn.linear_model import LogisticRegression logreg = LogisticRegression() logreg.fit(X,y) LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1, penalty='l2', random_state=None, solver='liblinear', tol=0.0001, verbose=0, warm_start=False) logreg.predict(X_new) array([2, 0])
作者:physihy



训练模型 训练 模型 scikit-learn

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章