机器学习模型:
线性回归LinearRegression:给定xy样本集,y与x的关系属于线性y=ax+b,测试集给定x,看输出是否属于y=ax+b
KNN:监督学习,给定样本集和target训练,测试集看输出target是否正确
Kmean:对样本进行聚类,无label
from sklearn.cluster import KMeans
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
from sklearn.datasets import make_blobs
import joblib
x = [[i] for i in range(5)]
y = [i * 2 + 7 for i in range(5)]
regression = LinearRegression()
regression.fit(x, y)
joblib.dump(regression, 'regression.pkl')
regression1 = joblib.load('regression.pkl')
print(regression1.predict([[6], [8]]))
print(f'coef = {regression.coef_},inter = {regression.intercept_}')
x1 = [[-10, -11], [-1, -11], [-11, -22], [22, 32], [33, 31], [44, 100]]
y1 = [0, 0, 0, 1, 1, 1]
knn = KNeighborsClassifier()
knn.fit(x1, y1)
print(knn.predict([[-2, -111], [2222, 333]]))
x3, y3 = make_blobs(n_samples=1000, n_features=2, centers=[[0, 0], [1, 1], [2, 2]], cluster_std=[0.4, 0.3, 0.3])
kmeans = KMeans(n_clusters=3)
kmeans.fit(x3)
print(x3)
print(kmeans.labels_)
plt.figure()
plt.scatter(x3[:, 0], x3[:, 1], c=kmeans.labels_, cmap='viridis')
plt.show()
F:\Users\anaconda3\python.exe E:\PycharmProjects\pythonProject\2.py
19. 23.
coef = 2.,inter = 7.0
0 1
\[1.18237797 1.10239862
2.52009639 2.24120624
1.51328496 0.74354331
...
0.40767352 0.11376598
1.57515938 1.95613922
0.71643209 0.77786856\]
[0 1 0 0 2 0 1 1 1 0 1 0 0 1 2 2 2 0 1 2 2 0 1 1 0 0 0 2 0 1 1 1 1 2 1 0 0
1 0 2 0 2 0 2 1 2 1 2 1 1 1 1 1 1 2 2 2 0 2 1 2 0 0 1 2 2 1 0 0 2 0 2 0 2
1 0 1 2 2 2 2 0 0 2 0 0 0 1 1 2 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 1 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