python 机器学习 ML LinearRegression KNN KMeans

机器学习模型:

线性回归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 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0 2 1 1 0 0 1 2 0 0 2 1 2 0 2 2 0 0 0 2 0 2 2 1

1 0 2 2 1 2 2 2 1 0 1 0 1 1 2 0 0 2 1 2 2 0 0 0 0 0 2 0 0 2 2 0 1 1 1 0 2

2 1 2 1 1 2 0 1 1 1 2 0 1 2 0 2 0 2 0 1 0 0 1 1 1 1 1 0 0 1 1 2 2 1 2 1 2

2 1 1 2 0 1 2 2 2 2 2 1 1 0 2 1 0 2 0 1 0 2 2 0 2 2 2 0 1 0 0 0 2 0 0 1 1

2 2 1 1 2 2 0 1 0 1 1 1 1 0 2 2 2 2 0 0 1 1 1 1 0 1 1 2 2 0 1 0 0 1 2 0 1

2 0 1 1 1 1 1 2 1 2 1 2 2 1 0 2 2 0 1 2 1 1 2 2 2 0 1 0 1 0 1 2 1 0 0 0 0

0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 2 0 2 1 1 1 2 2 2 1 1 0 1 0 0 0 2 0 0 1 1

2 1 0 2 2 1 0 2 2 2 0 0 2 0 0 1 0 2 1 0 2 0 1 1 0 1 0 2 0 0 1 1 2 0 1 2 0

1 1 0 0 1 0 1 2 2 0 0 0 2 1 1 0 2 1 2 2 2 2 0 0 2 0 2 1 1 1 2 0 0 2 1 2 1

2 0 0 0 1 0 2 1 1 1 1 2 0 0 0 1 2 2 1 1 0 0 1 2 0 2 1 1 0 1 2 1 2 1 1 1 2

1 1 2 0 2 2 0 0 0 1 0 2 1 1 0 0 0 2 0 0 2 2 0 0 2 0 0 1 1 0 0 0 0 1 2 2 1

0]

Process finished with exit code 0

相关推荐
学无止境小袁同学5 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴5 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
塑料铲子5 小时前
你的 Skill 越多越蠢?Token 上下文爆炸的求生指南
机器学习·agent
LK_076 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk5556 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户54817845909507 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
TsingtaoAI7 小时前
VeyForge:构建具身智能的AI 3D资产与Sim-to-Real数据引擎
人工智能·机器学习
yiangL_7 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
高洁018 小时前
世界模型的因果内核:从预测表象到理解机制
人工智能·python·深度学习·transformer·迁移学习