scikit实现机器学习常用模型
程序员文章站
2024-01-01 11:04:10
1.线性模型import matplotlib.pyplot as pltimport numpy as npfrom sklearn import linear_modelx = np.linspace(-3,3,30);y = 2 * x + 1plt.scatter(x,y)plt.show()X = x + np.random.rand(30)print("X",len(X))print("Y",len(y))plt.scatter(X,y)# plt.show()....
使用scikit库实现了几种常见的机器学习分类算法。
1.线性模型
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
x = np.linspace(-3,3,30);
y = 2 * x + 1
plt.scatter(x,y)
plt.show()
X = x + np.random.rand(30)
print("X",len(X))
print("Y",len(y))
plt.scatter(X,y)
# plt.show()
model = linear_model.LinearRegression()
print("X",len(X))
print("Y",len(y))
print("type X", type(X))
x_tran = [[i] for i in X]
y_train = [[i] for i in y]
model.fit(x_tran,y_train)
print(model.coef_)
print(model.intercept_)
y_ = x*model.coef_ [0]+ model.intercept_
plt.plot(x, y_)
plt.show()
2 logistic回归
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
print("X",X)
print("X len", len(X))
print("y",y)
print("y len",len(y))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = LogisticRegression().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
3 KNN分类算法
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = KNeighborsClassifier().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
4 朴素贝叶斯算法
from sklearn.naive_bayes import MultinomialNB
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = MultinomialNB().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
5 决策树分类算法
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = DecisionTreeClassifier().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
6支持向量机
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = SVC().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
print(clf.kernel)
7 Kmeans聚类算法
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
n_samples = 1500
X,y = make_blobs(n_samples = n_samples)
plt.scatter(X[:,0],X[:,1],c=y)
plt.show()
y_pred = KMeans(n_clusters = 3).fit_predict(X)
plt.scatter(X[:,0],X[:,1],c=y_pred)
plt.show()
8 神经网络分类算法
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = MLPClassifier().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.score(X_test,y_test))
9 随机森林分类算法
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X,y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = RandomForestClassifier().fit(X_train,y_train)
print(clf.predict(X_test))
print(clf.estimators_)
print(clf.score(X_test,y_test))
本文地址:https://blog.csdn.net/u012410628/article/details/107893000