机器学习100天系列学习笔记 机器学习100天(中文翻译版)机器学习100天(英文原版)
代码阅读:

第一步:导包

#Step 1: Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

第二步:导入数据

#Step 2: Importing the dataset
dataset = pd.read_csv('D:/daily/机器学习100天/100-Days-Of-ML-Code-中文版本/100-Days-Of-ML-Code-master/datasets/Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values

第三步:划分训练集、测试集

#Step 3: Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

第四步:特征缩放

#Step 4: Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

经过特征缩放后的X_train:

[[ 0.58164944 -0.88670699][-0.60673761  1.46173768][-0.01254409 -0.5677824 ][-0.60673761  1.89663484][ 1.37390747 -1.40858358][ 1.47293972  0.99784738][ 0.08648817 -0.79972756][-0.01254409 -0.24885782][-0.21060859 -0.5677824 ]...]

对于进行特征缩放这一步,个人认为是非常重要的,它可以加快计算速度,在深度学习中间尤为重要(梯度爆炸问题)。

第五步:Logistic Regression

#Step 5: Fitting Logistic Regression to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train, y_train)

第六步:预测

#Step 6: Predicting the Test set results
y_pred = classifier.predict(X_test)

第七步:混淆矩阵

#Step 7: Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
cm = confusion_matrix(y_test, y_pred)
print(cm)  # print confusion_matrix
print(classification_report(y_test, y_pred))   # print classification report

混淆:简单理解为一个class被预测成另一个class。
给一个参考链接 混淆矩阵

第八步:可视化

#Step 8: Visualization
from matplotlib.colors import ListedColormap
X_set,y_set = X_train,y_train
X1,X2 = np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:,0].max()+1, step=0.01),np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())for i,j in enumerate(np.unique(y_set)):plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1],c = ListedColormap(('red', 'green'))(i), label=j)
plt. title(' LOGISTIC(Training set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()X_set,y_set=X_test,y_test
X1,X2=np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:, 0].max()+1, step=0.01),np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())
for i,j in enumerate(np. unique(y_set)):plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1],c = ListedColormap(('red', 'green'))(i), label=j)plt. title(' LOGISTIC(Test set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()

可视化这一部分代码可能比较难理解,函数meshgrid是生成一个二维矩阵,大小为X*Y数据个数,这里为(592, 616);函数ravel将矩阵X1(二维)矩阵展开为一维矩阵(364672,);函数reshape将(364672,)转为跟矩阵X1大小一致的二维矩阵;
参数alpha为透明度;函数unique返回参数数组y_set中所有不同的值,并按照从小到大排序,这里返回(0,1);函数enumerate() 用于将一个可遍历的数据对象enumerate;这里循环i,j取0或1,
标签y_setj成立,True=1,标签y_setj不成立,Flase=0,所以0为红色点,1为绿色点。
做出的图为:


全部代码:

#Day 4: Simple Linear Regression 2022/4/7
#Step 1: Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd#Step 2: Importing the dataset
dataset = pd.read_csv('D:/daily/机器学习100天/100-Days-Of-ML-Code-中文版本/100-Days-Of-ML-Code-master/datasets/Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values#Step 3: Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)#Step 4: Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#print(X_train)#Step 5: Fitting Logistic Regression to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train, y_train)#Step 6: Predicting the Test set results
y_pred = classifier.predict(X_test)#Step 7: Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
cm = confusion_matrix(y_test, y_pred)
print(cm)  # print confusion_matrix
print(classification_report(y_test, y_pred))   # print classification report#Step 8: Visualization
from matplotlib.colors import ListedColormap
X_set,y_set = X_train,y_train
X1,X2 = np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:,0].max()+1, step=0.01),np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())for i,j in enumerate(np.unique(y_set)):plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1],c = ListedColormap(('red', 'green'))(i), label=j)
plt. title(' LOGISTIC(Training set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()X_set,y_set=X_test,y_test
X1,X2=np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:, 0].max()+1, step=0.01),np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())
for i,j in enumerate(np. unique(y_set)):plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1],c = ListedColormap(('red', 'green'))(i), label=j)plt. title(' LOGISTIC(Test set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()

ML_Logistic_Regression相关推荐

最新文章

  1. fiash星空动画制作_三维动画制作的详细流程
  2. 超越MobileNetV3!Facebook提出更轻更快的FBNetV2
  3. 用日志记录LINQ中的所有增删改的SQL语句的方法
  4. HBase基本操作命令整理
  5. Test传送门(更新中)
  6. JAVA连接Excel最好用的开源项目EasyExcel,官方使用文档及.jar包下载
  7. 加装的硬盘进入后点不了文件夹_在外接移动硬盘上制作win to go教程
  8. python科学坐标图绘制的四个要素_Python3.0科学计算学习之绘图(四)
  9. Oracle导出导入dmp文件(exp.imp命令行)
  10. 95-080-040-源码-启动-start-cluster.sh
  11. quarts集群 运维_精讲Elastic-job + Quartz实现企业级定时任务
  12. Salesforce LWC学习(二) helloWorld程序在VSCode中的实现
  13. java修改.class重新打包jar
  14. 深度学习中常用的数据集
  15. mysql左连接右连接内连接的含义_MySQL-解释左连接,右连接,内连接,全连接
  16. 使用Inno Setup 打包.NET程序,并自动安装.Net Framework
  17. Android手机sock5代理免root
  18. ace自定义在线编辑器方法及提示
  19. 一个500强公司的数据化运营管理实践 1
  20. PCL笔记二:PCD解析;PCD读取;PCD与XYZ转换;

热门文章

  1. SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)
  2. 基于STM32F103ZET6 HC_SR04超声波测距模块
  3. echarts实现柱状图分页展示
  4. springweb拦截器
  5. 教你打造千万用户的海量视频网站、保卫云端安全!
  6. 对模型方差和偏差的解释之一:过拟合
  7. [Linux] 命令行工具
  8. iOS Provisioning Portal概述
  9. 图文方式管理Linux服务器(Webmin)
  10. Pycharm的.py文件的导入