|
一.数据预处理 1.缺失数据查找 wps实现(最简单的方法) https://zhidao.baidu.com/question/1690641178558189228.html matlab实现
8 T+ Y7 X, {8 ]/ I0 lB = xlsread('C:\Users\DELL\Desktop\新建文件夹\cumcm2012a\附件1-葡萄酒品尝评分表.xls','第一组红葡萄酒品尝评分','C75 84');7 g2 [& i K# l" s) I' {; E
A = B;
9 _ u; ?2 `# z* ~[I,J]= size(A);
! q" F( {2 E% q6 F" A6 y
; G7 i& {/ {2 b- F. Y/ ]8 I5 C# u( Wfor j=1:J
8 }) b e [7 I' B for i=2:I % the 1st row does not contain NaN
V6 d- Q% Q- l) ^( |! @" G& | if isnan(A(i,j)) %通过isnan函数判断该数据是否为NaN类型2 e5 |) z D6 h+ g; P
disp(['(',num2str(i),',',num2str(j),')']); %如果是NaN类型则打印该数据在表格中的位置
7 M# m1 M( D( s' z+ g5 d9 x end ; A l, P. X9 s7 C& r
end
# O8 ]- p: _6 ~. d2 vend
; A7 y% J3 r+ H+ E: _. G7 ~
( ?' {" S i4 z( X% V# [7 dpython实现(https://blog.csdn.net/alanguoo/article/details/77198503)
. J" {2 I8 R0 v* Q ?& H8 f, e8 ^5 [ k" v2 d, ]: Z) ~/ |" G
import pandas as pd/ f1 q" ] q/ d# j+ R" O
& U3 _/ [* {8 m7 D: `8 w( rimport numpy as np
3 Q, B. J# C" `0 J
$ ~. @. a7 I1 u% `df = pd.DataFrame(np.random.randn(10, 6))
! j% W, ]. [5 u$ n( ]+ u
2 z( u" J8 D( c0 D# n5 t' W# Make a few areas have NaN values4 ^9 D' {1 q8 q7 K: K* C- o4 Y. F
) R& s# d; y* O9 [* A
df.iloc[1:3, 1] = np.nan
) {% E; K. t4 H& W
* d$ P" ^7 q/ X0 y; cdf.iloc[5, 3] = np.nan) T: m! F7 z7 r; U+ I$ ~1 D
* G) g8 ]) \, V+ S# C4 H# Ldf.iloc[7:9, 5] = np.nan4 e6 ?, X+ M1 @7 ^9 [8 m6 W8 L
/ w" k: f. e, b( w
print(df)
$ q/ Y) t: d* b
^0 K; [( S1 C5 C: P6 gprint(df.index[np.where(np.isnan(df))[0]]) #打印缺失值的行
{( C j' _& J+ p0 N( v2 U rprint(df.columns[np.where(np.isnan(df))[1]]) #打印缺失值的列
, E( a0 o" e9 Z* r ^
+ Z% E2 G$ Y3 H1 Q2.异常数据查找 z-score标准化(matlab实现)
9 ]! f$ b8 `3 m' g6 T9 ]B = xlsread('C:\Users\DELL\Desktop\新建文件夹\cumcm2012a\附件1-葡萄酒品尝评分表.xls','第一组白葡萄酒品尝评分','D233:M233');
7 I+ n ?) P& N9 {" ?, A%将数据z-score标准化
* ^ y, _ H; s' M6 k/ dA = zscore(B)/ ~; i0 V2 S, s
disp(A)
2 v- l! D/ B' L%获取矩阵的行数和列数,m为行数,n为列数/ _% }: n! D! |; ~
[m,n] = size(A)
. r% d, }+ r- N- u* q. Ifor i = 1:n9 I# P( O+ A) V8 b- |8 y
%确定一个阈值,用来判断是否是异常数据
. L. `3 n* K1 u) A if abs(A(1,i))>2
: y6 B, r# }! E8 ?/ ^ disp(['(1,',num2str(i),')']). H& Y D- x" x% h" U$ j5 n
end. S5 W$ J a( T
end
9 Q+ q. a+ o% D0 m3 N3 `: L' V" ?9 C+ ?* k1 B" [2 r, l* G
二.聚类分析
& P2 m) S" u/ U% ?0 @. O% \) B) U- i: w, v4 b6 z) M
1.一维数据的聚类分析6 d: Q8 e) k: d$ y
- R7 C5 ]- n, X9 Z
参考博文:https://www.biaodianfu.com/clustering-on-a-one-dimensional-array.html5 Z4 i |5 o) N4 e% z0 n6 ~
- Q7 u d- j( s2 d4 l/ V- p0 ~ K
matlab实现(参考博文:https://jingyan.baidu.com/article/0202781154d7ba1bcc9ce5f5.html)
4 }0 a" t+ r( f4 @/ \! S( Q5 Z" ?* K+ r8 F+ y
clc;, c e! |; }' p- i. u2 t( j- X# V
2 O4 f2 B. @! t# ~
clear;! L' m5 I ]+ H9 _8 v* E) h
( ], A; g( M- a, s
yw_data=xlsread('C:\Users\DELL\Desktop\第二问得分.xls'); %读入一维数据样本到yw_data矩阵) g+ n1 i3 U( Z- \9 G
& w1 m, B1 F4 zA = yw_data';
& D9 G1 }# y1 A i9 U
, S' {2 ~& h7 q2 }8 f! f3 Qxx=A;* |5 A* f2 j1 i/ ?6 K! D4 y& z. w0 @
7 t. s7 n+ L& D9 i& m3 [- ^3 aclus=4;; u& C2 P2 G: t- K3 ^. N0 E, y$ R
X. X5 ]& y% C" w0 j) e/ C
[idx,c]=kmeans(xx,clus);
# Q8 g$ ~! Y" J* k: N4 ]' {, ?. y, U8 E1 X
cc1=[];
# l; v4 }0 c: B/ q; {
% ~# ^% ` k4 ]. {8 ^2 E4 N: Kcc2=[];; h0 t5 z( U" I( x7 o* K/ G3 B. X
: R j" b" k: I) Z v) P
cc3=[];8 N( w& ^9 _ d# k3 ~
- ?' N# H0 L& U8 A& `. l/ F
cc4=[];0 ]' f8 T2 r5 D* K4 n
5 _# a0 m- n" D' b# V4 T
[n,m]=size(xx);6 L9 K' z! M% H7 r* x7 z6 H
4 p, i1 C7 s1 f5 Kfor i=1:n3 J( ^) R X) M& y
+ y; N' y: o5 @$ v if idx(i)==1
- Y! W# S/ u) Z1 n9 s' r
R! A4 ^* y- ^2 ~ cc1=[cc1;xx(i)];- \' u& P# U# \. W
$ y ^6 G9 E! O V1 p
elseif idx(i)==2
' f% e6 s d* S" V4 `/ i5 p+ Y: U* a1 [: N! p
cc2=[cc2;xx(i)];
2 M L% u2 `, {" _: L4 @' x; b/ u
elseif idx(i)==3
* m/ O n# \# ]( z0 O1 g( B1 K2 E S
cc3=[cc3;xx(i)];
! U% ]$ t6 o% s) S% u n! o8 V/ @* s7 } P2 o8 P4 D! e7 K
elseif idx(i)==4
, J& s0 F- u& h- o' j3 {/ ^+ P. @7 E4 ]
cc4=[cc4;xx(i)];
' V! b7 B' A6 B) x& P6 l6 e' y) C9 B5 y0 ~
end8 `5 `" c7 T9 M( ~. s1 O- v
0 V7 W y1 W: X) W- C
end+ }, J" w/ V8 |# ]! l
/ ] m' v- ?' p
disp('第一类边界')" o( Z- `" {1 Y. w* L% w
" U" w( S4 k a
[min(cc1),max(cc1)] S$ N( r' y# G" f9 P' S0 \: {; s! e
# j4 n6 z) q! x0 I! E$ f7 q3 ndisp('第二类边界'): @, D# l# B8 s' R! [" I7 S
$ q# O% R# ] N5 Z8 f3 z) W[min(cc2),max(cc2)]
/ s5 z: P# }' }2 z/ Q# u9 ?! [- o7 S$ }3 i" }+ [% J5 N2 L O. y
disp('第三类边界')
( m4 S& j3 ~9 C( V8 t8 Y5 X
) p6 o3 ]* f8 p" T4 ~) b[min(cc3),max(cc3)] 1 \. Q n8 M. ?9 I8 a
* M6 M7 T7 v5 @* S2 p8 Z. Z' @
disp('第四类边界')
& }2 a5 \4 T" E6 z% |
5 m$ S9 q, `" a7 M: q& V: \) V6 M[min(cc4),max(cc4)]
# G" C4 w, u1 p' o# b& b8 m" _, C6 A: F2 x
disp('四类中心')
8 h5 S* e, n7 A: n! w; r3 [6 \( B' W% v! e& r% ~! S
c
7 m# L5 @+ t# s$ i) C* ?. c# j+ J* n
9 j3 T8 h9 }$ x$ I2 Vh1=plot(xx,'w');grid on;hold on; " }7 X: J; n; U9 I
% d) w4 B& p/ E2 M& [% R) B/ ofor i=1:n
9 } W2 N" F& H: d* R
1 `2 U& G. ~8 y9 G4 | if idx(i)==11 F6 p7 p+ k, n
- Z' F4 |" H, w! u5 t text(i,xx(i),num2str(idx(i)),'color',[1 0 0]);% X' }; P4 E, o
. q+ h' l! g# }. u) f) }0 W) [ elseif idx(i)==2
$ n2 P0 G; z( H. a; i3 w1 O2 V( J: N
; w% H& p! ?8 }; E5 b text(i,xx(i),num2str(idx(i)),'color',[0 1 0]);( X9 o: i5 C% B" U" s$ t4 a
! f% P1 l1 e3 T: l- H/ N O1 W
elseif idx(i)==3
! A* l- Q3 ?6 G' F, e
7 I' g! ]; Z' { text(i,xx(i),num2str(idx(i)),'color',[0 0 1]);
/ k1 q' {- W- t- ?5 w# @' m( s+ [4 Q' z2 |( t, ~* i. _
elseif idx(i)==4
) b0 X6 Y1 V+ s E5 G: L) W7 R) p7 ^& N! e) @' ]
text(i,xx(i),num2str(idx(i)),'color',[1 0 1]);
& N: ?' p+ O3 r: w9 t
O0 O/ D) n5 h+ `0 b% C! S ~ end6 L n- ^) r/ I7 j& ]& [
& z' P4 f0 N) j9 S
end
# m+ A( I# k) d' m% I$ \) H5 N
$ E0 |. V0 w+ S+ _+ N8 P* Pylabel('一维样本数据值')
/ z1 } e5 w2 h2 H' V: @6 p2 c* J# ~6 {0 U
xlabel('样本序列')) f$ ?: S; R* `+ _% C8 s
$ k) P6 F6 V) }' m! _: R9 b1 p: ]6 S' o7 o; X/ w0 {: }2 c- o0 O3 V
2.二维数据的聚类分析 参考博文:https://blog.csdn.net/zpp1994/article/details/53456306 https://blog.csdn.net/wys7541/article/details/82153844 - s7 W, H2 j: Q( q) W/ Z
————————————————$ W# j" L. [+ n$ \( L( i8 K5 D% O
版权声明:本文为CSDN博主「马马也」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。) U+ l! w1 K; N/ X+ \/ I$ a
原文链接:https://blog.csdn.net/machi1/article/details/981816218 v3 s q6 ^) U5 p
G6 ~8 ]" }6 ]: a
6 E) x6 e" t3 X8 f }4 i6 I+ X
5 V; D& B$ W! M* I
/ y1 [! L4 L4 K* Q! C) R5 @3 ^% X0 Y0 N1 s' v5 V9 U. W
* e* l6 e. c0 O9 d) a
L- J F6 j \8 {* w& K
|