# 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. 常用的lamp环境以及一些依赖包的安装

  2. SQL Server数据库开发的二十一条军规

    如果你正在负责一个基于SQL Server的项目,或者你刚刚接触SQL Server,你都有可能要面临一些数据库性能的问题,这篇文章会为你提供一些有用的指导(其中大多数也可以用于其它的DBMS).在这 ...

  3. 低版本IDE 打开 高版本 IDE 代码时 unit

    可以用单元别名 比如Vcl.Forms=Forms 来兼容.

  4. git add.后回退 代码丢失

    记录一次操作git丢失代码的过程: 写完代码后:git staus git add. git status 发现有一堆.class 文件不想提交,想着代码回退到add 之前,使用了 git log 开 ...

  5. Django--Managers

    Django--Managers Manager 概念: 1.Manager是Django中的数据模型,可以通过manager进行对数据库的查询操作.可以看其结构它本身是一个空的类,其主要的功能来自于 ...

  6. Python3的基础

    Python的3.0版本,常被称为Python 3000,或简称Py3k. 关于Python版本的下载:https://www.python.org/,以及Anaconda的下载:https://ww ...

  7. Java Spring 在线程中或其他位置获取 ApplicationContext 或 ServiceBean

    部分一转载自:http://blog.csdn.net/yang123111/article/details/32099329 via @yang123111 部分二转载自:http://www.cn ...

  8. MySQL【日期和时间处理函数】的使用方法

    名称 调用示例 示例结果 描述 NOW NOW() 2018-09-19 09:24:10 返回当前日期和时间 CURDATE CURDATE() 2018-09-19 返回当前日期 CURTIME ...

  9. eclipse中添加jstl标签支持(引入头)

    https://blog.csdn.net/wangyuxuan_java/article/details/8580318

  10. MySQL免编译二进制包安装简记

    相比较于MySQL的源代码安装来说.免编译二进制包的速度实在是快了太多,而且性能损失也不是很大,同时具有一定的定制性.所以,如果没有特殊的 需求,尽量用MySQL免编译二进制包来安装MySQL. 1. ...