kmeans
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
X, y = iris.data, iris.target
data = X[:,[1,3]] # 为了便于可视化,只取两个维度
plt.scatter(data[:,0],data[:,1]);
def distance(p1,p2):
"""
Return Eclud distance between two points.
p1 = np.array([0,0]), p2 = np.array([1,1]) => 1.414
"""
tmp = np.sum((p1-p2)**2)
return np.sqrt(tmp)
def rand_center(data,k):
"""Generate k center within the range of data set."""
n = data.shape[1] # features
centroids = np.zeros((k,n)) # init with (0,0)....
for i in range(n):
dmin, dmax = np.min(data[:,i]), np.max(data[:,i])
centroids[:,i] = dmin + (dmax - dmin) * np.random.rand(k)
return centroids
def rand_center(data,k):
"""Generate k center within the range of data set."""
n = data.shape[1] # features
centroids = np.zeros((k,n)) # init with (0,0)....
for i in range(n):
dmin, dmax = np.min(data[:,i]), np.max(data[:,i])
centroids[:,i] = dmin + (dmax - dmin) * np.random.rand(k)
return centroids
def kmeans(data, k=3):
def _distance(p1, p2):
"""
Return Eclud distance between two points.
p1 = np.array([0,0]), p2 = np.array([1,1]) => 1.414
"""
tmp = np.sum((p1 - p2) ** 2)
return np.sqrt(tmp)
def _rand_center(data, k):
"""Generate k center within the range of data set."""
n = data.shape[1] # features
centroids = np.zeros((k, n)) # init with (0,0)....
for i in range(n):
dmin, dmax = np.min(data[:, i]), np.max(data[:, i])
centroids[:, i] = dmin + (dmax - dmin) * np.random.rand(k)
return centroids
def _converged(centroids1, centroids2):
# if centroids not changed, we say 'converged'
set1 = set([tuple(c) for c in centroids1])
set2 = set([tuple(c) for c in centroids2])
return (set1 == set2)
n = data.shape[0] # number of entries
centroids = _rand_center(data, k)
label = np.zeros(n, dtype=np.int) # track the nearest centroid
assement = np.zeros(n) # for the assement of our model
converged = False
while not converged:
old_centroids = np.copy(centroids)
for i in range(n):
# determine the nearest centroid and track it with label
min_dist, min_index = np.inf, -1
for j in range(k):
dist = _distance(data[i], centroids[j])
if dist < min_dist:
min_dist, min_index = dist, j
label[i] = j
assement[i] = _distance(data[i], centroids[label[i]]) ** 2
# update centroid
for m in range(k):
centroids[m] = np.mean(data[label == m], axis=0)
converged = _converged(old_centroids, centroids)
return centroids, label, np.sum(assement)
best_assement = np.inf
best_centroids = None
best_label = None
for i in range(10):
centroids, label, assement = kmeans(data,3)
if assement < best_assement:
best_assement = assement
best_centroids = centroids
best_label = label
data0 = data[best_label==0]
data1 = data[best_label==1]
data2=data[best_label==1]
fig, (ax1,ax2) = plt.subplots(1,2,figsize=(12,5))
ax1.scatter(data[:,0],data[:,1],c='c',s=30,marker='o')
ax2.scatter(data0[:,0],data0[:,1],c='r')
ax2.scatter(data1[:,0],data1[:,1],c='c')
ax2.scatter(data2[:,0],data2[:,1],c='y')
ax2.scatter(centroids[:,0],centroids[:,1],c='b',s=120,marker='o')
plt.show()
knn
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 14 11:49:49 2018
@author: Robo Crio
"""
class KNNClassify():
def __init__(self,k=5, p=2):
self.k = k
self.p = p
self._X_train = None
self._y_train = None
def fit(self, X_train, y_train):
self._X_train = X_train
self._y_train = y_train
return self
def predict_y(self, X_test):
m = self._X_train.shape[0]
y_pre = []
for intX in X_test:
minus_mat = np.fabs(np.tile(intX, (m, 1)) - self._X_train) # 将新的实例复制成m行1列,并进行相减
sq_minus_mat = minus_mat ** self.p
sq_distance = sq_minus_mat.sum(axis=1)
diff_sq_distance = sq_distance ** float(1/self.p)
sorted_distance_index = diff_sq_distance.argsort() # 记录距离最近的k个点的索引
class_count = {}
vola = []
for i in range(self.k):
vola = self._y_train[sorted_distance_index[i]]
class_count[vola] = class_count.get(vola, 0) + 1 # 统计k个点中所属各个类别的实例数目
sorted_class_count = sorted(class_count.items(), key=operator.itemgetter(1), reverse=True) # 返回列表,元素为元组。每个类别以及对应的实例数目
y_pre.append((sorted_class_count[0][0]))
return (np.array(y_pre))
def score(self, X_test, y_test):
j = 0
for i in range(len(self.predict_y(X_test))):
if self.predict_y(X_test)[i] == y_test[i]:
j += 1
return ('accuracy: {:.10%}'.format(j / len(y_test)))
import numpy as np
import operator
from sklearn import datasets
from sklearn.model_selection import train_test_split
# 获取数据集,并进行8:2切分
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 定义分类器的实例,并进行拟合预测
f = KNNClassify()
f.fit(X_train, y_train)
y_pre = f.predict_y(X_test)
accuracy = f.score(X_test, y_test)
print(y_test)
print(y_pre)
print(accuracy)
神经网络
from sklearn import neural_network
import matplotlib.pyplot as plt
import numpy as np
x_data =np.linspace(-10,10,500)
y_data=x_data**2+3*x_data+1
x_test =np.linspace(-10,10,500)
y_test=x_test**2+3*x_test+1
mlp=neural_network.MLPRegressor(hidden_layer_sizes=(40,20 ), activation='relu',
solver='adam', alpha=0.0001, batch_size='auto',
learning_rate='constant', learning_rate_init=0.001,
power_t=0.5, max_iter=10000)
mlp.fit(np.asarray(x_data).reshape([-1,1]),y_data)
print(mlp.loss_)
pre=mlp.predict(x_test.reshape([-1,1]))
plt.plot(np.array(x_test),np.array(y_test),c='b',label='real')
plt.plot(np.array(x_test),np.array(pre),c='r',label='nihe')
plt.legend()
plt.show()