使用某些规则打印数组的元素

时间:2019-07-18 04:42:29

标签: python numpy scipy

我的标题可能令人困惑,但我不知道该写些什么。目前,我正在尝试从一本书中了解RBF-Kernel-PCA,我在代码中加载数据集,然后使用如下代码绘制数据集:

    clc; close all; clear all;
    % waveform setting
    ga=1;           % g(t) magnibude
    gr=2;           % symbol rates,Mhz
    gt=1/gr;        % symbol interval
    gd=0.5;         %
    gn=2^7;         % g(t) symbol number
    gs=64;          % sampling points per symbol
    gp=gn*gs;        % total sampling point
    dt=gt/gs;        % symbo time interval
    st=gp*dt;       % total sampling time
    df=1/st;        % samping frequency interval
    bw=gp*df;       % system bandwidth
    t=linspace(-st/2,st/2,gp);  % time vector
    f=linspace(-bw/2,bw/2,gp);        % freq vector
    loop=1000;            % for smooth the curve
    EP=zeros(1,gp);
    for jj=1:loop
        a=round(rand(1,gn));    % generate gn symbols
        ma=mean(a,2);
        ta=std(a,0,2);          % variance
        [s t] = lineencoder('unirz',a,gt,ga,gs,0.5);
        O=fftshift(fft(s));
        Q=abs(O./gp);             % time to freq, two side spectral 
        P=Q.^2;       %P
        EP=(EP*(jj-1)+P)/jj;  %????
    end
    % f1=figure('Name','pam_spectral_nrz_unipolar_f1','Visible','off');
    % plot(t(1:10/gt*gs),s(1:10/gt*gs),'b');
    % axis([1 10 -0.5 1.5]);
    % xlabel('t (Sec)');ylabel('Volts(V)');title('RZ Unipolar Symbols');
    f2=figure('Name','pam_spectral_nrz_unipolar_f2','Visible','on');
    ps=30+10*log10(EP./df+eps);% plus eps to avoid device by zero, unit dBm so puls 30
    plot(f,ps,'r');hold on;grid on;
    xlabel('f (MHZ)');ylabel('Ps(f)');title('RZ Unipolar PSD by MATLAB FFT');
    axis([-bw/8 bw/8 -60 60]);
    f3=figure('Name','pam_spectral_nrz_unipolar_f3','Visible','on');
    gf=ga*gt/2*sinc(f*gt/2).*exp(-1i*pi.*f*gt/2);
    m=-bw/2:gr:bw/2-gr;
    m=m/2;
    gmf=zeros(1,bw/df);
    % gmf0=sinc(m/2);
    gmf(1:gr/df:end)=(ga*gt)./2*sinc(m/2);
    p=ta^2/gt*abs(gf).^2 + (ma/gt)^2.*abs(gmf).^2;
    % p=(ma/gt)^2.*abs(gmf0).^2;
    % p=(ma/gt)^2.*abs(gmf).^2;
    plot(f,10*log10(p)+30,'b');grid on;
    axis([-bw/8 bw/8 -60 60]);
    xlabel('f (MHZ)');ylabel('Ps(f)');title('RZ Unipolar PSD by Math Calculation');
    function [x T] = LineEncoder(type,inbits,Tb,A,Fs,D)
    if nargin<4, A = 1;end
    if nargin<3, Tb = 1e-9;end
    if nargin<2, inbits = [1 0 1 0];end
    if nargin<1, type = 'uninrz';end
    %---Implementation starts here
    Rb = 1/Tb; %---Bit rate
    % Fs = 4*Rb;
    N = length(inbits);   %---Bit Length of input bits
    tTb = linspace(0,Tb,Fs); %---interval of bit time period
    x = [];
    switch lower(type)
        case 'uninrz'
            for k = 1:N
                x = [x A*inbits(k)*ones(1,length(tTb))];
            end
        case 'unirz'
            for k = 1:N
                x = [x A*inbits(k)*ones(1,floor(length(tTb)*D)) 0*inbits(k)*ones(1,length(tTb)-floor(length(tTb)*D))];
            end
        case 'polrz'
            for k = 1:N
                c = ones(1,floor(length(tTb)*D));
                b = zeros(1,length(tTb)-floor(length(tTb)*D));
                p = [c b];
                x = [x ((-1)^(inbits(k)+1))*(A)*p];
            end
        case 'polnrz'
            for k = 1:N
                x = [x ((-1)^(inbits(k) + 1))*A*ones(1,length(tTb))];
            end
        case 'manchester'
            for k = 1:N
                c = ones(1,length(tTb)/2);
                b = -1*ones(1,length(tTb)/2);
                p = [c b];
                x = [x ((-1)^(inbits(k)+1))*A*p];
            end
        case 'ami'
    end
    T = linspace(0,N*Tb,length(x)); %---Time vector for n bits
    end

我不明白为什么他们使用X [y == 0,0]和X [y == 0,1]。 y是什么,为什么要用y == 0,1来执行? 0和1实际上是什么?请详细解释或分享您的知识。我仍然是一个初学者,所以我可能无法理解深入的解释。谢谢

编辑**

我知道“ y”现在是数据集中的标签。但是我不明白为什么他们使用0,1。 0代表标签0,但是1呢?

示例

from scipy.spatial.distance import pdist, squareform 
from scipy import exp 
from scipy.linalg import eigh 

from sklearn.datasets import make_moons 

import matplotlib.pyplot as plt
import numpy as np
X, y = make_moons(n_samples=100, random_state=123)
plt.scatter(X[y==0, 0], X[y==0, 1],
            color='red', marker='^', alpha=0.5) 
plt.scatter(X[y==1, 0], X[y==1, 1],
            color='blue', marker='o', alpha=0.5)
plt.show()

1 个答案:

答案 0 :(得分:1)

请注意括号,它更像X[(y==0), 1]。具体来说,这段代码选择的是y==0所在的每一行,然后1是列(第二列)。逗号分隔X数组的轴。例如,让我们拥有这些数组Xy

In [100]: X = np.array([[5, 4], [3, 2], [1, 0]])

In [101]: X
Out[101]:
array([[5, 4],
       [3, 2],
       [1, 0]])

In [102]: y = np.array([1, 0, 0])

现在y==0将为您提供一个布尔数组,其大小与y相同,但是分别使用TrueFalse,它们的值等于零:

In [103]: y == 0
Out[103]: array([False,  True,  True])

现在,您可以通过boolean indexing使用此布尔数组选择行:

In [104]: X[y == 0]
Out[104]:
array([[3, 2],
       [1, 0]])

请注意,它选择了第二行和第三行,它们是y等于零的索引。如果我只想要这些列之一,我只需添加另一个索引:

In [105]: X[y == 0, 1]
Out[105]: array([2, 0])

因此,这里对此索引操作的完整描述是“根据y为零的索引选择行,然后选择第二列。”