import numpy
import numpy as np
import csv
from sklearn.model_selection import train_test_split, ShuffleSplit
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
2.2 一折验证
data_filename = os.path.join(os.getcwd(),"ionosphere.data")# print(data_filename)
x = numpy.zeros((351,34),dtype='float')
y = np.zeros((351,),dtype='bool')withopen(data_filename,'r')as input_file:
reader = csv.reader(input_file)for i,row inenumerate(reader):
data =[float(datum)for datum in row[:-1]]
x[i]= data
y[i]= row[-1]=='g'
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=14)
estimator = KNeighborsClassifier(n_neighbors=1)
estimator.fit(x_train,y_train)
y_predicted = estimator.predict(x_test)
accuracy = np.mean(y_test==y_predicted)*100print("The accuracuy is {0:.1f}%".format(accuracy))
2.3 k折验证
data_filename = os.path.join(os.getcwd(),"ionosphere.data")# print(data_filename)
x = numpy.zeros((351,34),dtype='float')
y = np.zeros((351,),dtype='bool')withopen(data_filename,'r')as input_file:
reader = csv.reader(input_file)for i,row inenumerate(reader):
data =[float(datum)for datum in row[:-1]]
x[i]= data
y[i]= row[-1]=='g'# cv : set x-fold cross validation
cv = ShuffleSplit(n_splits=10, test_size=0.3, random_state=0)# cross validation
scores = cross_val_score(estimator,x,y,scoring='accuracy',cv=cv)print(scores)
average_accuracy = np.mean(scores)*100print("The accuracuy is {0:.1f}%".format(average_accuracy))
2.4 找到最优k并plot出来
data_filename = os.path.join(os.getcwd(),"ionosphere.data")# print(data_filename)
x = numpy.zeros((351,34),dtype='float')
y = np.zeros((351,),dtype='bool')withopen(data_filename,'r')as input_file:
reader = csv.reader(input_file)for i,row inenumerate(reader):
data =[float(datum)for datum in row[:-1]]
x[i]= data
y[i]= row[-1]=='g'
avg_scores =[]
all_scores =[]
parameter_values =list(range(1,21))for n_neighbors in parameter_values:
estimator = KNeighborsClassifier(n_neighbors=n_neighbors)
scores = cross_val_score(estimator,x,y,scoring='accuracy')
avg_scores.append(np.mean(scores))
all_scores.append(scores)print(all_scores)
plt.plot(parameter_values,avg_scores,'-o')
plt.show()
data_filename = os.path.join(os.getcwd(),"ionosphere.data")
x = numpy.zeros((351,34),dtype='float')
y = np.zeros((351,),dtype='bool')withopen(data_filename,'r')as input_file:
reader = csv.reader(input_file)for i,row inenumerate(reader):
data =[float(datum)for datum in row[:-1]]
x[i]= data
y[i]= row[-1]=='g'
scaling_pipeline = Pipeline([('scale',MinMaxScaler()),('predict',KNeighborsClassifier(n_neighbors=2))])
scores = cross_val_score(scaling_pipeline,x,y,scoring='accuracy')print("The accuracuy is {0:.1f}%".format(np.mean(scores)*100))
2.6.2 循环找最优k
data_filename = os.path.join(os.getcwd(),"ionosphere.data")# print(data_filename)
x = numpy.zeros((351,34),dtype='float')
y = np.zeros((351,),dtype='bool')withopen(data_filename,'r')as input_file:
reader = csv.reader(input_file)for i,row inenumerate(reader):
data =[float(datum)for datum in row[:-1]]
x[i]= data
y[i]= row[-1]=='g'
x_transformed = MinMaxScaler().fit_transform(x)# cv : set x-fold cross validation
cv = ShuffleSplit(n_splits=10, test_size=0.3, random_state=0)
avg_scores =[]
all_scores =[]
parameter_values =list(range(1,21))for n_neighbors in parameter_values:
estimator = KNeighborsClassifier(n_neighbors=n_neighbors)# scores = cross_val_score(estimator,x,y,scoring='accuracy')# pipeline
scaling_pipeline = Pipeline([('scale', MinMaxScaler()),('predict', KNeighborsClassifier(n_neighbors=n_neighbors))])
scores = cross_val_score(scaling_pipeline, x, y, scoring='accuracy')
avg_scores.append(np.mean(scores))
all_scores.append(scores)
plt.plot(parameter_values,avg_scores,'-o')
plt.show()