我的代码-test models


# coding: utf-8

# In[2]:


import pandas as pd
import numpy as np
from sklearn.preprocessing import binarize
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import Normalizer
data=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data.iloc[:,8:10]),columns=data.iloc[:,8:10].columns)
data.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data.iloc[:,0:3]),columns=data.iloc[:,0:3].columns)


# In[1]:


import pandas as pd
import numpy as np


# In[151]:


data=pd.read_csv(r"D:\Users\sgg91044\Desktop\auto_data\5mins_data_2.csv")
data.head()


# In[152]:


data=data.iloc[:,1:]
data.drop(['ooc','oos'],axis=1,inplace=True)
data.drop(["waferid","Step","finishtime","parametername"],axis=1,inplace=True)
data.columns = ["eqpid","chamber","lotid","wafer","param_name","recipe","data"]
pivoted = data.pivot_table(index=['eqpid','chamber','lotid','wafer','recipe'],columns="param_name",values="data",aggfunc=np.sum)
pivoted.reset_index(inplace=True)
columns=["eqpid","chamber","lotid","wafer","recipe","ETCM_PHA4","ETCM_PHB4","ETCM_PHC4","HELK_MEAN","LOWERCHM_PRESS","PBK4","RR13_MAX.","RR13_MEAN","RR23_MAX.","RR23_MEAN","THR3_MAX.","THR3_MAX._DIFF","THR3_MEAN","THR3_MEAN_DIFF","THR3_MEAN_SLOPE"]
final = pd.DataFrame(columns = columns)
final = final.merge(pivoted,how="right").reindex_axis(columns, axis=1)


# In[153]:


final


# In[154]:


#normalize
final=final.drop(columns=["eqpid","chamber","lotid","wafer","recipe"])
final= final.dropna(axis=0, how='any')

 

# In[155]:


final


# In[156]:


final.iloc[:,8:10]=nz.transform(final.iloc[:,8:10])
final


# In[157]:


final.iloc[:,0:3]=nz.transform(final.iloc[:,0:3])


# In[158]:


final


# In[20]:


#SUM_ETCM
final["SUM_ETCM"]=np.array(final.ETCM_PHA4)+np.array(final.ETCM_PHB4)+np.array(final.ETCM_PHC4)
final


# In[166]:


data=pd.read_csv(r"D:\Users\sgg91044\Desktop\sep_oct_data\sep01-03.csv")
data.head()


# In[167]:


data=data.drop(columns=['ooc','oos','RNK'])
pivoted = data.pivot_table(index=['eqpid','Chamber','lotid','slotid','stage','Recipie_Name','finishtime'],columns="Param_Name",values="data1",aggfunc=np.sum)


# In[171]:


pivoted


# In[173]:

 

 

# In[177]:


import pandas as pd
import numpy as np
pivoted.reset_index(inplace=True)
columns=["eqpid","Chamber","lotid","slotid","Recipie_Name","ETCM_PHA4","ETCM_PHB4","ETCM_PHC4","HELK_MEAN","LOWERCHM_PRESS","PBK4","RR13_MAX.","RR13_MEAN","RR23_MAX.","RR23_MEAN","THR3_MAX.","THR3_MAX._DIFF","THR3_MEAN","THR3_MEAN_DIFF","THR3_MEAN_SLOPE"]
final = pd.DataFrame(columns = columns)
final = final.merge(pivoted,how="right").reindex_axis(columns, axis=1)
final= final.dropna(axis=0, how='any')


# In[180]:


index=final.drop(columns=["ETCM_PHA4","ETCM_PHB4","ETCM_PHC4","HELK_MEAN","LOWERCHM_PRESS","PBK4","RR13_MAX.","RR13_MEAN","RR23_MAX.","RR23_MEAN","THR3_MAX.","THR3_MAX._DIFF","THR3_MEAN","THR3_MEAN_DIFF","THR3_MEAN_SLOPE"])
final=final.drop(columns=["eqpid","Chamber","lotid","slotid","Recipie_Name"])
final.head()


# In[181]:


final.iloc[:,8:10]=nz.transform(final.iloc[:,8:10])
final.iloc[:,0:3]=nz.transform(final.iloc[:,0:3])
#SUM_ETCM
final["SUM_ETCM"]=np.array(final.ETCM_PHA4)+np.array(final.ETCM_PHB4)+np.array(final.ETCM_PHC4)


# In[182]:


final.head()


# In[185]:


from sklearn.externals import joblib
random_forest=joblib.load(r'D:\Users\sgg91044\Desktop\deployment\model_RF.pkl')


