重新排序numpy数组

时间:2016-03-07 01:43:05

标签: python numpy

我有以下Numpy数组:

def sentiment_analysis():
    fo = open("positive_words.txt", "r")
    positive_words = fo.readlines()
    fo.close()
    positive_words = map(lambda positive_words: positive_words.strip(), positive_words)
    fo = open("negative_words.txt", "r")
    negative_words = fo.readlines()
    fo.close()
    negative_words = map(lambda negative_words: negative_words.strip(), negative_words)
    fo = open("BAC.csv", "r")
    data = fo.readlines()
    fo.close()
    data = map(lambda data: data.strip(), data)
    x1 = 0 #number of bullish
    x2 = 0 #number of bearish
    x3 = 0 #number of unknown

    fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
    writer = csv.writer(fo)

    for info in data:
        data_specs = info.split(',')
        time_n_date = data_specs[0]
        sentiment = data_specs[2]
        '''Possibly precede with a nested for loop for data_specs???'''
        if sentiment == 'Bullish':
            '''fo.write(time + ',' + 'Bullish' + '\n')'''
        elif sentiment == 'Bearish':
            ''' fo.write(time + ',' + 'Bearish' + '\n')'''
        else:
            x3 += 1
            positive = 0
            negative = 0
            content_words = data_specs[1].split()
            for a in positive_words:
                for b in content_words:
                    if (a == b):
                        positive = positive + 1
            for c in negative_words:
                for d in content_words:
                    if (c == d):
                        negative = negative + 1
            if positive > negative:
                '''fo.write(time + ',' + 'Bullish' + '\n')'''
                sentiment = 'Bullish'
            elif positive < negative:
                sentiment = 'Bearish'
            else:
                sentiment = 'Neutral'

        bac2data = [time_n_date, sentiment]
        print bac2data
        writer.writerow(bac2data)

    fo.close()

我想重新排序数组,使其从第4个位置开始,然后是开始位置之后的项目,然后是开始位置之前的项目。即。

arr = np.array([0.3, 3.5, 12.0, 2.9, 11.0, 23.0])

如果没有for循环,我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

尝试

np.roll(arr, -3)

否定,因为你想&#34;移动&#34;左边的元素

答案 1 :(得分:2)

您要查找的命令是#include <stdio.h> char c, b; int main() { printf("Give me letter: "); scanf(" %c", &c); _ _asm( ".intel_syntax noprefix;" "xor eax, eax;" // clear eax "mov al, byte ptr [c];" // save c in eax "cmp eax, 65;" // eax ? "A" "jl Fin;" // eax < "A" -> Fin "cmp eax, 90;" // eax ? "Z" "jg UpC;" // eax >= Z -> Up Case "add eax, 32;" // make low case "jmp Fin;" // -> Fin "UpC: cmp eax, 97;" // eax ? "a" "jl Fin;" // eax < "a" -> Fin "cmp eax, 122;" // eax ? "z" "jg Fin;" // eax > "z" -> Fin "sub eax, 32;" // make Up Case "Fin: mov byte ptr [b], al;" // save res in b ".att_syntax"); printf("Case changed : %c\n", b); } 。它相当于Mathematica的numpy.roll命令。