数据集



sklearn
sklearn 包含的内容

sklearn数据集的使用:



以鸢尾花数据集为例:

演示代码如下:

from sklearn.datasets  import load_iris

def datasets_demo():
iris = load_iris()
print(“鸢尾花数据集:\n”,iris)
print(“查看数据集描述:\n”,iris[“DESCR”])
print(“查看特征值的名字:\n”,iris.feature_names)
print(“查看特征值:\n”,iris.data)
print(“查看特征值形状:\n”,iris.data.shape)
return None
if name == “main”:
datasets_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

☆这里要注意不要把所有的数据集都拿来训练一个模型,还一个留一部分用来验证,于是有了数据集的划分


以鸢尾花数据集为例代码如下:

from sklearn.datasets  import load_iris
from sklearn.model_selection import train_test_split
def datasets_demo():#获取数据集iris = load_iris()print("鸢尾花数据集:\n",iris)print("查看数据集描述:\n",iris["DESCR"])print("查看特征值的名字:\n",iris.feature_names)print("查看特征值:\n",iris.data)print("查看特征值形状:\n",iris.data.shape)#数据集的划分x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.2,random_state=22)print("训练集的特征值:\n",x_train,x_train.shape)print("测试集的特征值:\n",x_test,x_test.shape)return None
if __name__ == "__main__":datasets_demo()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里我们定义 训练集的特征值(x_train),测试集的特征值(x_test),训练集的目标值(y_train),测试集的目标值(y_test)







将特征值都处理成one-hot编码

代码如下:

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer

def dict_demo():
data = [{‘city’:‘北京’,‘temperature’:100},{‘city’:‘上海’,‘temperature’:60},{‘city’:‘深圳’,‘temperature’:30}]
#实例一个转换器类
transfer = DictVectorizer(sparse=False)
#调用fit_transform()
data_new = transfer.fit_transform(data)
print(“data_new:\n”,data_new)
print(“特征名称:\n”,transfer.get_feature_names())
return None

if name== “main”:
dict_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15


这里可以看到输出并不是我们想象中的二维数组,这是因为当我们实例化DictVectorizer时,他默认有一个参数sparse=True,如果想要得到二维数组形式,需要将sparse=False,但两者应该是等价的,前者对应后者在数组中的位置


例:英文文本分词

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer

def count_demo():
data = {“Life is short,i like like python”,“Life is too long,i dislike python”}
#实例化一个转化器
transfer = CountVectorizer()
#调用transform
data_new = transfer.fit_transform(data)
print(“data_new:\n”,data_new)
return None
if name == ‘main’:
count_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14


这里我们看到输出又不是我们想要的,这是我想同上面的例子一样在CountVectorizer中加入sparse=False发现行不通,但是sparse矩阵里面有一个默认的方法toarray(),我们可以直接调用这个方法

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer

def count_demo():
data = {“Life is short,i like like python”,“Life is too long,i dislike python”}
#实例化一个转化器
transfer = CountVectorizer()
#调用transform
data_new = transfer.fit_transform(data)
print(“data_new:\n”,data_new.toarray())
print(“特征名字:\n”,transfer.get_feature_names())
return None
if name == ‘main’:
count_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14


这里提到了停用词的概念

例:中文文本分词,需要用到jieba分词

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
import jieba

def cut_word(text):
return " ".join(list(jieba.cut(text)))
def count_chinese_demo():
data = {“在北上广深,软考证书可以混个工作居住证,也是一项大的积分落户筹码。”,
“升职加薪必备,很多企业人力资源会以此作为审核晋升的条件。”,
“简历上浓彩重抹一笔,毕竟是国家人力部、工信部承认的IT高级人才。”}
data_new=[]
for sent in data:
data_new.append(cut_word(sent))
#实例化一个转化器
transfer = CountVectorizer()
#调用transform
data_final = transfer.fit_transform(data_new)
print(“data_new:\n”,data_final.toarray())
print(“特征名字:\n”,transfer.get_feature_names())
return None
if name == ‘main’:
count_chinese_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23


但是这种方法也有弊端,于是我们用到了另一种方法





特征预处理就是通过一些转化函数将特征数据转换成更加适合算法模型的特征数据过程(无量纲化处理)





其中[mx-mi]成为区间占比,一般定为0-1

