2.加载数据
dataSet = pd.read_csv("data\\winequality-red.csv");
#print(dataSet.head());
pd.set_option('display.max_columns',None);# DataFrame 显示所有列
#print("describe",dataSet.describe());
print("shape",np.shape(dataSet));
输出
3.数据分析
dataSet.hist();#直方图
plt.show();
#变量的相关性 展示
sns.heatmap(dataSet.astype(float).corr(),linewidths=0.1,vmax=1.0, square=True,linecolor='white', annot=True)
plt.show();
输出
可以看出quality 数值主要在5,6附近 我们可以以5为分界点
4.划分x,y 训练集和测试集
以y1作为target值
y = dataSet["quality"]; #将 quality 作为target
x = dataSet.drop("quality",axis=1); # 其他作为属性
# 将葡萄酒分成两组 >5为 好酒 为1 小于5为1
y1 = (y>5).astype(int); #改变np.array中所有数据元素的数据类型。
#print(y1);
# 分 训练集合测试集合 7:3
xtrain,xtest,ytrain,ytext = model_selection.train_test_split(x,y1,test_size=0.3,random_state=1);
5.交叉验证 用训练集 进行交叉训练 看看不进行调参的 score为多少
RF_clf = RandomForestClassifier();
cv_score = model_selection.cross_val_score(RF_clf,xtrain,ytrain,scoring='accuracy',cv=10); #scoring='accuracy' 正确率
print("cv_score",cv_score);
print("cv_score_mean",cv_score.mean()); #mean()平均值
输出
此时 什么参数都没有调 平均的score已经达到0.7998
6.调参
我们首先调n_estimators
默认的n_estimators值是100,
我们让n_estimators从50开始 每次加10 最高到300 运行看平均score有什么变化
n_estimators_score = [];
for n in np.arange(50,300,10):
RF_clf = RandomForestClassifier(n_estimators=n);
cv_score = model_selection.cross_val_score(RF_clf, xtrain, ytrain, scoring='accuracy',cv=10); # scoring='accuracy' 正确率
n_estimators_score.append(cv_score.mean());
plt.figure();
plt.plot(np.arange(50,300,10),n_estimators_score);
plt.show();
输出
我们可以看到此时score在0.7825-0.801波动
在70左右取得较高的值
所有我们进一步 让n_estimators从60取到80 看看具体那一个值最好
这次我们直接用GridSearchCV
param_grid = {'n_estimators':range(60,80,1)};
rfc1 = model_selection.GridSearchCV(RandomForestClassifier(),param_grid=param_grid,cv=5);
rfc1.fit(xtrain,ytrain);
print("best_params_",rfc1.best_params_);
print("best_score_",rfc1.best_score_);
输出
现在就以n_estimators=79为值 调一下max_depth
param_grid = {'n_estimators':[79],'max_depth':range(5,30,1)};
rfc1 = model_selection.GridSearchCV(RandomForestClassifier(),param_grid=param_grid,cv=5);
rfc1.fit(xtrain,ytrain);
print("best_params_",rfc1.best_params_);
print("best_score_",rfc1.best_score_);
输出
增长了0.003左右
我们继续在以上两个值的基础上 调一下min_samples_leaf
param_grid = {'n_estimators':[79],'max_depth':[24],'min_samples_leaf':range(1,10,1)};
rfc1 = model_selection.GridSearchCV(RandomForestClassifier(),param_grid=param_grid,cv=5);
rfc1.fit(xtrain,ytrain);
print("best_params_",rfc1.best_params_);
print("best_score_",rfc1.best_score_);
输出
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。