#線性迴歸是用於預測建模的最簡單且使用最廣泛的機器學習演算法之一。
它是一種監督學習演算法,用於根據一個或多個自變數預測因變數的值。
線性迴歸的核心是根據觀察到的資料來擬合線性模型。
線性模型由下列方程式表示:
線性迴歸演算法涉及查找到通過資料點的最佳擬合線。這通常是透過最小化觀測值和預測值之間的平方差來完成的。
from sklearn.datasets import load_diabetesfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_error, r2_score# Load the Diabetes datasetdiabetes = load_diabetes()X, y = diabetes.data, diabetes.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Linear Regression modelmodel = LinearRegression()model.fit(X_train, y_train)# Predicting the test set resultsy_pred = model.predict(X_test)# Evaluating the modelmse = mean_squared_error(y_test, y_pred)r2 = r2_score(y_test, y_pred)print("MSE is:", mse)print("R2 score is:", r2)
邏輯迴歸用於分類問題。它預測給定資料點屬於某個類別的機率,例如是/否或 0/1。
from sklearn.datasets import load_breast_cancerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score# Load the Breast Cancer datasetbreast_cancer = load_breast_cancer()X, y = breast_cancer.data, breast_cancer.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Logistic Regression modelmodel = LogisticRegression(max_iter=10000)model.fit(X_train, y_train)# Predicting the test set resultsy_pred = model.predict(X_test)# Evaluating the modelaccuracy = accuracy_score(y_test, y_pred)precision = precision_score(y_test, y_pred)recall = recall_score(y_test, y_pred)f1 = f1_score(y_test, y_pred)# Print the resultsprint("Accuracy:", accuracy)print("Precision:", precision)print("Recall:", recall)print("F1 Score:", f1)
#決策樹是多功能且強大的機器學習演算法,可用於分類和迴歸任務。
它們因其簡單性、可解釋性以及處理數值和分類資料的能力而廣受歡迎。
決策樹由代表決策點的節點、代表可能結果的分支以及代表最終決策或預測的葉子組成。
決策樹中的每個節點對應一個特徵,分支代表該特徵的可能值。
建構決策樹的演算法涉及根據不同特徵的值遞歸地將資料集分割成子集。目標是建立同質子集,其中目標變數(我們想要預測的變數)在每個子集中都是相似的。
分割過程持續進行,直到滿足停止標準,例如達到最大深度、最小樣本數,或無法進行進一步改進。
from sklearn.datasets import load_winefrom sklearn.tree import DecisionTreeClassifier# Load the Wine datasetwine = load_wine()X, y = wine.data, wine.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Decision Tree modelmodel = DecisionTreeClassifier(random_state=42)model.fit(X_train, y_train)# Predicting the test set resultsy_pred = model.predict(X_test)# Evaluating the modelaccuracy = accuracy_score(y_test, y_pred)precision = precision_score(y_test, y_pred, average='macro')recall = recall_score(y_test, y_pred, average='macro')f1 = f1_score(y_test, y_pred, average='macro')# Print the resultsprint("Accuracy:", accuracy)print("Precision:", precision)print("Recall:", recall)print("F1 Score:", f1)
樸素貝葉斯分類器是一系列簡單的“機率分類器”,它們使用貝葉斯定理和特徵之間的強(樸素)獨立性假設。它特別用於文本分類。
它計算給定每個輸入值的每個類別的機率和每個類別的條件機率。然後使用這些機率根據最高機率對新值進行分類。
from sklearn.datasets import load_digitsfrom sklearn.naive_bayes import GaussianNB# Load the Digits datasetdigits = load_digits()X, y = digits.data, digits.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Naive Bayes modelmodel = GaussianNB()model.fit(X_train, y_train)# Predicting the test set resultsy_pred = model.predict(X_test)# Evaluating the modelaccuracy = accuracy_score(y_test, y_pred)precision = precision_score(y_test, y_pred, average='macro')recall = recall_score(y_test, y_pred, average='macro')f1 = f1_score(y_test, y_pred, average='macro')# Print the resultsprint("Accuracy:", accuracy)print("Precision:", precision)print("Recall:", recall)print("F1 Score:", f1)
K 最近邻 (KNN) 是一种简单直观的机器学习算法,用于分类和回归任务。
它根据输入数据点与其在特征空间中最近邻居的相似性进行预测。
在 KNN 中,新数据点的预测由其 k 个最近邻的多数类(用于分类)或平均值(用于回归)确定。KNN 中的 “k” 表示要考虑的邻居数量,这是用户选择的超参数。
KNN 算法包括以下步骤
from sklearn.datasets import load_winefrom sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score# Load the Wine datasetwine = load_wine()X, y = wine.data, wine.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the KNN modelknn_model = KNeighborsClassifier(n_neighbors=3)knn_model.fit(X_train, y_train)# Predicting the test set resultsy_pred_knn = knn_model.predict(X_test)# Evaluating the modelaccuracy_knn = accuracy_score(y_test, y_pred_knn)precision_knn = precision_score(y_test, y_pred_knn, average='macro')recall_knn = recall_score(y_test, y_pred_knn, average='macro')f1_knn = f1_score(y_test, y_pred_knn, average='macro')# Print the resultsprint("Accuracy:", accuracy_knn)print("Precision:", precision_knn)print("Recall:", recall_knn)print("F1 Score:", f1_knn)
支持向量机 (SVM) 是一种强大的监督学习算法,用于分类和回归任务。
它们在高维空间中特别有效,广泛应用于图像分类、文本分类和生物信息学等各个领域。
支持向量机的工作原理是找到最能将数据分为不同类别的超平面。
选择超平面以最大化边距,即超平面与每个类的最近数据点(支持向量)之间的距离。
SVM 还可以通过使用核函数将输入空间转换为可以线性分离的高维空间来处理非线性数据。
训练 SVM 的算法包括以下步骤:
from sklearn.svm import SVCbreast_cancer = load_breast_cancer()X, y = breast_cancer.data, breast_cancer.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the SVM modelsvm_model = SVC()svm_model.fit(X_train, y_train)# Predicting the test set resultsy_pred_svm = svm_model.predict(X_test)# Evaluating the modelaccuracy_svm = accuracy_score(y_test, y_pred_svm)precision_svm = precision_score(y_test, y_pred_svm, average='macro')recall_svm = recall_score(y_test, y_pred_svm, average='macro')f1_svm = f1_score(y_test, y_pred_svm, average='macro')accuracy_svm, precision_svm, recall_svm, f1_svm# Print the resultsprint("Accuracy:", accuracy_svm)print("Precision:", precision_svm)print("Recall:", recall_svm)print("F1 Score:", f1_svm)
随机森林是一种集成学习技术,它结合了多个决策树来提高预测性能并减少过度拟合。
它们广泛用于分类和回归任务,并以其鲁棒性和多功能性而闻名。
随机森林是根据数据集的随机子集并使用特征的随机子集进行训练的决策树的集合。
森林中的每棵决策树独立地进行预测,最终的预测是通过聚合所有树的预测来确定的。
构建随机森林的算法包括以下步骤
from sklearn.ensemble import RandomForestClassifierbreast_cancer = load_breast_cancer()X, y = breast_cancer.data, breast_cancer.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Random Forest modelrf_model = RandomForestClassifier(random_state=42)rf_model.fit(X_train, y_train)# Predicting the test set resultsy_pred_rf = rf_model.predict(X_test)# Evaluating the modelaccuracy_rf = accuracy_score(y_test, y_pred_rf)precision_rf = precision_score(y_test, y_pred_rf, average='macro')recall_rf = recall_score(y_test, y_pred_rf, average='macro')f1_rf = f1_score(y_test, y_pred_rf, average='macro')# Print the resultsprint("Accuracy:", accuracy)print("Precision:", precision)print("Recall:", recall)print("F1 Score:", f1)
K 均值聚类是一种无监督学习算法,用于将数据分组为 “K” 个聚类。确定 k 个质心后,每个数据点被分配到最近的簇。
该算法将数据点分配给一个簇,使得数据点与簇质心之间的平方距离之和最小。
from sklearn.datasets import load_irisfrom sklearn.cluster import KMeansfrom sklearn.metrics import silhouette_score# Load the Iris datasetiris = load_iris()X = iris.data# Applying K-Means Clusteringkmeans = KMeans(n_clusters=3, random_state=42)kmeans.fit(X)# Predicting the cluster for each data pointy_pred_clusters = kmeans.predict(X)# Evaluating the modelinertia = kmeans.inertia_silhouette = silhouette_score(X, y_pred_clusters)print("Inertia:", inertia)print("Silhouette:", silhouette)
降维是通过使用主成分分析 (PCA) 来完成的。它将数据转换为新的坐标系,减少变量数量,同时尽可能多地保留原始数据的变化。
使用 PCA 可以找到使数据方差最大化的主要成分或轴。第一个主成分捕获最大方差,第二个主成分(与第一个主成分正交)捕获第二大方差,依此类推。
from sklearn.datasets import load_breast_cancerfrom sklearn.decomposition import PCAimport numpy as np# Load the Breast Cancer datasetbreast_cancer = load_breast_cancer()X = breast_cancer.data# Applying PCApca = PCA(n_compnotallow=2)# Reducing to 2 dimensions for simplicitypca.fit(X)# Transforming the dataX_pca = pca.transform(X)# Explained Varianceexplained_variance = pca.explained_variance_ratio_# Total Explained Variancetotal_explained_variance = np.sum(explained_variance)print("Explained variance:", explained_variance)print("Total Explained Variance:", total_explained_variance)
梯度提升是一种先进的机器学习技术。它依次构建多个弱预测模型(通常是决策树)。每个新模型都逐渐最小化整个模型的损失函数(误差)。
from sklearn.datasets import load_diabetesfrom sklearn.ensemble import GradientBoostingRegressorfrom sklearn.metrics import mean_squared_error, r2_score# Load the Diabetes datasetdiabetes = load_diabetes()X, y = diabetes.data, diabetes.target# Splitting the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Creating and training the Gradient Boosting modelgb_model = GradientBoostingRegressor(random_state=42)gb_model.fit(X_train, y_train)# Predicting the test set resultsy_pred_gb = gb_model.predict(X_test)# Evaluating the modelmse_gb = mean_squared_error(y_test, y_pred_gb)r2_gb = r2_score(y_test, y_pred_gb)print("MSE:", mse_gb)
以上是超強!必會的十大機器學習演算法的詳細內容。更多資訊請關注PHP中文網其他相關文章!