INT104-lab10 测试新数据集
Posted Jason-Cow
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了INT104-lab10 测试新数据集相关的知识,希望对你有一定的参考价值。
1 import numpy as np 2 from sklearn.manifold import TSNE 3 import matplotlib.pyplot as plt 4 from mpl_toolkits.mplot3d import Axes3D 5 6 7 def read(path: str) -> list: 8 with open(path, "r") as f: 9 text = f.readlines() 10 D = [] 11 for row in text: 12 substr = str.split(str.split(row, "\\n")[0], ",") 13 X = [] 14 for a in substr: 15 if a == \'?\': 16 continue 17 X.append(int(a)) 18 D.append(X) 19 return D 20 21 22 def init(D: list) -> tuple: 23 n, m = len(D), len(D[0]) 24 X, Y = [], [] 25 for i in range(n): 26 x = [] 27 if len(D[i]) != m: 28 n -= 1 29 continue 30 for j in range(1, m - 1, 1): 31 x.append(D[i][j]) 32 X.append(x) 33 Y.append(D[i][m - 1]) 34 # print(n, m) 35 return X, Y, n, m - 2 36 37 38 if __name__ == \'__main__\': 39 40 D = read("breast-cancer-wisconsin.data") 41 X, Y, n, m = init(D) 42 43 x = np.array(X) 44 45 tsne = TSNE(n_components=3) 46 47 tsne.fit_transform(x) 48 49 one_x, one_y, one_z, zero_x, zero_y, zero_z = [], [], [], [], [], [] 50 for i in range(n): 51 _x, _y, _z = tsne.embedding_[i][0], tsne.embedding_[i][1], tsne.embedding_[i][2] 52 if Y[i] == 4: 53 zero_x.append(_x) 54 zero_y.append(_y) 55 zero_z.append(_z) 56 else: 57 one_x.append(_x) 58 one_y.append(_y) 59 one_z.append(_z) 60 \'\'\' 61 ax = plt.axes(projection=\'3d\') 62 ax.scatter3D(one_x, one_y, one_z) 63 ax.scatter3D(zero_x, zero_y, zero_z) 64 \'\'\' 65 plt.subplot(311) 66 plt.scatter(one_x, one_y) 67 plt.scatter(zero_x, zero_y) 68 plt.subplot(312) 69 plt.scatter(one_x, one_z) 70 plt.scatter(zero_x, zero_z) 71 plt.subplot(313) 72 plt.scatter(one_y, one_z) 73 plt.scatter(zero_y, zero_z) 74 75 plt.show()
以上是关于INT104-lab10 测试新数据集的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用