# In[186]:


final_pred = random_forest.predict_proba(final)


# In[194]:


final_pred
final_pred=pd.DataFrame(final_pred)


# In[195]:


index.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep01-03index.csv')
final.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep01-03data.csv')
final_pred.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep01-03result.csv')


# In[196]:


index.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep01-03index1.csv')


# In[217]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov.csv')


# In[218]:


data.head()


# In[219]:


data=data.drop(columns=["layer_id","ooc","oos"])
pivoted = data.pivot_table(index=['eqpid','Chamber','lotid','slotid','defect_count','stage','Recipie_Name','finishtime'],columns="Param_Name",values="data1",aggfunc=np.sum)


# In[220]:


pivoted


# In[226]:


pivoted.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_pivoted.csv')


# In[227]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_pivoted.csv')
data.head()


# In[228]:


Index=data.drop(columns=["ETCM_PHA4","ETCM_PHB4","ETCM_PHC4","HELK_MEAN","LOWERCHM_PRESS","PBK4","RR13_MAX.","RR13_MEAN","RR23_MAX.","RR23_MEAN","THR3_MAX.","THR3_MAX._DIFF","THR3_MEAN","THR3_MEAN_DIFF","THR3_MEAN_SLOPE","Target"])
Index.head()


# In[229]:


data=data.drop(columns=["eqpid","Chamber","lotid","slotid","defect_count","stage","Recipie_Name","finishtime"])
data.head()


# In[232]:


good_wafer= data[data.Target ==0]
good_wafer


# In[233]:


good_wafer.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_good.csv')


# In[234]:


good_wafer= pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_good.csv')
good_wafer.head()
good_wafer=good_wafer.drop(columns=["eqpid","Chamber","lotid","slotid","defect_count","stage","Recipie_Name","finishtime"])


# In[240]:


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


# In[242]:


good_wafer.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_good_imputed.csv')


# In[236]:


bad_wafer= data[data.Target ==1]
bad_wafer
bad_wafer.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_badd.csv')


# In[213]:


bad_wafer= pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep_oct_bad.csv')
bad_wafer.head()
bad_wafer=bad_wafer.drop(columns=["eqpid","Chamber","lotid","slotid","defect_count","stage","Recipie_Name","finishtime"])


# In[237]:


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


# In[238]:


bad_wafer.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_bad_imputed.csv')


# In[259]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep_oct_good_imputed.csv')


# In[260]:


data.head()


# In[261]:


data=data.drop(columns=['lotid','defect_count','stage','Recipie_Name','finishtime'])
data.head()


# In[262]:


data.eqpid = data.eqpid.astype("category")
data.Chamber = data.Chamber.astype("category")
le = LabelEncoder()
data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
data["slot_encoded"] = le.fit_transform(data.iloc[:,2])
data['chamber_encoded'] = le.fit_transform(data.iloc[:,1])


# In[263]:


Trace_back = pd.concat([data[["eqpid","Chamber","slotid"]],data[["eqp_encoded","chamber_encoded","slot_encoded"]]],axis=1)
data.head(20)


# In[264]:


data.drop(columns=['eqpid','Chamber','slotid'],inplace=True)
data.head()


# In[265]:


data.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep_oct_good_imputed_edcoded.csv')


# In[266]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep_oct_good_imputed_edcoded.csv')


# In[267]:


data.head()


# In[268]:


data=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data.iloc[:,8:10]),columns=data.iloc[:,8:10].columns)
data.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data.iloc[:,0:3]),columns=data.iloc[:,0:3].columns)


# In[277]:


data1=pd.read_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\Nov_good_imputed.csv')


# In[278]:


data1.head(20)


# In[279]:


data1.iloc[:,11:13]=nz.transform(data1.iloc[:,11:13])
data1.iloc[:,3:6]=nz.transform(data1.iloc[:,3:6])


# In[280]:


data1["SUM_ETCM"]=np.array(data1.ETCM_PHA4)+np.array(data1.ETCM_PHB4)+np.array(data1.ETCM_PHC4)


# In[281]:


data1["eqp_encoded"] = le.transform(data1.iloc[:,0])
data1["slot_encoded"] = le.transform(data1.iloc[:,2])
data1['chamber_encoded'] = le.transform(data1.iloc[:,1])


# In[276]:


data1.to_csv(r'D:\Users\sgg91044\Desktop\sep_oct_data\sep_oct_good_imputed_encoded.csv')


# In[3]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\data_01-10\data1.csv')
data.head()


