# coding: utf-8

# In[6]:

import pandas as pd
import numpy as np
from sklearn import tree
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import binarize
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import Normalizer
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score,recall_score,average_precision_score,auc
from imblearn.over_sampling import SMOTE

# In[7]:

data= pd.read_csv(r"D:\Users\sgg91044\Desktop\Copy of sampling.csv")

# In[8]:

data.iloc[:,5:22] = data.iloc[:,5:22].apply(pd.to_numeric,errors='coerce')

# In[9]:

data.head()

# In[10]:

data.Target = data.Target.astype("category")

# In[11]:

Y = data.Target
X = data.drop(columns='Target')

# In[13]:

X=X.drop(columns=['Recipe_Name','defect_count'])

# In[14]:

X.head()

# In[64]:

X=X.drop(columns=['defect_count'])
X.head()

# In[8]:

for i in range(0,18):
med = np.median(X.iloc[:,i][X.iloc[:,i].isna() == False])
X.iloc[:,i] = X.iloc[:,i].fillna(med)

# In[9]:

nz = Normalizer()
X.iloc[:,10:12]=pd.DataFrame(nz.fit_transform(X.iloc[:,10:12]),columns=X.iloc[:,10:12].columns)
X.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(X.iloc[:,0:3]),columns=X.iloc[:,0:3].columns)
X

# In[15]:

X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.2, random_state=0)

# In[16]:

sm = SMOTE(random_state=12, ratio = 1.0)
x_train_smote, y_train_smote = sm.fit_sample(X_train, y_train)

# In[17]:

print(y_train.value_counts(), np.bincount(y_train_smote))

# In[18]:

from sklearn.ensemble import RandomForestClassifier

# Make the random forest classifier
random_forest = RandomForestClassifier(n_estimators = 100, random_state = 50, verbose = 1, oob_score = True, n_jobs = -1)

# In[19]:

# Train on the training data
random_forest.fit(x_train_smote,y_train_smote)

# In[20]:

rm_trans=random_forest.transform()

# In[21]:

# Make predictions on the test data
y_pred = random_forest.predict(X_test)

# In[22]:

print(classification_report(y_pred=y_pred,y_true=y_test))

# In[23]:

print(confusion_matrix(y_pred=y_pred,y_true=y_test))

# In[24]:

f1_score(y_pred=y_pred,y_true=y_test)

# In[25]:

print("Accuracy of Random_forest:",round(accuracy_score(y_pred=y_pred,y_true=y_test) * 100,2),"%")

# In[26]:

print("Sensitivity of Random_forest:",round(recall_score(y_pred=y_pred,y_true=y_test)*100,2),"%")

我的代码- rf sampling的更多相关文章

  1. LoRa术语

    ADR      Adaptive Data Rate          自适应数据率 AES      Advanced Encryption Standard        高级加密标准 AFA  ...

  2. 智能灯控(基于ZigBee)

    时间:2017年12月 阶段:大二上学期 背景:单片机原理与应用课设 名称:智能灯控 摘要 本系统实现了多方式控灯功能,有按键控灯.串口指令控灯.点对点无线射频控灯.AI模式控灯.其中AI模式控灯是通 ...

  3. RF 测试代码体会

    这里的需求是完成一个cc2540 RF测试程序.实现功能为开发板按键控制 RF 发射频率的改变. 首先被告知要用PTM来做这个测试程序,然后我去了PTM的介绍文档,地址为 http://process ...

  4. SMOTE RF MLP demo use cross_val_score to find best argument 处理不平衡数据的demo代码 先做smote处理 再用交叉验证找到最好的模型参数 实践表明MLP更好

    # _*_coding:UTF-8_*_ from sklearn.externals.six import StringIO from sklearn import tree import pydo ...

  5. 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting

    一.序言   陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...

  6. 决策树(中)-集成学习、RF、AdaBoost、Boost Tree、GBDT

    参考资料(要是对于本文的理解不够透彻,必须将以下博客认知阅读): 1. https://zhuanlan.zhihu.com/p/86263786 2.https://blog.csdn.net/li ...

  7. MATLAB的PLOT函数线型设置及横坐标为字符串的代码实例

    2.横坐标为字符串的代码实例 cell={‘PLS’,’SVM’,’RF’,’NNET’,’NB’,’PLR’,’C5.0′,’PDA’,’KNN’,’GLM’,’BCT’};%分类方法yData=[ ...

  8. 某互联网后台自动化组合测试框架RF+Sikuli+Python脚本

    某互联网后台自动化组合测试框架RF+Sikuli+Python脚本 http://www.jianshu.com/p/b3e204c8651a 字数949 阅读323 评论1 喜欢0 一.**Robo ...

  9. 【iOS 使用github上传代码】详解

    [iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...

随机推荐

  1. 小白的python之路10/29 文件归档

    一打包解包文件 [root@localhost ~]# cd /test/[root@localhost test]# touch a.txt b.txt c.txt[root@localhost t ...

  2. MSSQL转Mysql常用函数,语法等

    MSSQL转Mysql常用 一.字段类型 MSSQL Mysql 备注 "nchar" "char()" 最大长度为255 "nvarchar&quo ...

  3. CentOS-7-1804下MySQL安装及防火墙设置

    第一步,下载MySQL Linux 版本安装包,这里使用 这个版本. 第二步,上传安装包到Linux系统中. 第三步,解压安装包 tar -zxvf mysql--linux-glibc2.-x86_ ...

  4. 在win中,给powershell客户端,搭建sshd服务器。

    下载:https://github.com/PowerShell/Win32-OpenSSH/releases     问:为什么要用这个sshd?答:这是微软用,openssh官方的源码,源码网址: ...

  5. 关于Django的配置

    一. 下载与安装Django             登录Django的官网,https://www.djangoproject.com/download/,在网页的左边有下载压缩包,下载后解压在py ...

  6. Python的布尔值与空值

    1.Boolean值(布尔值) 一个布尔值只有Ture.False两种值 b1 =True b2 =False print (b1,b2)>>>True False 2.空值(non ...

  7. python flask 如何修改默认端口号

    场景:按照github文档上启动一个flask的app,默认是用5000端口,如果5000端口被占用,启动失败. 样例代码: from flask import Flask app = Flask(_ ...

  8. windows7 下jdk1.8 改为 1.7过程,及环境变量配置;

    一. 安装完成后 当然是配置环境变量: JAVA_HOME 是我们以后用到jdk所在位置的内容时为了方便,给jdk起的名字,也是java程序约定的变量名,如果没有,当你在cmd 输入 java  ja ...

  9. elasticsearch(5) 请求体搜索

    上一篇提到的轻量搜索非常简单便捷,但是通过请求体查询可以更充分的利用查询的强大功能.因为_search api中大部分参数是通过HTTP请求体而非查询字符串来传递的. 一 空查询 对于空查询来说,最简 ...

  10. python基础17_列表推导式 vs 生成器表达式

    [ ] 列表推导式,是用简单的语法来生成列表, ( ) 生成器表达式,是用简单的语法创建个生成器. 外观上仅括号不一样. 虽然写起来方便,但是读起来稍显费力,另外,不易调试. # 列表推导式 prin ...