我在循环的每次迭代中计算索引数组,然后我想删除重复的元素并将计算的数组连接到前一个.例如,第一次迭代给了我这个数组:
array([ 1, 6, 56, 120, 162, 170, 176, 179, 197, 204])
Run Code Online (Sandbox Code Playgroud)
第二个:
array([ 29, 31, 56, 104, 162, 170, 176, 179, 197, 204])
Run Code Online (Sandbox Code Playgroud)
等等.我怎么能这样做?
我写了一个类来绘制一些数据点.我曾经seaborn制作kernel density情节,它导致了(1)框架消失了,我想要一个刚性框架和(2)图中有网格,(3)背景颜色,我想摆脱它们.应该怎么做?另外,我如何获得散点图的星形和多边形标记?
import seaborn
import pandas
import pylab as P
import numpy as np
class PlotLocus(object):
def __init__(self, colorX, colorY, colorpX, colorpY ,excluded_points,lcolorx1,lcolorx2,lcolory1,lcolory2,correspondence_Matrix):
self.exarr=excluded_points #scatter points excluded by kde
self.colorx=colorX
self.colory=colorY
self.colorpx=colorpX
self.colorpy=colorpY
r=np.arange(self.colorx.shape[0])
self.arr=np.setxor1d(r,self.exarr)
self.lx1=lcolorx1
self.lx2=lcolorx2
self.ly1=lcolory1
self.ly2=lcolory2
correspondence_indicies = np.where(M > 0.99)
self.colorx_corr=self.colorx[correspondence_indicies[0]]
self.colory_corr=self.colory[correspondence_indicies[0]]
self.colorpx_corr=self.colorpx[correspondence_indicies[1]]
self.colorpy_corr=self.colorpy[correspondence_indicies[1]]
def plot_before_colors(self):
fig=P.figure(1, figsize=(8,8), dpi=100)
ax = fig.add_subplot(111)
X=np.vstack((self.colorx, self.colory)).T
data = pandas.DataFrame(X, columns=["X", "Y"])
seaborn.kdeplot(data.X,data.Y,bw='scott',shade=False, cmap="Purples")
ax.tick_params(axis='both', which='major', direction='in', length=6, width=2)
ax.scatter(self.colorx[self.exarr], self.colory[self.exarr], s=30, c='g', marker='o', edgecolors='k',facecolors='none') …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个class,我希望如果该类的初始输入值不遵循特定类型,则会引发异常.例如,我会except TypeError用来返回错误.我不知道应该怎么做.我第一次写这篇文章的尝试class如下:
class calibration(object):
def __init__(self, inputs, outputs, calibration_info, interpolations=2):
try:
self.inputs=inputs
except TypeError
self.outputs=outputs
self.cal_info=calibration_info
self.interpol=interpolations
Run Code Online (Sandbox Code Playgroud)
我想如果inputsvalue不是字符串,那么它会引发错误消息.我将不胜感激任何帮助.
我有一个ascii文件,我想把它读成一个numpy array.但它失败了,对于文件中的第一个数字,它在我使用时返回'NaN' numpy.genfromtxt.然后我尝试使用以下方式将文件读入数组:
lines = file('myfile.asc').readlines()
X = []
for line in lines:
s = str.split(line)
X.append([float(s[i]) for i in range(len(s))])
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
ValueError: could not convert string to float: 15.514
Run Code Online (Sandbox Code Playgroud)
当我打印文件的第一行时,它看起来像:
>>> s
['\xef\xbb\xbf15.514', '15.433', '15.224', '14.998', '14.792', '15.564', '15.386', '15.293', '15.305', '15.132', '15.073', '15.005', '14.929', '14.823', '14.766', '14.768', '14.789']
Run Code Online (Sandbox Code Playgroud)
我怎么能把这样一个文件读成numpy array没有问题和关于行数和列数的假设?
我正在cython中实例化一个类。我想声明一个实例,它是一个值数组,通过使用给定函数计算它一次,并使用cfloat函数将其保存在二进制文件中。对于我的类的进一步调用,如果输入文件已经存在,则将通过从文件中读取数组来声明该实例,否则它将再次计算它。
import numpy as np
cimport numpy as np
cimport cython
from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fseek, SEEK_END, ftell, stdout, stderr
cdef extern from "math.h":
double exp(double) nogil
double log(double) nogil
cdef class Hit(object):
cdef public double[::1] zs, Da
cdef char* path
def __cinit__(self, zs=None, path=None):
if path is None:
raise ValueError("Could not find a path to the file which contains the table of distances")
else:
self.path=path
if zs is None:
raise ValueError("You …Run Code Online (Sandbox Code Playgroud) 我有一个大小为1505MB的文本文件,其中包含浮点数据。该文件大约有 73000 行和 1500 列。我想将文件的内容读入一个numpy数组,然后对数组执行一些分析,但我的机器numpy.readtxt在读取文件时变得越来越慢。使用python将此文件读入数组的最快方法是什么?
我有一个带有标题的ascii文件中的数据表,我想将我的ascii表转换为带头的fit文件
#ID ra dec x y Umag Bmag Vmag Rmag Imag
1.0 53.146 -27.8123 3422.98 3823.58 24.4528 24.7995 23.6266 22.64 20.8437
2.0 53.1064 -27.801 3953.49 3994.62 23.3284 22.6716 22.1762 21.6189 21.2141
3.0 53.1322 -27.7829 3608.34 4269.29 21.2676 20.1937 19.6743 19.0707 18.6983
4.0 53.1017 -27.8022 4017.09 3975.24 23.6987 22.84 21.9946 21.0781 19.8616
5.0 53.118 -27.8021 3798.98 3978.42 23.3087 22.1932 21.2205 20.1842 18.6448
6.0 53.1479 -27.8239 3397.92 3648.27 25.0347 24.598 23.7259 22.9945 21.9228
7.0 53.1334 -27.7758 3592.51 4375.76 21.5159 20.4777 19.6065 18.6609 …Run Code Online (Sandbox Code Playgroud) 一个类如何从另一个类继承?我正在尝试通过以下示例来实现它:
class parents(object):
def __init__(self,dim_x, dim_y, nameprefix, sequence_number):
if not isinstance(dim_x, (int, float, long)):
raise TypeError("Dimension in x must be a number")
else:
self.sizex=str(int(dim_x))
if not isinstance(dim_y, (int, float, long)):
raise TypeError("Dimension in y must be a number")
else:
self.sizey=str(int(dim_y))
if not isinstance(nameprefix, string_types):
raise TypeError("The name prefix must be a string")
else:
self.prefix=nameprefix
if not isinstance(sequence_number, (int, float, long)):
raise TypeError("The sequence number must be given as a number")
else:
self.sqNr=str(int(sequence_number))
Run Code Online (Sandbox Code Playgroud)
我想这个类child继承prefix和sqNr从parents …
我想boolean在cython中使用另一个numpy.array的给定大小制作一个numpy数组,但它会引发一条错误消息:
CosmoTest.pyx
import numpy as np
cimport numpy as np
cimport cython
from libcpp cimport bool
x=np.array([[-0.3,1.2],[2.5,0.82],[0.61,-0.7]])
mask= np.ones_like(x,dtype=bool)
Run Code Online (Sandbox Code Playgroud)
错误:
mask= np.ones_like(x,dtype=bool)
^
------------------------------------------------------------
CosmoTest.pyx:318:39: 'bool' is not a constant, variable or function identifier
Run Code Online (Sandbox Code Playgroud)
如何在cython中定义?
更新:
cpdef np.ndarray arc( np.ndarray x):
cdef np.ndarray[double, ndim=1, mode='c'] out = np.zeros_like(x)
cdef np.ndarray[np.uint8_t,cast=True, ndim=1] mask = (x < 0.999).view(dtype=np.uint8)
if mask.any():
out[mask] = 0.5*np.log((1.+((1.-x[mask])/(x[mask]+1.))**0.5)/(1.-((1.-x[mask])/(x[mask]+1.))**0.5))/(1-x[mask]**2)**0.5
cdef np.ndarray[np.uint8_t,cast=True, ndim=1] mask = (x > 1.001).view(dtype=np.uint8)
if mask.any():
out[mask] = np.arctan(((x[mask]-1.)/(x[mask]+1.))**0.5)/(x[mask]**2 …Run Code Online (Sandbox Code Playgroud) 我有一组庞大的目录,每列有不同的列和不同的标题名称,每个标题名称的描述在我的ascii文件的开头连续给出.阅读它们的最佳方法是什么pandas.DataFrame,它可以设置列的名称,而无需从头开始定义它.以下是我的目录示例:
# 1 MAG_AUTO Kron-like elliptical aperture magnitude [mag]
# 2 rh half light radius (analyse) [pixel]
# 3 MU_MAX Peak surface brightness above background [mag * arcsec**(-2)]
# 4 FWHM_IMAGE FWHM assuming a gaussian core [pixel]
# 5 CLASS_STAR S/G classifier output
18.7462 4.81509 20.1348 6.67273 0.0286538
18.2440 7.17988 20.6454 21.6235 0.0286293
18.3102 3.11273 19.0960 8.26081 0.0430532
21.1751 2.92533 21.9931 5.52080 0.0290418
19.3998 1.86182 19.3166 3.42346 0.986598
20.0801 3.52828 21.3484 6.76799 0.0303842
21.9427 2.08458 22.0577 5.59344 0.981466 …Run Code Online (Sandbox Code Playgroud) python ×10
numpy ×7
arrays ×5
class ×2
cython ×2
astropy ×1
c ×1
duplicates ×1
file-io ×1
inheritance ×1
matplotlib ×1
pandas ×1
pyfits ×1
scipy ×1
seaborn ×1
subclass ×1
text-files ×1