# In[4]:


data=data.drop(columns=["step",'parametername'])
data.head()


# In[5]:


pivoted = data.pivot_table(index=['eqpid','chamber','lotid','slotid','stage','recipe','finishtime'],columns="Param_Name",values="data1",aggfunc=np.sum)


# In[6]:


pivoted.to_csv(r'D:\Users\sgg91044\Desktop\data_01-10\data1_pivoted.csv')


# In[30]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\data_01-10\data1_pivoted.csv')
data


# In[31]:


data=data.drop(columns=["stage","finishtime","recipe",'HELK_MAX.'])
data.head()


# In[32]:


for i in range(4,19):
med = np.median(data.iloc[:,i][data.iloc[:,i].isna() == False])
data.iloc[:,i] = data.iloc[:,i].fillna(med)


# In[33]:


data1=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data1.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data1.iloc[:,8:10]),columns=data1.iloc[:,8:10].columns)
data1.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data1.iloc[:,0:3]),columns=data1.iloc[:,0:3].columns)
data.iloc[:,12:14]=nz.transform(data.iloc[:,12:14])
data.iloc[:,4:7]=nz.transform(data.iloc[:,4:7])


# In[34]:


def encode_eqpid(eqpid):
return int(eqpid[-2:])-1

def encode_chamber(chamber):
if chamber == 'A':
return 0
else:
return 1

def encode_slotid(slotid):
if slotid > 0:
return slotid-1

data.eqpid = data.eqpid.apply(encode_eqpid)
data.chamber = data.chamber.apply(encode_chamber)
data.slotid = data.slotid.apply(encode_slotid)
data.eqpid = data.eqpid.astype("category")
data.chamber = data.chamber.astype("category")
data.slotid = data.slotid.astype("category")


# In[35]:


data=data.drop(columns=["lotid"])


# In[36]:


data


# In[107]:

 

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


# In[167]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\model_data_1.csv')


# In[168]:


data.eqpid1 = data.eqpid1.astype("category")
data.chamber1 = data.chamber1.astype("category")
data.wafer1 = data.wafer1.astype("category")


# In[169]:


data.Target = data.Target.astype("category")
y = data.Target
X = data.drop(columns='Target')
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=8)
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=12, ratio = 1.0)
x_train_smote, y_train_smote = sm.fit_sample(X_train, y_train)
from sklearn.ensemble import RandomForestClassifier

# Make the random forest classifier
random_forest = RandomForestClassifier(n_estimators = 100, random_state = 50, oob_score = True, verbose = 1, n_jobs = -1)
random_forest.fit(x_train_smote,y_train_smote)
y_pred = random_forest.predict(X_test)
#print(classification_report(y_pred=y_pred,y_true=y_test))


# In[170]:


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


# In[135]:


from sklearn.externals import joblib

joblib.dump(random_forest, r'D:\Users\sgg91044\Desktop\deployment\model_RF_test.pkl')


# In[136]:


from sklearn.externals import joblib
random_forest=joblib.load(r'D:\Users\sgg91044\Desktop\deployment\model_RF_test.pkl')


# In[221]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\Some wafer Examples from Engg team\OK\test_AD_ok_pivoted_15.csv')


# In[222]:


data.head()


# In[223]:


lotid=data.lotid
data=data.drop(columns=['lotid','recipe','finishtime'])


# In[224]:


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


# In[225]:


data1=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data1.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data1.iloc[:,8:10]),columns=data1.iloc[:,8:10].columns)
data1.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data1.iloc[:,0:3]),columns=data1.iloc[:,0:3].columns)
data.iloc[:,11:13]=nz.transform(data.iloc[:,11:13])
data.iloc[:,3:6]=nz.transform(data.iloc[:,3:6])


# In[226]:


data.head()


# In[227]:


data["SUM_ETCM"]=np.array(data.ETCM_PHA4)+np.array(data.ETCM_PHB4)+np.array(data.ETCM_PHC4)


# In[228]:


def encode_eqpid(eqpid1):
return int(eqpid1[-2:])-1

def encode_chamber(chamber1):
if chamber1 == 'A':
return 0
else:
return 1

def encode_slotid(wafer1):
if wafer1 > 0:
return wafer1-1

data.eqpid1 = data.eqpid1.apply(encode_eqpid)
data.chamber1 = data.chamber1.apply(encode_chamber)
data.wafer1 = data.wafer1.apply(encode_slotid)
data.eqpid1 = data.eqpid1.astype("category")
data.chamber1 = data.chamber1.astype("category")
data.wafer1 = data.wafer1.astype("category")


