1 import pandas as pd 2 from sklearn.decomposition import PCA 3 4 # 以detail 为例实现数据降维 5 # 加载数据 6 7 detail = pd.read_excel("../day05/meal_order_detail.xlsx") 8 print("detail :\n", detail) 9 print("detail 的列索引名称:\n", detail.columns) 10 11 # 将detail_id order_id dishes_id counts amounts合成一列数据 12 # PCA降维 13 # (1)实例化对象 14 # n_components ---两种传递方式 15 # 整数 ----将数据合成成几列 16 # 小数-----保留原来数据的属性占比 17 # pca = PCA(n_components=1) 18 # 映射关系--数学函数 19 # (x,y) (x,y,z) (a,b,c,d,e) ---(x,y) 20 pca = PCA(n_components=0.98) 21 # (2)数据降维 22 res = pca.fit_transform(detail.loc[:, ["detail_id", "order_id", "dishes_id", "counts", "amounts"]]) 23 24 print("res:\n", res) 25 print("res 的形状:\n", res.shape)