from tensorflow. keras. layers import Dense, Input, Activation
from tensorflow. keras. models import Sequential
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn. model_selection import train_test_split
from sklearn. metrics import accuracy_score
model = Sequential( )
model. add( Dense( units= 20 , input_dim= 2 , activation= 'sigmoid' ) )
model. add( Dense( units= 1 , activation= 'sigmoid' ) )
model. summary( )
model. compile ( loss= 'categorical_crossentropy' , optimizer= 'sgd' , metrics= [ 'accuracy' ] ) data = pd. read_csv( 'D:/pythonDATA/data.csv' )
X = data. drop( [ 'y' ] , axis= 1 )
y = data. loc[ : , 'y' ]
fig1 = plt. figure( figsize= ( 5 , 5 ) )
passed = plt. scatter( X. loc[ : , 'x1' ] [ y == 1 ] , X. loc[ : , 'x2' ] [ y == 1 ] )
filed = plt. scatter( X. loc[ : , 'x1' ] [ y == 0 ] , X. loc[ : , 'x2' ] [ y == 0 ] )
plt. legend( ( passed, filed) , ( 'passed' , 'filed' ) )
plt. xlabel( 'x1' )
plt. ylabel( 'x2' )
plt. title( 'raw data' )
plt. show( )
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size= 0.33 , random_state= 10 )
model. compile ( optimizer= 'adam' , loss= 'binary_crossentropy' )
model. fit( X_train, y_train, epochs= 3000 )
y_test_predict = model. predict_classes( X_test)
accuracy_test = accuracy_score( y_test, y_test_predict)
xx, yy = np. meshgrid( np. arange( 0 , 1 , 0.01 ) , np. arange( 0 , 1 , 0.01 ) )
x_range = np. c_[ xx. ravel( ) , yy. ravel( ) ]
print ( "输出坐标:" )
print ( x_range)
y_range_predict = model. predict_classes( x_range)
y_range_predict_form = pd. Series( i[ 0 ] for i in y_range_predict) print ( y_range_predict_form)
print ( accuracy_test) fig2 = plt. figure( figsize= ( 5 , 5 ) )
passed_predict = plt. scatter( x_range. loc[ : , 'x1' ] [ y == 1 ] , x_range. loc[ : , 'x2' ] [ y == 1 ] )
filed_predict = plt. scatter( x_range. loc[ : , 'x1' ] [ y == 0 ] , x_range. loc[ : , 'x2' ] [ y == 0 ] ) passed = plt. scatter( X. loc[ : , 'x1' ] [ y == 1 ] , X. loc[ : , 'x2' ] [ y == 1 ] )
filed = plt. scatter( X. loc[ : , 'x1' ] [ y == 0 ] , X. loc[ : , 'x2' ] [ y == 0 ] ) plt. legend( ( passed, filed, passed_predict, filed_predict) , ( 'passed' , 'filed' , 'passed_predict' , 'filed_predict' ) )
plt. xlabel( 'x1' )
plt. ylabel( 'x2' )
plt. title( 'raw result' )
plt. show( )