实例:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
def minmax_demo():data = pd.read_csv("dating.txt")data = data.iloc[:,:3]  #只取数据前三列print("data=\n",data)transfer = MinMaxScaler() #默认0-1data_new = transfer.fit_transform(data)print("data_new=\n",data_new)

if name == ‘main’:
minmax_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13





从图中可以看出,如果有异常值对结果的影响并不大


代码与上例类似此处略








def variance_demo():"""过滤低方差特征:return:"""# 1、获取数据data = pd.read_csv("factor_returns.csv")data = data.iloc[:, 1:-2]print("data:\n", data)
# 2、实例化一个转换器类
transfer = VarianceThreshold(threshold=10)# 3、调用fit_transform
data_new = transfer.fit_transform(data)
print("data_new:\n", data_new, data_new.shape)# 计算某两个变量之间的相关系数
r1 = pearsonr(data["pe_ratio"], data["pb_ratio"])
print("相关系数:\n", r1)
r2 = pearsonr(data['revenue'], data['total_expense'])
print("revenue与total_expense之间的相关性:\n", r2)return None

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25



一个小例子














现在将数据放在一个二维的空间直角坐标系中

现在我们想办法把二维的数据降成一维(一条直线)

这样我们可以看出,虽然是降成一维,但是由原来的五个点变成三个点,有数据损失了,于是:

由上图我们就可以完成降维的任务了,我们还应该考虑让所有点到直线的距离之和最小。



代码如下:

from sklearn.decomposition  import PCA

def pca_demo():
data = [[2,8,4,5],[6,3,0,8],[5,4,9,1]]
transfer = PCA(n_components=2) #降维成两个特征
data_new = transfer.fit_transform(data)
print(“data_new:\n”,data_new)
return None
if name == ‘main’:
pca_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


这里提到一个instacart案例,有点麻烦这里参考(https://www.bilibili.com/video/av39137333/?p=17)

上文总结


分类算法





其中:





代码如下:

from sklearn.datasets import load_iris
from sklearn.model_selection import  train_test_split
from sklearn.preprocessing import  StandardScaler
from sklearn.neighbors import KNeighborsClassifier

def knn_iris():
#获取数据
iris = load_iris()
#划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22)
#特征工程:标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
#KNN算法预估器
estimator = KNeighborsClassifier(n_neighbors=3)
estimator.fit(x_train,y_train)
#模型评估
#方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print(“y_predict:\n”,y_predict)
print(“直接比对真实值和预测值:\n”,y_test == y_predict)
#方法2:计算准确率
score = estimator.score(x_test,y_test)
print(“准确率为:\n”,score)
return None

if name == ‘main’:
knn_iris()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190414205938893.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDUxNzMwMQ==,size_16,color_FFFFFF,t_70






对鸢尾花数实例进行选择调优

from sklearn.datasets import load_iris
from sklearn.model_selection import  train_test_split
from sklearn.preprocessing import  StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
def knn_iris_gscv():#添加网格搜索和交叉验证#获取数据iris  = load_iris()#划分数据集x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22)#特征工程:标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.fit_transform(x_test)#KNN算法预估器estimator = KNeighborsClassifier() #不用添加k值了#网格搜索与交叉验证#数据准备param_data = {"n_neighbors":[1,3,5,7,9,11]}estimator = GridSearchCV(estimator,param_grid=param_data,cv=10)
estimator.fit(x_train,y_train)
#模型评估
#方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n",y_predict)
print("直接比对真实值和预测值:\n",y_test == y_predict)
#方法2:计算准确率
score = estimator.score(x_test,y_test)
print("准确率为:\n",score)
#最佳参数
print("最佳参数:\n",estimator.best_params_)
#最佳结果
print("最佳结果:\n",estimator.best_score_)
#最佳估计器
print("最佳估计器:\n",estimator.best_estimator_)
#交叉验证结果
print("交叉验证结果:\n",estimator.cv_results_)
return None

if name == ‘main’:
knn_iris_gscv()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43


一个比较大的实例:预测facebook签到位置有点难啊嗷嗷嗷

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV

def facebook_demo():
data = pd.read_csv(“F:/python_work/train.csv”)

#缩小数据范围
data = data.query("x<2.5 & x>2 & y<1.5 & y>1")#处理时间特征
#转换为年月日时分秒
time_value = pd.to_datetime(data["time"],unit="s")
date = pd.DatetimeIndex(time_value)
#人工排除年和月两个信息
data["day"] = date.day
data["weekday"] = date.weekday
data["hour"] = date.hour
print(data)
#过滤掉签到次数少的地方
#先统计每个地点被签到的次数
place_count = data.groupby("place_id").count()[ "row_id"]
place_count[place_count>3]
data_final=data[data["place_id"].isin(place_count[place_count>3].index.values)]
# 筛选特征值和目标值
x = data_final[["x", "y", "accuracy", "day", "weekday", "hour"]]
y = data_final["place_id"]
# 数据集划分
x_train, x_test, y_train, y_test = train_test_split(x, y)
# 3)特征工程:标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)# 4)KNN算法预估器
estimator = KNeighborsClassifier()# 加入网格搜索与交叉验证
# 参数准备
param_dict = {"n_neighbors": [3, 5, 7, 9]}
estimator = GridSearchCV(estimator, param_grid=param_dict, cv=3)
estimator.fit(x_train, y_train)
# 5)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)# 最佳参数:best_params_
print("最佳参数:\n", estimator.best_params_)
# 最佳结果:best_score_
print("最佳结果:\n", estimator.best_score_)
# 最佳估计器:best_estimator_
print("最佳估计器:\n", estimator.best_estimator_)
# 交叉验证结果:cv_results_
print("交叉验证结果:\n", estimator.cv_results_)

if name == ‘main’:
facebook_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

朴素贝叶斯算法原理(假设特征与特征之间的相互独立的)






在这里我们计算出p(Tokyo|C)=0;





from sklearn.datasets import fetch_20newsgroups
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
def nb_news():"""用朴素贝叶斯算法对新闻进行分类:return:"""# 1)获取数据news = fetch_20newsgroups(subset="all")
# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(news.data, news.target)# 3)特征工程:文本特征抽取-tfidf
transfer = TfidfVectorizer()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)# 4)朴素贝叶斯算法预估器流程
estimator = MultinomialNB()
estimator.fit(x_train, y_train)# 5)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)return None

