日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【回眸】LDA算法(数据处理与智能决策)

發(fā)布時間:2023/12/31 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【回眸】LDA算法(数据处理与智能决策) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

今天的數(shù)據(jù)處理與智能決策的作業(yè)需要用到LDA算法,接下來簡單注釋一下LDA算法的代碼。

LDA算法的代碼

import pandas as pd import numpy as np from matplotlib import pyplot as plt from sklearn.preprocessing import LabelEncoder from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDAfeature_dict = {i: label for i, label in zip(range(4),("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width",))} # print(feature_dict) # {0: 'Sepal.Length', 1: 'Sepal.Width', 2: 'Petal.Length', 3: 'Petal.Width'}df = pd.read_csv('iris.csv', sep=',') df.columns = ["Number"] + [l for i, l in sorted(feature_dict.items())] + ['Species'] # to drop the empty line at file-end df.dropna(how='all', inplace=True) # print(df.tail()) # 打印數(shù)據(jù)的后五個,和 .head() 是對應(yīng)的X = df[["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"]].values y = df['Species'].values enc = LabelEncoder() label_encoder = enc.fit(y) y = label_encoder.transform(y) + 1label_dict = {1: 'setosa', 2: 'versicolor', 3: 'virginica'}np.set_printoptions(precision=4) mean_vectors = [] for c1 in range(1, 4):mean_vectors.append(np.mean(X[y == c1], axis=0))# print('Mean Vector class %s : %s\n' % (c1, mean_vectors[c1 - 1])) S_W = np.zeros((4, 4)) for c1, mv in zip(range(1, 4), mean_vectors):# scatter matrix for every classclass_sc_mat = np.zeros((4, 4))for row in X[y == c1]:# make column vectorsrow, mv = row.reshape(4, 1), mv.reshape(4, 1)class_sc_mat += (row - mv).dot((row - mv).T)# sum class scatter metricesS_W += class_sc_mat # print('within-class Scatter Matrix:\n', S_W) overall_mean = np.mean(X, axis=0) S_B = np.zeros((4, 4)) for i, mean_vec in enumerate(mean_vectors):n = X[y == i + 1, :].shape[0]# make column vectormean_vec = mean_vec.reshape(4, 1)# make column vectoroverall_mean = overall_mean.reshape(4, 1)S_B += n * (mean_vec - overall_mean).dot((mean_vec - overall_mean).T) # print('between-class Scatter matrix:\n', S_B)eig_vals, eig_vecs = np.linalg.eig(np.linalg.inv(S_W).dot(S_B))for i in range(len(eig_vals)):eigvec_sc = eig_vecs[:, i].reshape(4, 1)# print('\n Eigenvector {}: \n {}'.format(i+1, eigvec_sc.real))# print('Eigenvalue {: }: {:.2e}'.format(i+1, eig_vals[i].real))# make a list of (eigenvalue, eigenvector) tuples eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:, i]) for i in range(len(eig_vals))]# sort the (eigenvalue, eigenvector) tuples from high to low eig_pairs = sorted(eig_pairs, key=lambda k: k[0], reverse=True)# Visually cinfirm that the list is correctly sorted by decreasing eigenvalues print('Eigenvalues in decreasing order: \n') for i in eig_pairs:print(i[0])print('Variance explained:\n') eigv_sum = sum(eig_vals) for i, j in enumerate(eig_pairs):print('eigenvalue {0:}: {1:.2%}'.format(i + 1, (j[0] / eigv_sum).real))W = np.hstack((eig_pairs[0][1].reshape(4, 1), eig_pairs[1][1].reshape(4, 1))) print('Matrix W: \n', W.real)X_lda = X.dot(W) assert X_lda.shape == (150, 2), 'The matrix is not 150*2 dimensional.'def plt_step_lda():ax = plt.subplot(111)for label, marker, color in zip(range(1, 4), ('^', 's', 'o'), ('blue', 'red', 'green')):plt.scatter(x=X_lda[:, 0].real[y == label],y=X_lda[:, 1].real[y == label],marker=marker,color=color,alpha=0.5,label=label_dict[label])plt.xlabel('LD1')plt.ylabel('LD2')leg = plt.legend(loc='upper right', fancybox=True)leg.get_frame().set_alpha(0.5)plt.title('LDA: Iris projection onto the first 2 linear discriminants')# hide axis ticksplt.tick_params(axis='both', which='both', bottom='off',top='off', labelbottom='on', left='off',labelleft='on')# remove axis spinesax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)plt.grid()plt.tight_layout()plt.show() #這里生成一個圖 # plt_step_lda()# LDA sklearn_lda = LDA(n_components=2) X_lda_sklearn = sklearn_lda.fit_transform(X, y)def plot_scikit_lda(X, title):ax = plt.subplot(111)for label, marker, color in zip(range(1, 4), ('^', 's', 'o'), ('blue', 'red', 'green')):plt.scatter(x=X_lda[:, 0].real[y == label],# flip the figurey=X_lda[:, 1].real[y == label] * -1,marker=marker,color=color,alpha=0.5,label=label_dict[label])plt.xlabel('LD1')plt.ylabel('LD2')leg = plt.legend(loc='upper right', fancybox=True)leg.get_frame().set_alpha(0.5)plt.title(title)# hide axis ticksplt.tick_params(axis='both', which='both', bottom='off',top='off', labelbottom='on', left='off',labelleft='on')# remove axis spinesax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)plt.grid()plt.tight_layout()plt.show() #這里也生成一個圖 plot_scikit_lda(X, title='Default LDA via scikit-learn') 首先導(dǎo)入包 其次打印 三個類別,四個特征 均值分別如下: Mean Vector class 1 : [5.006 3.428 1.462 0.246] Mean Vector class 2 : [5.936 2.77 4.26 1.326] Mean Vector class 3 : [6.588 2.974 5.552 2.026]

