机器学习经典算法(scikit-learn)

发布于:2024-12-19 ⋅ 阅读:(13) ⋅ 点赞:(0)

安装库:pip install scikit-learn numpy

  1. 线性回归 (Linear Regression)

在这里插入图片描述

import numpy as np  
import pandas as pd  
from sklearn.model_selection import train_test_split  
from sklearn.linear_model import LinearRegression  
from sklearn.datasets import load_boston  

# 加载数据  
boston = load_boston()  
X = boston.data  
y = boston.target  

# 划分数据集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  

# 创建线性回归模型  
model = LinearRegression()  
model.fit(X_train, y_train)  

# 预测  
predictions = model.predict(X_test)  
print(predictions)
  1. Logistic 回归 (Logistic Regression)

在这里插入图片描述

from sklearn.datasets import load_iris  
from sklearn.linear_model import LogisticRegression  

# 加载数据  
iris = load_iris()  
X = iris.data  
y = iris.target  

# 选择二分类问题  
X_bin = X[y != 2]  
y_bin = y[y != 2]  

# 划分数据集  
X_train, X_test, y_train, y_test = train_test_split(X_bin, y_bin, test_size=0.2, random_state=42)  

# 创建Logistic回归模型  
model = LogisticRegression()  
model.fit(X_train, y_train)  

# 预测  
predictions = model.predict(X_test)  
print(predictions)
  1. 线性判别分析 (Linear Discriminant Analysis, LDA)

在这里插入图片描述

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis  

# 使用上面的鸢尾花数据  
lda = LinearDiscriminantAnalysis()  
lda.fit(X_train, y_train)  

# 预测  
predictions = lda.predict(X_test)  
print(predictions)
  1. 决策树 (Decision Tree)
    在这里插入图片描述
from sklearn.tree import DecisionTreeClassifier  

# 创建决策树模型  
tree_model = DecisionTreeClassifier(random_state=42)  
tree_model.fit(X_train, y_train)  

# 预测  
predictions = tree_model.predict(X_test)  
print(predictions)
  1. 朴素贝叶斯 (Naive Bayes)

在这里插入图片描述

from sklearn.naive_bayes import GaussianNB  

# 创建朴素贝叶斯模型  
nb_model = GaussianNB()  
nb_model.fit(X_train, y_train)  

# 预测  
predictions = nb_model.predict(X_test)  
print(predictions)
  1. K 最近邻算法 (K-Nearest Neighbors)

在这里插入图片描述

from sklearn.neighbors import KNeighborsClassifier  

# 创建KNN模型  
knn_model = KNeighborsClassifier(n_neighbors=3)  
knn_model.fit(X_train, y_train)  

# 预测  
predictions = knn_model.predict(X_test)  
print(predictions)
  1. 学习向量量化 (Learning Vector Quantization)
    学习向量量化可以使用 KNN 的变种,通常在实际使用中与 KNN 一起。
    在这里插入图片描述

  2. 支持向量机 (Support Vector Machine)

在这里插入图片描述

from sklearn.svm import SVC  

# 创建支持向量机模型  
svm_model = SVC(kernel='linear')  
svm_model.fit(X_train, y_train)  

# 预测  
predictions = svm_model.predict(X_test)  
print(predictions)
  1. 袋装法和随机森林 (Bagging and Random Forest)

在这里插入图片描述

 from sklearn.ensemble import RandomForestClassifier  

# 创建随机森林模型  
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)  
rf_model.fit(X_train, y_train)  

# 预测  
predictions = rf_model.predict(X_test)  
print(predictions)

网站公告

今日签到

点亮在社区的每一天
去签到