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

相关推荐
鱼宵1 小时前
取集合第一个元素,Java 为什么要写三种写法?
java·windows·python·java21
卷无止境1 小时前
给AI装上一双眼睛:快速构建代码验证器的思路与落地方案
后端·python
维克兜率天2 小时前
【维克】指数调整:哪些股票会被“强制买入“?
人工智能·python·算法
come112342 小时前
Codex 下不同场景 模式选择
python
罗西的思考2 小时前
【轻量化复刻大模型】线性注意力交互式演示
人工智能·算法·机器学习
神也畏惧菠萝2 小时前
Day08 Python流程控制从入门到上瘾:if/match/while/for四件套,从B站登录一路打到猜数字游戏
python·算法·数据存储·for·while·if·ifelse
言乐63 小时前
通过 3D 动画直观演示球形瓶内可滑动半圆机构在倾倒时防粘盖、回正时刮膏体的运动学过程
python·3d·django·virtualenv·pygame
quantdash_cc3 小时前
ETF 行情数据清洗实战:字段语义拆解、标的池整理与五个校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash