在numpy_array Python中添加列 [英] Add a column in a numpy_array Python

查看:2341
本文介绍了在numpy_array Python中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是与Python一个numpy的数组,我想知道我可以在我的数组的末尾添加一个新的

我有N行数组,我计算每一行被命名为一个新的值 X 。我想,对于每一行,在新列中添加这个新的价值。

我的脚本是(最有趣的部分是在我的剧本的结尾):

 #!的/ usr / bin中/蟒蛇
#编码:UTF-8从astropy.io进口拟合
导入numpy的是NP
#进口matplotlib.pyplot如PLT
进口数学
        #########################################
        #Fichier contenant拉清单当然香榭#
        #########################################
开放(liste_essai.txt,R)为f:    fichier_entier = f.read()
    文件= fichier_entier.split(\\ n)在文件fichier:    开放(fichier,'R'):        读= fits.open(fichier)#序曲杜fichier A L'助手德astropy        TBDATA =读[1]。数据#讲座德最近搜索适合
        ################################################## #####
        #应用杜三带fonction德潜水员参数应用#
        ################################################## #####        #掩码1 = TBDATA ['CHI'< 1.0#创建科特迪瓦联合国面膜倒拉状态CHI
        #tbdata_temp1 = TBDATA [掩码1]        #PRINT三effectué河畔CHI        #MASK2 = tbdata_temp1 ['PROB']> 0.01#创建科特迪瓦联合国第二面膜拉河畔条件PROB
        #tbdata_temp2 = tbdata_temp1 [MASK2]        #PRINT三effectué河畔PROB        #MASK3 = tbdata_temp2 ['犀利']> -0.4#创建科特迪瓦联合国3E面膜拉河畔调节SHARP(1/2)
        #tbdata_temp3 = tbdata_temp2 [MASK3]        #mask4 = tbdata_temp3 ['犀利'< 0.1#创建科特迪瓦联合国4E面膜拉河畔调节SHARP(2/2)
        #tbdata_final = tbdata_temp3 [mask4]        #PRINT创造德拉新式表压轴
        #PRINT tbdata_final#Affichage德拉表滑雪后所有领域LES条件        #fig = plt.figure()
        #plt.plot(tbdata_final ['G' - '。'tbdata_final ['R'],tbdata_final ['G'])
        #plt.title('Diagramme传送彩色-幅度)
        #plt.xlabel('(G-R))
        #plt.ylabel('G')
        #plt.xlim(-2,2)
        #plt.ylim(15,26)
        #plt.gca()。invert_yaxis()
        #plt.show()
        #fig.savefig()        #PRINT创造杜Diagramme        #hdu = fits.BinTableHDU(数据= tbdata_final)
        #hdu.writeto('{} _ {}。格式(fichier,'traité(主旨)'))#杜书写理论résultatobtenu丹斯未暴发户fichier适合        #PRINT杜书写理论暴发户fichiertraité(主旨)        #################################################
        #决心德valeurs extremales杜冠军#
        #################################################        RA_max = np.max(TBDATA ['RA'])
        RA_min = np.min(TBDATA ['RA'])
        #PRINTRA_max vaut:+ STR(RA_max)
        #PRINTRA_min vaut:+ STR(RA_min)        DEC_max = np.max(TBDATA ['DEC'])
        DEC_min = np.min(TBDATA ['DEC'])
        #PRINTDEC_max vaut:+ STR(DEC_max)
        #PRINTDEC_min vaut:+ STR(DEC_min)        #########################################
        #演算德拉valeur中央杜冠军#
        #########################################        RA_central =(RA_max + RA_min)/ 2。
        DEC_central =(DEC_max + DEC_min)/ 2。        #PRINTRA_central vaut:+ STR(RA_central)
        #PRINTDEC_central vaut:+ STR(DEC_central)        打印
        打印#########################################    ##############################
    #决心去点¯x和de Y#
    ##############################        I = 0
        N = LEN(TBDATA)        对于在范围I(0,N):            打印Valeur德RA点菜LIGNE+ STR(I)+斯:+ STR(TBDATA ['RA'] ​​[I])
            打印Valeur德RA_moyen斯:+ STR(RA_central)
            打印Valeur德DEC_moyen斯:+ STR(DEC_central)            X =(TBDATA ['RA'] ​​[I] - RA_central)* math.cos(DEC_central)            Add_column = np.vstack(TBDATA,X)#==> ????            打印香格里拉valeur去点¯x斯:+ STR(X)
            打印

我试过的东西,但我不知道这是工作。

和我有一个第二个问题,如果有可能。在情节的一部分,我想救我的阴谋为每个文件,但每个文件的名称。我想,我需要写类似:

  plt.savefig('图',{} _ {}。格式(fichier,PNG))


解决方案

numpy的数组总是会被保存在连续的内存块,这意味着一旦你创造了它,使得它更大的任何将意味着numpy的将有到原来的阵列复制,以确保在加入将在存储器中的原始阵列旁边。结果
如果你有,你会多少列增加一个总的想法,你可以创建原始阵列零附加列。这将保留内存空间为您的阵列,然后你可以通过覆盖零的最左列的添加栏目。结果
如果你的话,你可以随时高估你需要的列数再后来就删除零的额外列的内存。据我所知,这是为了避免复制添加新列到numpy的阵列时的唯一途径。

例如:

  my_array = np.random.rand(200,3)#原数组
零= np.zeros((200,400))#预计额外的400列my_array = np.hstack((my_array,零))#加入my_array用零的数组(仅这一步将使复印件)= CURRENT_COLUMN 3#跟踪零的最左列的new_columns = []新列的#放列表在这里补充在new_columns西:
    my_array [:,CURRENT_COLUMN] =山坳
    + CURRENT_COLUMN = 1

I'm using a numpy array with Python and I would like to know how I can add a new column at the end of my array?

I have an array with N rows and I calculate for each row a new value which is named X. I would like, for each row, to add this new value in a new column.

My script is (the interesting part is at the end of my script) :

#!/usr/bin/python
# coding: utf-8

from astropy.io import fits
import numpy as np
#import matplotlib.pyplot as plt
import math


        #########################################
        # Fichier contenant la liste des champs #
        #########################################


with open("liste_essai.txt", "r") as f :

    fichier_entier = f.read()
    files = fichier_entier.split("\n")

for fichier in files :

    with open(fichier, 'r') :

        reading = fits.open(fichier)          # Ouverture du fichier à l'aide d'astropy

        tbdata = reading[1].data               # Lecture des données fits


        #######################################################
        # Application du tri en fonction de divers paramètres #
        #######################################################

        #mask1 = tbdata['CHI'] < 1.0        # Création d'un masque pour la condition CHI
        #tbdata_temp1 = tbdata[mask1]

        #print "Tri effectué sur CHI"

        #mask2 = tbdata_temp1['PROB'] > 0.01    # Création d'un second masque sur la condition PROB
        #tbdata_temp2 = tbdata_temp1[mask2]

        #print "Tri effectué sur PROB"

        #mask3 = tbdata_temp2['SHARP'] > -0.4   # Création d'un 3e masque sur la condition SHARP (1/2)
        #tbdata_temp3 = tbdata_temp2[mask3]

        #mask4 = tbdata_temp3['SHARP'] < 0.1    # Création d'un 4e masque sur la condition SHARP (2/2)
        #tbdata_final = tbdata_temp3[mask4]

        #print "Création de la nouvelle table finale"
        #print tbdata_final         # Affichage de la table après toutes les conditions

        #fig = plt.figure()
        #plt.plot(tbdata_final['G'] - tbdata_final['R'], tbdata_final['G'], '.')
        #plt.title('Diagramme Couleur-Magnitude')
        #plt.xlabel('(g-r)')
        #plt.ylabel('g')
        #plt.xlim(-2,2)
        #plt.ylim(15,26)
        #plt.gca().invert_yaxis()
        #plt.show()
        #fig.savefig()

        #print "Création du Diagramme"

        #hdu = fits.BinTableHDU(data=tbdata_final)
        #hdu.writeto('{}_{}'.format(fichier,'traité'))      # Ecriture du résultat obtenu dans un nouveau fichier fits

        #print "Ecriture du nouveau fichier traité"

        #################################################
        # Détermination des valeurs extremales du champ #
        #################################################

        RA_max = np.max(tbdata['RA'])
        RA_min = np.min(tbdata['RA'])
        #print "RA_max vaut :     " + str(RA_max)
        #print "RA_min vaut :     " + str(RA_min)

        DEC_max = np.max(tbdata['DEC'])
        DEC_min = np.min(tbdata['DEC'])
        #print "DEC_max vaut :   " + str(DEC_max)
        #print "DEC_min vaut :   " + str(DEC_min)

        #########################################
        # Calcul de la valeur centrale du champ #
        #########################################

        RA_central = (RA_max + RA_min)/2.
        DEC_central = (DEC_max + DEC_min)/2.

        #print "RA_central vaut : " + str(RA_central)
        #print "DEC_central vaut : " + str(DEC_central)

        print " "
        print " ######################################### "

    ##############################
    # Détermination de X et de Y #
    ##############################

        i = 0
        N = len(tbdata)

        for i in range(0,N) :

            print "Valeur de RA à la ligne " + str(i) + " est : " + str(tbdata['RA'][i])
            print "Valeur de RA_moyen est : " + str(RA_central)
            print "Valeur de DEC_moyen est : " + str(DEC_central)

            X = (tbdata['RA'][i] - RA_central)*math.cos(DEC_central)

            Add_column = np.vstack(tbdata, X) # ==> ????

            print "La valeur de X est : " + str(X)
            print " "

I tried something but I'm not sure that's working.

And I've a second question if it's possible. In the plot part, I would like to save my plot for each file but with the name of each file. I think that I need to write something like :

plt.savefig('graph',"{}_{}".format(fichier,png))

解决方案

Numpy arrays are always going to be stored in a continuous memory block, that means that once you've created it, making it any bigger will mean numpy will have to copy the original array to make sure that the addition will be beside the original array in memory.
If you have a general idea of how many columns you will be adding, you can create the original array with additional columns of zeros. This will reserve the space in memory for your array and then you can "add" columns by overwriting the left-most column of zeros.
If you have the memory to spare you can always over-estimate the number of columns you will need and then remove extra columns of zeros later on. As far as I know this is the only way to avoid copying when adding new columns to a numpy array.

For example:

my_array = np.random.rand(200,3)  # the original array
zeros = np.zeros((200,400))   # anticipates 400 additional columns

my_array = np.hstack((my_array,zeros)) # join my_array with the array of zeros (only this step will make a copy)

current_column = 3  # keeps track of left most column of zeros

new_columns = []  # put list of new columns to add here 

for col in new_columns:
    my_array[:,current_column] = col
    current_column += 1 

这篇关于在numpy_array Python中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