np.newaxis和np.tile

np.newaxis

import numpy as np
a = np.array([0.0, 10.0, 20.0, 30.0])
b = np.array([1.0, 2.0, 3.0])
a[:, np.newaxis] + b
# array([[  1.,   2.,   3.],
#        [ 11.,  12.,  13.],
#        [ 21.,  22.,  23.],
#        [ 31.,  32.,  33.]])
b = np.array([1, 2, 3])
print(b) # [1 2 3]
print(b[np.newaxis])  # [[1 2 3]]

c = b[np.newaxis]  # equals c = b[np.newaxis,:]

print(b.shape) # (3,)
print(c.shape) # (1, 3)

# 看一下转置的时候有什么区别
print(np.transpose(b)) # [1 2 3]
print(np.transpose(c))
# [[1]
#  [2]
#  [3]]

np.title

import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))  # Stack 4 copies of v on top of each other 拓展
print(vv)
# [[1 0 1]
#  [1 0 1]
#  [1 0 1]
#  [1 0 1]]

y = x + vv
print(y)
# [[ 2  2  4]
#  [ 5  5  7]
#  [ 8  8 10]
#  [11 11 13]]



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import pandas as pd data = pd.read_excel('C:\Users\home\Desktop\新建文件夹(1)\支撑材料\数据\111.xlsx','Sheet5',index_col=0) data.to_csv('data.csv',encoding='utf-8') import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt df = pd.read_csv(r"data.csv", encoding='utf-8', index_col=0).reset_index(drop=True) df from sklearn import preprocessing df = preprocessing.scale(df) df covX = np.around(np.corrcoef(df.T),decimals=3) covX featValue, featVec= np.linalg.eig(covX.T) featValue, featVec def meanX(dataX): return np.mean(dataX,axis=0) average = meanX(df) average m, n = np.shape(df) m,n data_adjust = [] avgs = np.tile(average, (m, 1)) avgs data_adjust = df - avgs data_adjust covX = np.cov(data_adjust.T) covX featValue, featVec= np.linalg.eig(covX) featValue, featVec tot = sum(featValue) var_exp = [(i / tot) for i in sorted(featValue, reverse=True)] cum_var_exp = np.cumsum(var_exp) plt.bar(range(1, 14), var_exp, alpha=0.5, align='center', label='individual explained variance') plt.step(range(1, 14), cum_var_exp, where='mid', label='cumulative explained variance') plt.ylabel('Explained variance ratio') plt.xlabel('Principal components') plt.legend(loc='best') plt.show() eigen_pairs = [(np.abs(featValue[i]), featVec[:, i]) for i in range(len(featValue))] eigen_pairs.sort(reverse=True) w = np.hstack((eigen_pairs[0][1][:, np.newaxis], eigen_pairs[1][1][:, np.newaxis])) X_train_pca = data_adjust.dot(w) colors = ['r', 'b', 'g'] markers = ['s', 'x', 'o'] for l, c, m in zip(np.unique(data_adjust), colors, markers): plt.scatter(data_adjust,data_adjust, c=c, label=l, marker=m) plt.xlabel('PC 1') plt.ylabel('PC 2') plt.legend(loc='lower left') plt.show()
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值