生成的圖放在下面了:



明天不見不散(^-^)V

總結(jié)

以上是生活随笔為你收集整理的【回眸】LDA算法(数据处理与智能决策)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美夜夜夜 | 嫩草视频入口 | 久久久久一区二区三区四区 | 四虎影院国产精品 | 久久久久久国产精品免费播放 | 一级片福利| 涩涩爱在线 | 91天天色| 99国产精品国产免费观看 | 色综合亚洲 | 五月色婷婷综合 | 欧美在线视频免费观看 | 国产男女猛烈无遮挡 | 神马国产 | 亚洲码无人客一区二区三区 | 第五色婷婷 | 91av麻豆 | 99re99热 | 青草超碰| 好吊日免费视频 | 偷拍视频久久 | av男人的天堂在线 | 日韩天堂av | av在线天堂| 亚洲图片在线视频 | 在线观看视频色 | 性xxxx欧美老肥妇牲乱 | 天天操天天操天天操 | 播放灌醉水嫩大学生国内精品 | 日本一区二区三区精品 | 午夜爱爱毛片xxxx视频免费看 | 国产av一区二区三区最新精品 | 亚洲一区日本 | 色综合综合色 | 精品免费av| 开心成人激情 | 色婷婷av一区二区三区大白胸 | 97人妻精品一区二区免费 | 伊人久综合| 国产久精品 | 久久免费看视频 | 国产精品a久久久久 | 北岛玲av | 成人黄色免费网站 | 香蕉视频免费在线观看 | 亚洲性一区 | 嫩草国产 | 亚洲日本香蕉 | 色人人| 免费av网站在线播放 | 女人天堂网站 | av在线有码 | 成人开心激情 | 激情五月婷婷网 | 亚洲人屁股眼子交1 | 成年人黄色片 | 日韩欧美一区二区三区久久婷婷 | 无码人妻一区二区三区免费n鬼沢 | 亚洲精品高清在线 | 国产一区二区三区四 | 51福利视频| 精品国产一二 | www超碰| 超碰碰97 | 丁香花高清在线观看完整动漫 | 久久精品99北条麻妃 | 亚洲人吸女人奶水 | 天堂在线免费视频 | 国产一区二区三区亚洲 | 国产在线日韩 | 日韩欧美一区二区区 | 在线观看日本一区 | 天天看天天爽 | 欧美视频区 | 久久精品免费电影 | 雪花飘电影在线观看免费高清 | 岛国大片在线 | 天天爱av | 丁香婷婷综合激情 | 四虎一级片 | 又黄又爽网站 | 免费日皮视频 | 一区二区三区四区欧美 | www.99色 | 久久久久人妻一区二区三区 | 欧美草逼网 | 国产一区二区网址 | 国产美女av在线 | 草草在线影院 | 亚洲日本久久 | av免费久久| 欧美大胆a| 男人操女人免费网站 | 日日操天天操夜夜操 | 性感美女一区二区三区 | 插插插网站 | 99热成人| avtt在线| 波多野结衣av无码 |