if name == ‘main’:
nb_news()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

决策树





例:





以鸢尾花数据集为例实现决策树

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_graphviz
def decision_iris():"""用决策树对鸢尾花进行分类:return:"""# 1)获取数据集iris = load_iris()
# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)# 3)决策树预估器
estimator = DecisionTreeClassifier(criterion="entropy")
estimator.fit(x_train, y_train)# 4)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)# 可视化决策树
export_graphviz(estimator, out_file="iris_tree.dot", feature_names=iris.feature_names)return None

if name == ‘main’:
decision_iris()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34



这时,文件里的内容我们还是看不懂的,于是我们需要把内容放到一个生成树的网站里
(https://webgraphviz.com/)

点击网站最下面的


用决策树实现泰坦尼克号乘客生存预测


代码:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier, export_graphviz
def titanic():# 1、获取数据path = "http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt"titanic = pd.read_csv(path)# 筛选特征值和目标值x = titanic[["pclass", "age", "sex"]]y = titanic["survived"]# 2、数据处理# 1)缺失值处理x["age"].fillna(x["age"].mean(), inplace=True) #填充平均值# 2) 转换成字典x = x.to_dict(orient="records")# 3、数据集划分x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)# 4、字典特征抽取transfer = DictVectorizer()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 3)决策树预估器estimator = DecisionTreeClassifier(criterion="entropy", max_depth=8)estimator.fit(x_train, y_train)
# 4)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)# 可视化决策树
export_graphviz(estimator, out_file="titanic_tree.dot", feature_names=transfer.get_feature_names())

if name == ‘main’:
titanic()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

随机森林






用随机森林实现泰坦尼克号实例

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.feature_extraction import DictVectorizer
def suijisanli_demo():# 1、获取数据path = "http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt"titanic = pd.read_csv(path)# 筛选特征值和目标值x = titanic[["pclass", "age", "sex"]]y = titanic["survived"]# 2、数据处理# 1)缺失值处理x["age"].fillna(x["age"].mean(), inplace=True)# 2) 转换成字典x = x.to_dict(orient="records")# 3、数据集划分x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)# 4、字典特征抽取transfer = DictVectorizer()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)#随机森林预估器estimator = RandomForestClassifier()# 加入网格搜索与交叉验证# 参数准备param_dict = {"n_estimators": [120, 200, 300, 500, 800, 1200], "max_depth": [5, 8, 15, 25, 30]}estimator = GridSearchCV(estimator, param_grid=param_dict, cv=3)estimator.fit(x_train, y_train)
# 5)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)# 最佳参数:best_params_
print("最佳参数:\n", estimator.best_params_)
# 最佳结果:best_score_
print("最佳结果:\n", estimator.best_score_)
# 最佳估计器:best_estimator_
print("最佳估计器:\n", estimator.best_estimator_)
# 交叉验证结果:cv_results_
print("交叉验证结果:\n", estimator.cv_results_)

if name == ‘main’:
suijisanli_demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52


回归与聚类算法








注:线性关系一定是线性模型,线性模型不一定是线性关系


即:目标就是找到一条直线,使所有点到直线的距离之和最小,即误差最小

优化方法一、正规方程

优化方法二、梯度下降







代码如下:

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.metrics import mean_squared_error
def linear1():"""正规方程的优化方法对波士顿房价进行预测:return:"""# 1)获取数据boston = load_boston()
# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)# 3)标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)# 4)预估器
estimator = LinearRegression()
estimator.fit(x_train, y_train)# 5)得出模型
print("正规方程-权重系数为:\n", estimator.coef_)
print("正规方程-偏置为:\n", estimator.intercept_)# 6)模型评估
y_predict = estimator.predict(x_test)
print("预测房价:\n", y_predict)
error = mean_squared_error(y_test, y_predict)
print("正规方程-均方误差为:\n", error)return None

def linear2():
“”"
梯度下降的优化方法对波士顿房价进行预测
:return:
“”"
# 1)获取数据
boston = load_boston()
print(“特征数量:\n”, boston.data.shape)

# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)# 3)标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)# 4)预估器
estimator = SGDRegressor(learning_rate="constant", eta0=0.01, max_iter=10000, penalty="l1")
estimator.fit(x_train, y_train)# 5)得出模型
print("梯度下降-权重系数为:\n", estimator.coef_)
print("梯度下降-偏置为:\n", estimator.intercept_)# 6)模型评估
y_predict = estimator.predict(x_test)
print("预测房价:\n", y_predict)
error = mean_squared_error(y_test, y_predict)
print("梯度下降-均方误差为:\n", error)return None

if name == ‘main’:
linear1()
linear2()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74


两种方法的对比




以计算机识别天鹅为例,第一种欠拟合,第二种过拟合





hw(xi)为预测值,yi为真实值,L1正则化就是把wj²改为|wj|



from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor,Ridge
from sklearn.metrics import mean_squared_error

def linear3():
“”"
岭回归对波士顿房价进行预测
:return:
“”"
# 1)获取数据
boston = load_boston()
print(“特征数量:\n”, boston.data.shape)

# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)# 3)标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)#4)预估器
estimator = Ridge(alpha=0.5, max_iter=10000)
estimator.fit(x_train, y_train)# 5)得出模型
print("岭回归-权重系数为:\n", estimator.coef_)
print("岭回归-偏置为:\n", estimator.intercept_)# 6)模型评估
y_predict = estimator.predict(x_test)
print("预测房价:\n", y_predict)
error = mean_squared_error(y_test, y_predict)
print("岭回归-均方误差为:\n", error)return None

if name == ‘main’:
linear3()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
















总结:



from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import pandas as pd
import numpy as np

def cancer():
# 1、读取数据
path = “https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data”
column_name = [‘Sample code number’, ‘Clump Thickness’, ‘Uniformity of Cell Size’, ‘Uniformity of Cell Shape’,
‘Marginal Adhesion’, ‘Single Epithelial Cell Size’, ‘Bare Nuclei’, ‘Bland Chromatin’,
‘Normal Nucleoli’, ‘Mitoses’, ‘Class’]

data = pd.read_csv(path, names=column_name)
# 2、缺失值处理
# 1)替换-》np.nan
data = data.replace(to_replace="?", value=np.nan)
# 2)删除缺失样本
data.dropna(inplace=True)
data.isnull().any()  # 检验,不存在缺失值
# 3、划分数据集
# 筛选特征值和目标值
x = data.iloc[:, 1:-1]
y = data["Class"]
x_train, x_test, y_train, y_test = train_test_split(x, y)
# 4、标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 5、预估器流程
estimator = LogisticRegression()
estimator.fit(x_train, y_train)
# 5)得出模型
print("逻辑回归-权重系数为:\n", estimator.coef_)
print("逻辑回归-偏置为:\n", estimator.intercept_)
# 6、模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)

if name == ‘main’:
cancer()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47








from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import pandas as pd
import numpy as np
from sklearn.metrics import classification_report
from sklearn.metrics import roc_auc_score
def cancer():# 1、读取数据path = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"column_name = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape','Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin','Normal Nucleoli', 'Mitoses', 'Class']
data = pd.read_csv(path, names=column_name)
# 2、缺失值处理
# 1)替换-》np.nan
data = data.replace(to_replace="?", value=np.nan)
# 2)删除缺失样本
data.dropna(inplace=True)
data.isnull().any()  # 检验,不存在缺失值
# 3、划分数据集
# 筛选特征值和目标值
x = data.iloc[:, 1:-1]
y = data["Class"]
x_train, x_test, y_train, y_test = train_test_split(x, y)
# 4、标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 5、预估器流程
estimator = LogisticRegression()
estimator.fit(x_train, y_train)
# 5)得出模型
print("逻辑回归-权重系数为:\n", estimator.coef_)
print("逻辑回归-偏置为:\n", estimator.intercept_)
# 6、模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("准确率为:\n", score)
# 查看精确率、召回率、F1-score
report = classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"])
print(report)
# y_true:每个样本的真实类别,必须为0(反例),1(正例)标记
# 将y_test 转换成 0 1
y_true = np.where(y_test > 3, 1, 0)
score = roc_auc_score(y_true, y_predict)
print(score)

if name == ‘main’:
cancer()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55















import pandas as pd
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

def lunkuoxishu():
# 1、获取数据
order_products = pd.read_csv("./instacart/order_products__prior.csv")
products = pd.read_csv("./instacart/products.csv")
orders = pd.read_csv("./instacart/orders.csv")
aisles = pd.read_csv("./instacart/aisles.csv")
# 2、合并表
# order_products__prior.csv:订单与商品信息

# 字段:order_id, product_id, add_to_cart_order, reordered
# products.csv:商品信息
# 字段:product_id, product_name, aisle_id, department_id
# orders.csv:用户的订单信息
# 字段:order_id,user_id,eval_set,order_number,….
# aisles.csv:商品所属具体物品类别
# 字段: aisle_id, aisle# 合并aisles和products aisle和product_id
tab1 = pd.merge(aisles, products, on=["aisle_id", "aisle_id"])
tab2 = pd.merge(tab1, order_products, on=["product_id", "product_id"])
tab3 = pd.merge(tab2, orders, on=["order_id", "order_id"])
# 3、找到user_id和aisle之间的关系
table = pd.crosstab(tab3["user_id"], tab3["aisle"])
data = table[:10000]
# 4、PCA降维
# 1)实例化一个转换器类
transfer = PCA(n_components=0.95)# 2)调用fit_transform
data_new = transfer.fit_transform(data)
data_new.shape
# 预估器流程
estimator = KMeans(n_clusters=3)
estimator.fit(data_new)
y_predict = estimator.predict(data_new)
y_predict[:300]
# 模型评估-轮廓系数
silhouette_score(data_new, y_predict)

if name == ‘main’:
lunkuoxishu()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46


**参考:**https://blog.csdn.net/weixin_44517301/article/details/88405939

3天入门Python机器学习(黑马程序员)相关推荐

  1. 机器学习入门笔记 人工智能 黑马程序员

    机器学习入门笔记 前言 这个是根据"黑马程序员"的一套机器学习课程编写的博客.这篇博客上的案例均为课程里面的,但并没有包含所有案例,只是记录了部分. B站视频地址 文章目录 机器学 ...

  2. JVM入门笔记-摘自黑马程序员

    JVM笔记 这是一篇JVM小白的入门笔记,适合刚接触JVM的伙伴们入门参考. 笔记是完全按照我看的视频来的,说实话内容不深很好理解,主要的作用就是把所有的知识点串起来让你不会迷茫,就像画画一样先描好轮 ...

  3. C++入门学习(黑马程序员课程讲义)——第一阶段

    1 C++初识 1.1 编写C++程序步骤 四个:创建项目.创建文件.编写代码.运行程序 1.2 注释 单行注释://描述信息 (通常放在一行代码的上方,或者一条语句的末尾) 多行注释:/描述信息/ ...

  4. Uni-App从入门到实战-黑马程序员【uniapp基础知识讲义笔记】

    想要课程资料的同学可以留言,留下你的邮箱哦 ####uni-app的基本使用 课程介绍: 基础部分: 环境搭建 页面外观配置 数据绑定 uni-app的生命周期 组件的使用 uni-app中样式学习 ...

  5. python培训班黑马程序员

    Python的热潮还没过啊,后台很多人私信我问我没有时间还想优雅的学Python,今天总结整理了经验.工作之余应该怎么学Python? 本文简要目录: - 基于不同目的学习的角度- Python基础学 ...

  6. java基础入门课后答案黑马程序员

    前言 最近刷到了一句耐人寻味的话,"解决雪崩问题的最好办法是不发生雪崩". 不论是在硅谷互联网公司里还是在国内的互联网平台上,曾多次遇到过海量规模的交易瞬间吞噬平台的悲惨故事. 核 ...

  7. 项目前期准备 -- 手把手教你做ssm+springboot入门后端项目黑马程序员瑞吉外卖(一)

    文章目录 前言 一.导学内容 1.前置知识(必备) 2.博客收获 3.效果展示 4.软件开发流程整体介绍 4.瑞吉外卖整体项目介绍 二.开发环境搭建 1.数据库环境搭建 2.maven环境搭建 总结 ...

  8. 《黑马程序员》通讯录管理系统实战

    通讯录管理系统 想直接看源码直达:https://blog.csdn.net/Augenstern_QXL/article/details/116982366 更新:源码我设置的是0cb/积分下载,但 ...

  9. 《黑马程序员》职工管理系统实战

    职工管理系统 源码成品直达:https://download.csdn.net/download/Augenstern_QXL/19080725?spm=1001.2014.3001.5501 已经设 ...

  10. 《黑马程序员》演讲比赛管理系统实战

    演讲比赛流程管理系统 更新:源码我设置的是0cb/积分下载,但是csdn貌似会自动随着浏览量进行自己变动积分下载,需要的可下方评论留邮箱,看到了会发送 演讲比赛流程管理系统源码直达:https://d ...

最新文章

  1. VASP计算光学性质
  2. 关于Input内容改变的触发事件
  3. java中什么时候不能用泛型_java中泛型的正确使用姿势
  4. oracle中将number类型毫秒值转为时间类型
  5. miniui datagrid 隐藏列默认赋值_Qt商业组件DataGrid:内置视图和布局详解(一)
  6. 2019递归实现字符串的逆序存储(C++)
  7. 小鹏汽车9月总交付10412台 成为新造车势力中第一家月交付过万的企业
  8. uva1583-digit generator
  9. 数字信号处理matlab版答案,数字信号处理matlab版答案
  10. 星星之火-49:5G 移动边缘计算MEC快览
  11. 北大软微计算机技术硕士复试,【高分学长带你飞】北大软微408学长超详经验贴...
  12. 人脸识别系统的活体检测技术
  13. bp神经网络综合评价例题,bp神经网络综合评价法
  14. 2018永洪科技大数据技术上海峰会-唤醒数据.遇见未来
  15. android自定义View 中秋节放个烟花吧~
  16. 无向有权图的邻接矩阵实现(C++)
  17. 使用QImage生成纯透明png图片
  18. 三星临时取消Galaxy Fold中国发布会;视觉中国网站部分恢复上线?官方回应:并未恢复;特斯拉周一发布完全自动驾驶将网络直播 | 雷锋早报...
  19. html页面设计参考文献英文,网页设计网站参考文献(国外英文资料).doc
  20. CA(电子签名)与HIS等医疗信息化系统的集成简介

热门文章

  1. 应用编程课程4.系统IO和标准IO的比较,自己做的一些理解,仅仅方便自己看
  2. 看见阿里的语音识别系统很好
  3. React组件的用法和理解
  4. 如何高效实现多窗口卖票
  5. 用计算机语言写祝福语,最新用化学语言写祝福语2篇,.docx
  6. 30支队伍将在阿布扎比角逐奖金达500万美元的2020年穆罕默德-本-扎耶德国际机器人挑战赛
  7. 电脑xp系统坏了,如何处理
  8. 11-golang流程控制
  9. 宾得k5ii_【有图】小众宾得K5II学习一月有余,谈谈感想,发点作业-蜂鸟网
  10. windows、Linux、VxWorks 下errno含义