【机器学习,Python】机器学习1

监督学习(supervised learning):训练数据包含正确结果

回归分析:根据数据确定两种或两种以上变量间相互依赖的定量关系

线性回归

评估模型表现

均方误差MSE:越小越好

 R方值:越接近1越好

from sklearn.metrics import mean_squared_error,r2_score
MSE=mean_squared_error(y,y_predict)#越小越好
R2=r2_score(y,y_predic)#越靠近1越好
from matplotlib import pyplot as plt
plt.scatter(y,yr)#集中度越高越好,是y=x直线

逻辑回归

二分类问题

from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.read_csv('examdata.csv')
fig = plt.figure()
plt.scatter(data.loc[:, 'Exam1'], data.loc[:, 'Exam2'])
plt.title('exam1-exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.show()
# 增加标识
mask = data.loc[:, 'Pass'] == 1
fig2 = plt.figure()
passed = plt.scatter(data.loc[:, 'Exam1'][mask], data.loc[:, 'Exam2'][mask])
failed = plt.scatter(data.loc[:, 'Exam1'][~mask], data.loc[:, 'Exam2'][~mask])
plt.title('exam1-exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.legend((passed, failed), ('passed', 'failed'))
plt.show()

x = data.drop(['Pass'], axis=1)
y = data.loc[:, 'Pass']
x1 = data.loc[:, 'Exam1']
x2 = data.loc[:, 'Exam2']

# 模型建立
LR = LogisticRegression()
LR.fit(x, y)

# 结果
y_predit = LR.predict(x)
print(y_predit)
accuracy = accuracy_score(y, y_predit)
print(accuracy)  # 准确率
# 测试exam1=70,exam2=65
y_test = LR.predict([[70, 65]])
print(y_test)

二阶边界函数封装

def f(x):
    a = theta4
    b = theta5*x+theta2
    c = theta0+theta1*x+theta3*x*x
    x2_new_boundary1 = (-b+np.sqrt(b*b-4*a*c))
    x2_new_boundary2 = (-b-np.sqrt(b*b-4*a*c))
    return x2_new_boundary1, x2_new_boundary2

决策树

特征选择,每个节点应该选用哪个特征

ID3:利用信息熵的原理选择信息增益最大的属性作为分类属性

#水仙花的决策树
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
# plt.rcParams['axes.labelsize']=14
# plt.rcParams['xtrick.labelsize']=12
# plt.rcParams['ytrick.labelsize']=12
import warnings
# warnings.filterwarnings('ingnore')

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
iris=load_iris()
x=iris.data[:,2:]
y=iris.target
tree_clf=DecisionTreeClassifier(max_depth=2)
tree_clf.fit(x,y)

异常检测anomaly detection

基于高斯分布(正态分布)

主成分分析(PCA)

数据降维:降低随机变量个数,减少模型分析数据量,降低难度,实现数据可视化

目标:寻找k维的新数据,使它们反映事物的主要特征

过程:1.进行预处理(正态函数标准化)

          2.计算协方差矩阵特征向量,及数据在特征向量上投影后的方差

          3.根据需求确定降维维度

          4.选取k维特征向量,计算数据在其形成空间的投影

神经网络、卷积神经网络、循环神经网络

无监督学习(unspervised learning):训练数据不包含正确结果

聚类分析、关联规则、维度缩减

聚类算法

群分析,应用:客户划分

k-means实现

knn实现

DBSCAN

过滤噪声

Mean-shift

在中心点一定区域检索数据点,自动更新

自动发现类别数量,需要选择区域半径

#接KMeans
#meanshift开始
from sklearn.cluster import MeanShift,estimate_bandwidth
#获得带宽
bw=estimate_bandwidth(x,n_samples=500)#n_samples=500,意为有500样本
#建立模型
ms=MeanShift(bandwidth=bw)
ms.fix(x)
y_predict_ms=ms.predict(x)
print(pd.value_counts(y_predict_ms))

半监督学习(semi-supervised learning):训练数据包含少量正确结果

强化学习(reinforcement learning):根据每次结果收获的奖惩进行学习,实现优化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岩塘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值