我的代码- rf sampling
# 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的更多相关文章
- LoRa术语
ADR Adaptive Data Rate 自适应数据率 AES Advanced Encryption Standard 高级加密标准 AFA ...
- 智能灯控(基于ZigBee)
时间:2017年12月 阶段:大二上学期 背景:单片机原理与应用课设 名称:智能灯控 摘要 本系统实现了多方式控灯功能,有按键控灯.串口指令控灯.点对点无线射频控灯.AI模式控灯.其中AI模式控灯是通 ...
- RF 测试代码体会
这里的需求是完成一个cc2540 RF测试程序.实现功能为开发板按键控制 RF 发射频率的改变. 首先被告知要用PTM来做这个测试程序,然后我去了PTM的介绍文档,地址为 http://process ...
- 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 ...
- 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
一.序言 陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...
- 决策树(中)-集成学习、RF、AdaBoost、Boost Tree、GBDT
参考资料(要是对于本文的理解不够透彻,必须将以下博客认知阅读): 1. https://zhuanlan.zhihu.com/p/86263786 2.https://blog.csdn.net/li ...
- MATLAB的PLOT函数线型设置及横坐标为字符串的代码实例
2.横坐标为字符串的代码实例 cell={‘PLS’,’SVM’,’RF’,’NNET’,’NB’,’PLR’,’C5.0′,’PDA’,’KNN’,’GLM’,’BCT’};%分类方法yData=[ ...
- 某互联网后台自动化组合测试框架RF+Sikuli+Python脚本
某互联网后台自动化组合测试框架RF+Sikuli+Python脚本 http://www.jianshu.com/p/b3e204c8651a 字数949 阅读323 评论1 喜欢0 一.**Robo ...
- 【iOS 使用github上传代码】详解
[iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...
随机推荐
- SSM搭建遇到的坑
1,Error:(6, 24) java: package org.junit.runner does not exist 错误原因: 当时傻傻的把zl 包(单元测试包)放在了src/main/jav ...
- 互粉的sql查询
建立表: CREATE TABLE `tb_sns_attention` ( `id` int(11) NOT NULL AUTO_INCREMENT, `to_uid` int(11) NOT NU ...
- 白鹭wing的自动编译
1.lunch.json文件中的"preLaunchTask":"build",注释掉. 2.安装node-v6.11.2-x64.msi 3.安装tsc 命令 ...
- 说一说js中的闭包
不说官方给的定义,感觉自己刚开始看也不是很理解. 闭包 : 其实就是 该函数能使用函数外定义的变量. 为什么要使用闭包? 首先来说一下局部变量和全局变量的危害: 全局变量容易全局污染,局部变量又无法共 ...
- JDBC原理
JDBC原理 JDBC是什么: Java Database Connectivity:Java访问数据库的解决方案 JDBC是Java应用程序访问数据库的里程碑式解决方案.Java研发者希望用相同 ...
- hadoop streaming字段排序介绍
我们在使用hadoop streaming的时候默认streaming的map和reduce的separator不指定的话,map和reduce会根据它们默认的分隔符来进行排序 map.reduce: ...
- node作为中间层 —> 跨域请求java后台接口(使用http-proxy-middleware中间件)
情景:利用node的express 作为中间层,跨域调取java后台接口,由于java接口对session有判断,因此每次请求都必须在req的headers中需要带上cookie,否则接口报500错误 ...
- ffmpeg奇数分辨率转码失败
偶然遇到将目的分辨率设置成奇数(例如:854x481)导致ffmpeg转码失败(错误:width not divisible by 2 (854x481)) 于是去查了一下原因:由于转码后的pix_f ...
- 如何制作exe小程序
1.构建maven项目 需要有一个main入口函数 添加M2_HOME及,java_home的环境变量 打开Preference->Java->Installed JREs->Edi ...
- NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接
NHibernate数据库配置参数在hibernate.cfg.xml中 <?xml version="1.0" encoding="utf-8"?> ...