# In[ ]:


data.to_csv()


# In[229]:


y_pred = random_forest.predict(data)


# In[230]:


y_pred=pd.DataFrame(y_pred)


# In[219]:


y_pred.to_csv(r'D:\Users\sgg91044\Desktop\y_pred_ok.csv')


# In[220]:


lotid.to_csv(r'D:\Users\sgg91044\Desktop\lotid_ok.csv')


# In[37]:


data["SUM_ETCM"]=np.array(data.ETCM_PHA4)+np.array(data.ETCM_PHB4)+np.array(data.ETCM_PHC4)


# In[38]:


data.head()


# In[43]:


data.to_csv(r'D:\Users\sgg91044\Desktop\data_01-10\data1_test.csv')


# In[137]:


data1=pd.read_csv(r'D:\Users\sgg91044\Desktop\AEM2_pivotdata_12-13-10-29.csv')
data1.head()
data1.eqpid1 = data1.eqpid1.astype("category")
data1.chamber1 = data1.chamber1.astype("category")
data1.wafer1 = data1.wafer1.astype("category")


# In[138]:


y_pred = random_forest.predict(data1)


# In[139]:


print(y_pred)


# In[204]:


data1=pd.read_csv(r'D:\Users\sgg91044\Desktop\Some wafer Examples from Engg team\OK\test_AD_ok.csv')
data1.head()


# In[205]:


data1=data1.drop(columns=['waferid','Step'])
pivoted = data1.pivot_table(index=['eqpid','chamber','lotid','wafer','recipe','finishtime'],columns="param_name",values="data",aggfunc=np.sum)


# In[206]:


pivoted.to_csv(r'D:\Users\sgg91044\Desktop\Some wafer Examples from Engg team\OK\test_AD_ok_pivoted.csv')


# In[152]:


data2=pd.read_csv(r'D:\Users\sgg91044\Desktop\all_parameters.csv')
data2.head()


# In[153]:


data2=data2.drop(columns=['layerid','waferid','stg','stage','step','parametername','ooc','oos','RNK'])
pivoted = data2.pivot_table(index=['eqpid','chamber','lotid','sloitid','defect','recipe','finishtime'],columns="param_name",values="data",aggfunc=np.sum)


# In[154]:


pivoted.to_csv(r'D:\Users\sgg91044\Desktop\all_parameters_pivoted.csv')


# In[159]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\all_parameters_deleted.csv')


# In[160]:


data.head()


# In[161]:


for i in range(7,61):
med = np.median(data.iloc[:,i][data.iloc[:,i].isna() == False])
data.iloc[:,i] = data.iloc[:,i].fillna(med)
data


# In[162]:


data=data.drop(columns=['lotid','defect','recipe','finishtime'])
data.head()


# In[163]:


def encode_eqpid(eqpid):
return int(eqpid[-2:])-1

def encode_chamber(chamber):
if chamber == 'A':
return 0
else:
return 1

def encode_slotid(slotid):
if slotid > 0:
return slotid-1

data.eqpid = data.eqpid.apply(encode_eqpid)
data.chamber = data.chamber.apply(encode_chamber)
data.slotid = data.slotid.apply(encode_slotid)
data.eqpid = data.eqpid.astype("category")
data.chamber = data.chamber.astype("category")
data.slotid = data.slotid.astype("category")


# In[164]:


data.head()


# In[165]:


data.Target = data.Target.astype("category")
y = data.Target
X = data.drop(columns='Target')
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=8)
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=12, ratio = 1.0)
x_train_smote, y_train_smote = sm.fit_sample(X_train, y_train)
from sklearn.ensemble import RandomForestClassifier

# Make the random forest classifier
random_forest = RandomForestClassifier(n_estimators = 100, random_state = 50, oob_score = True, verbose = 1, n_jobs = -1)
random_forest.fit(x_train_smote,y_train_smote)
y_pred = random_forest.predict(X_test)
#print(classification_report(y_pred=y_pred,y_true=y_test))


# In[166]:


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


# In[234]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\data_01-10\data_pivoted.csv')
data


# In[236]:


data=data.drop(columns=['layer_id','stage','Recipie_Name','finishtime','defect_count','lotid'])
data.head()


# In[238]:


