ones()函数用以创建指定形状和类型的数组,默认情况下返回的类型是float64。但是,如果使用ones()函数时指定了数据类型,那么返回的就是该类型。
参考NumPy v111官方手册中对ones()函数的描述:
numpyones(shape, dtype=None, order='C')
其中:
shape : 数组的形状,例如 (2, 3) 或 2
dtype : 数组的数据类型,默认为numpyfloat64
order : 数组元素在内存中的排列方式,默认 'C’表示C语言排列方式,或者‘F’表示 Fortran 语言排列方式
具体举例如下:
如果不指定数据类型,则默认返回float64
In [1]: import numpy as npIn [2]: a=npones(3)
In [3]: print adtype
float64
如果指定了数据类型,那么返回指定的类型
In [1]: import numpy as npIn [2]: a = npones(3,npint) # 此处指定ones()创建的数据类型为int32
In [3]: print adtype
int32
可以使用强制类型转换、自动类型转换两种方式。
强制类型转换是通过类型转换运算来实现的。自动转换是在源类型和目标类型兼容以及目标类型广于源类型时发生一个类型到另一类的转换。
C语言常用数据类型,int:整数类型,float:单精度浮点类型,double:双精度浮点类型,char:字符类型,char:字符指针类型。
numpyargsort( a , axis=-1 , kind='quicksort' , order=None )
Returns the indices that would sort an array
Perform an indirect sort along the given axis using the algorithm specified by the kind keyword It returns an array of indices of the same shape as a that index data along the given axis in sorted order
Parameters:
a : array_like
Array to sort
axis : int or None, optional
Axis along which to sort The default is -1 (the last axis) If None, the flattened array is used
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
Sorting algorithm
order : str or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties
Returns:
index_array : ndarray, int
Array of indices that sort a along the specified axis If a is one-dimensional, a[index_array] yields a sorted a
例1:
# Python36
import numpy as np
while True:
x = input("输入一个m维数组,元素以空格分开:")
try:
# split input
x = xsplit()
s = []
# iterate:str-->int
for i in x:
sappend(int(i))
x = s
break
except:
print("输入有错,请重新输入。")
while True:
n = input("整数n:")
try:
n = int(n)
break
except:
print("输入有错,请重新输入。")
# map object -->list object
lst = [i for i in x]
# list object -->numpyarray object
mtrx = nparray(lst)
# copy the 1st line of matrix as a template
mtrx_1 = npcopy(mtrx)
k = len(mtrx)
# reshape (k,) to (1,k) for concatenate
mtrx = npreshape(mtrx, (1, k))
# calculate left lines
for i in range(1, n):
tmp = nppower(mtrx_1, i+1)
tmp = npreshape(tmp, (1, k))
mtrx = npconcatenate((mtrx, tmp), axis=0)
print(mtrx)
1 你可能会喜欢SciPy的统计软件包。它有百分函数你之后,许多其他统计好吃的东西。
此票证相信他们不会被整合percentile()到numpy的很快。
2
顺便说一句,有百分函数的纯Python,万一一个不希望依赖于SciPy的。具体函数如下复制:
## {{{ CodeGonet (r1)
import math
import functools
def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values
@parameter N - is a list of values Note N MUST BE already sorted
@parameter percent - a float value from 00 to 10
@parameter key - optional key function to compute value from each element of N
@return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) percent
f = mathfloor(k)
c = mathceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) (c-k)
d1 = key(N[int(c)]) (k-f)
return d0+d1
# median is 50th percentile
median = functoolspartial(percentile, percent=05)
## end of CodeGonet }}}
3
检查scipystats模块:
scipystatsscoreatpercentile
4
import numpy as np
a = [154, 400, 1124, 82, 94, 108]
print nppercentile(a,95) # gives the 95th percentile
5
百分看到定义预期结果从提供的列表,低于该值的百分之P被发现的价值。为了得到这一点,你一个简单的函数。
def percentile(N, P):
"""
Find the percentile of a list of values
@parameter N - A list of values N must be sorted
@parameter P - A float value from 00 to 10
@return - The percentile of the values
"""
n = int(round(P len(N) + 05))
return N[n-1]
# A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# B = (15, 20, 35, 40, 50)
#
# print percentile(A, P=03)
# 4
# print percentile(A, P=08)
# 9
# print percentile(B, P=03)
# 20
# print percentile(B, P=08)
# 50
如果您宁愿从处于或低于该值的百分之P被发现所提供的列表中获得的价值,这个简单的修改:
def percentile(N, P):
n = int(round(P len(N) + 05))
if n > 1:
return N[n-2]
else:
return 0
6
numpypercentile
在那里我很想念?
7
size=len(mylist)
p5=mylist[mathceil((size5)/100)-1]
p25=mylist[mathceil((size25)/100)-1]
p50=mylist[mathceil((size50)/100)-1]
p75=mylist[mathceil((size75)/100)-1]
p95=mylist[mathceil((size95)/100)-1]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)