实战(二):芯片质量预测:
1、基于chip test..csv数据,建立逻辑回归模型(二阶边界),评估模型表现;
2、以函数的方式求解边界曲线
3、描绘出完整的决策边界曲线
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error,r2_score
from sklearn.metrics import accuracy_score
# load data;visualization
data = pd.read_csv(r'D:/tencent/qicq/chip_test.csv')
data.head()
mask = data.loc[:,'pass']==1
fig1 = plt.figure()
passed = plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed = plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.title('Chip Test')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
# define X,y
x = data.drop(['pass'],axis=1)
y = data.loc[:,'pass']
X1 = data.loc[:,'test1']
X2 = data.loc[:,'test2']
# generate the new data
X1_2 = X1*X1
X2_2 = X2*X2
X1_X2 = X1*X2
X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}
X_new = pd.DataFrame(X_new)
X_new.head()
print(X_new)
# Establish the new model and train
LR2 = LogisticRegression()
LR2.fit(X_new,y)
# Evaluate the model
y2_predict = LR2.predict(X_new)
accuarcy2 = accuracy_score(y,y2_predict)
print(f'accuarcy2 : {accuarcy2}')
#boundary
X1_new = X1.sort_values()
print(X1,X1_new)
theat0 = LR2.intercept_
theat1,theat2,theat3,theat4,theat5 = LR2.coef_[0][0],LR2.coef_[0][1],LR2.coef_[0][2],LR2.coef_[0][3],LR2.coef_[0][4]
print(theat0,theat1,theat2,theat3,theat4,theat5)
a = theat4
b = theat5*X1_new+theat2
c = theat0 + theat1*X1_new+theat3*X1_new*X1_new
X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)
X2_new_boundary2 = (-b-np.sqrt(b*b-4*a*c))/(2*a)
print(f'X2_new_boundary : {X2_new_boundary}')
fig2 = plt.figure()
passed = plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed = plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.plot(X1_new,X2_new_boundary)
plt.plot(X1_new,X2_new_boundary2)
plt.title('Chip Test')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.plot(X1_new,X2_new_boundary,'-m')
plt.show()
# define f(x)
def f(x):
theat0 = LR2.intercept_
theat1,theat2,theat3,theat4,theat5 = LR2.coef_[0][0],LR2.coef_[0][1],LR2.coef_[0][2],LR2.coef_[0][3],LR2.coef_[0][4]
a = theat4
b = theat5*x+theat2
c = theat0 + theat1*x+theat3*x*x
X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)
X2_new_boundary2 = (-b-np.sqrt(b*b-4*a*c))/(2*a)
return X2_new_boundary,X2_new_boundary2
X2_boundary1 = []
X2_boundary2 = []
for x in X1_new:
X2_boundary1.append(f(x)[0])
X2_boundary2.append(f(x)[1])
# 完整决策边界
X1_range = [-1+x/10000 for x in range(0,20000)]
X1_range - np.array(X1_range)
X2_boundary3 = []
X2_boundary4 = []
for x in X1_range:
X2_boundary3.append(f(x)[0])
X2_boundary4.append(f(x)[1])
fig4 = plt.figure()
passed = plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed = plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.plot(X1_range,X2_boundary3)
plt.plot(X1_range,X2_boundary4)
plt.title('Chip Test')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.show()