for i in range(3,18):
med = np.median(data.iloc[:,i][data.iloc[:,i].isna() == False])
data.iloc[:,i] = data.iloc[:,i].fillna(med)
data1=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data1.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data1.iloc[:,8:10]),columns=data1.iloc[:,8:10].columns)
data1.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data1.iloc[:,0:3]),columns=data1.iloc[:,0:3].columns)
data.iloc[:,11:13]=nz.transform(data.iloc[:,11:13])
data.iloc[:,3:6]=nz.transform(data.iloc[:,3:6])


# In[239]:


def encode_eqpid(eqpid):
return int(eqpid[-2:])-1

def encode_chamber(Chamber):
if Chamber == 'A':
return 0
else:
return 1

def encode_slotid(slotid):
if slotid > 0:
return slotid-1

data.eqpid = data.eqpid.apply(encode_eqpid)
data.Chamber = data.Chamber.apply(encode_chamber)
data.slotid = data.slotid.apply(encode_slotid)
data.eqpid = data.eqpid.astype("category")
data.Chamber = data.Chamber.astype("category")
data.slotid = data.slotid.astype("category")
data.head()


# In[240]:


data["SUM_ETCM"]=np.array(data.ETCM_PHA4)+np.array(data.ETCM_PHB4)+np.array(data.ETCM_PHC4)
data.Target = data.Target.astype("category")
y = data.Target
X = data.drop(columns='Target')
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=8)
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=12, ratio = 1.0)
x_train_smote, y_train_smote = sm.fit_sample(X_train, y_train)
from sklearn.ensemble import RandomForestClassifier

# Make the random forest classifier
random_forest = RandomForestClassifier(n_estimators = 100, random_state = 50, oob_score = True, verbose = 1, n_jobs = -1)
random_forest.fit(x_train_smote,y_train_smote)
y_pred = random_forest.predict(X_test)
#print(classification_report(y_pred=y_pred,y_true=y_test))


# In[241]:


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


# In[242]:


from sklearn.externals import joblib

joblib.dump(random_forest, r'D:\Users\sgg91044\Desktop\deployment\model_RF_Mclass.pkl')


# In[250]:


data=pd.read_csv(r'D:\Users\sgg91044\Desktop\Some wafer Examples from Engg team\Defective\test_AD_15.csv')
lotid=data.lotid
data=data.drop(columns=['lotid','recipe','finishtime'])
for i in range(3,18):
med = np.median(data.iloc[:,i][data.iloc[:,i].isna() == False])
data.iloc[:,i] = data.iloc[:,i].fillna(med)
data1=pd.read_csv(r"D:\Users\sgg91044\Desktop\normalizing_example.csv")
#le = LabelEncoder()
#data["eqp_encoded"] = le.fit_transform(data.iloc[:,0])
#data["slot_encoded"] = le.fit_transform(data.iloc[:,1])
#data['chamber_encoded'] = le.fit_transform(data.iloc[:,2])
nz = Normalizer()
data1.iloc[:,8:10]=pd.DataFrame(nz.fit_transform(data1.iloc[:,8:10]),columns=data1.iloc[:,8:10].columns)
data1.iloc[:,0:3]=pd.DataFrame(nz.fit_transform(data1.iloc[:,0:3]),columns=data1.iloc[:,0:3].columns)
data.iloc[:,11:13]=nz.transform(data.iloc[:,11:13])
data.iloc[:,3:6]=nz.transform(data.iloc[:,3:6])
data.head()


# In[251]:


def encode_eqpid(eqpid):
return int(eqpid[-2:])-1

def encode_chamber(Chamber):
if Chamber == 'A':
return 0
else:
return 1

def encode_slotid(slotid):
if slotid > 0:
return slotid-1

data.eqpid = data.eqpid.apply(encode_eqpid)
data.Chamber = data.Chamber.apply(encode_chamber)
data.slotid = data.slotid.apply(encode_slotid)
data.eqpid = data.eqpid.astype("category")
data.Chamber = data.Chamber.astype("category")
data.slotid = data.slotid.astype("category")
data.head()


# In[252]:


data["SUM_ETCM"]=np.array(data.ETCM_PHA4)+np.array(data.ETCM_PHB4)+np.array(data.ETCM_PHC4)
from sklearn.externals import joblib
random_forest=joblib.load(r'D:\Users\sgg91044\Desktop\deployment\model_RF_Mclass.pkl')


# In[253]:


y_pred = random_forest.predict(data)


# In[254]:


y_pred=pd.DataFrame(y_pred)
y_pred.to_csv(r'D:\Users\sgg91044\Desktop\y_pred_defect.csv')

 

posted on 2018-12-19 10:26  Aimee0207  阅读(126)  评论(0编辑  收藏  举报

导航