【python-将int或float列转换为百分比分布】教程文章相关的互联网学习教程文章

python-从24位wav pcm格式转换为float的更快方法?【代码】

我需要从24位pcm格式的wav文件中读取数据,然后转换为float.我正在使用Python 2.7.2. wave包以字符串形式读取数据,因此我尝试了以下操作:import wave import numpy as np import array import structf = wave.open('filename.wav') # read in entire wav file wdata = f.readframes(nFrames) f.close()# unpack into signed integers and convert to float data = array.array('f') for i in range(0,nFrames*3,3):data.app...

python-将int或float列转换为百分比分布【代码】

我有一个熊猫数据框df:import pandas as pd import numpy as np data = {'A':[250,100,400,np.nan,300]} df = pd.DataFrame(data) print(df)A 0 250.0 1 100.0 2 400.0 3 NaN 4 300.0我想基于列表(值)中的值来转换此数据仓库(DF).values = [0,200,400,600]在df中,第一个数字250.列表值在200和400之间,使得(| 200-250 |)/(400-200)= 0.25和(400-250)/(400-200)= 0.75 .如果缺少数据(np.nan),则必须用0填充行.我要以这种方式...

令人困惑的python-无法将字符串转换为float【代码】

我遇到了一个值错误,即使我尝试使用代码,也无法正常工作! 我该如何正确处理? -我正在使用Python 3.3.2! 这是代码: 如您所见,该程序会询问您可以走多少英里,并根据您键入的内容给出响应. 这是文本格式的代码:print("Welcome to Healthometer, powered by Python...") miles = input("How many miles can you walk?: ") if float(miles) <= 0:print("Who do you think you are?!! Go and walk 1000 miles now!") elif float(mil...

python – TypeError:需要一个float【代码】

无法发布图像,因此:a [i] = {( – 1)^(i 1)* sin(x)* ln(x)} / {i ^ 2 *(i 1)!} 任务:需要找到a1,a2,…,an.n是自然而且是给定的. 这就是我尝试这样做的方式:import math a=[] k=0 p=0 def factorial(n):f=1for i in range(1,n+1):f=f*ireturn fdef narys(n):x=input('input x: ') #x isn't given by task rules, so i think that is error else.float(x)k=(math.pow(-1,n+1)*math.sin(x)*math.log10(n*x))/(n*n*factorial(n+1...

python – 将numpy int和float数组相乘:无法从dtype转换ufunc乘法输出【代码】

我想将一个int16数组乘以浮点数组,并使用自动舍入,但这会失败:import numpyA = numpy.array([1, 2, 3, 4], dtype=numpy.int16) B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)A *= B我明白了:TypeError: Cannot cast ufunc multiply output from dtype(‘float64’) to dtype(‘int16’) with casting rule ‘same_kind’解决方法:解决这个问题的两种方法: 您可以通过替换来解决此问题A *= B同A = (A * B)或者numpy.m...

如何在python中将int转换为float?【代码】

有谁知道如何将int转换为float. 由于某种原因,它继续打印0.我希望它打印一个特定的小数.sum = 144 women_onboard = 314 proportion_womenclass3_survived = sum / np.size(women_onboard) print 'Proportion of women in class3 who survived is %s' % proportion_womenclass3_survived解决方法:除了John的答案之外,你还可以创建一个变量float,结果将产生float.>>> 144 / 314.0 0.4585987261146497

python – LabelEncoder:在’float’和’str’的实例之间不支持TypeError:’>’【代码】

即使处理缺失值,我也面临多个变量的错误.例如:le = preprocessing.LabelEncoder() categorical = list(df.select_dtypes(include=['object']).columns.values) for cat in categorical:print(cat)df[cat].fillna('UNK', inplace=True)df[cat] = le.fit_transform(df[cat]) # print(le.classes_) # print(le.transform(le.classes_))--------------------------------------------------------------------------- TypeErr...

python – TypeError:无法将表达式转换为float【代码】

我是一个Python新手.我正在尝试使用python评估普朗克方程.我写了一个简单的程序.但是,当我给它输入时,给我一个错误.任何人都可以告诉我哪里出错了?这是程序和错误: 程序:from __future__ import division from sympy.physics.units import * from math import * import numpy from scipy.interpolate import interp1d#Planck's Law evaluation at a single wavelength and temperature def planck_law(wavelength,temperature):...

python – float.as_integer_ratio()的实现限制【代码】

最近,一位记者提到了XP000中的新功能,并指出典型的浮点实现基本上是实数的有理近似.好奇,我不得不尝试π:>>> float.as_integer_ratio(math.pi); (884279719003555L, 281474976710656L)由于Arima,我没有看到更多的accurate结果,我感到有点意外:(428224593349304L, 136308121570117L)例如,这段代码:#! /usr/bin/env python from decimal import * getcontext().prec = 36 print "python: ",Decimal(884279719003555) / Decimal(28...

python – pandas shift将我的列从integer转换为float.【代码】

shift将我的列从整数转换为float.事实证明,np.nan只是浮动的.有没有办法将移位列保持为整数?df = pd.DataFrame({"a":range(5)}) df['b'] = df['a'].shift(1)df['a'] # 0 0 # 1 1 # 2 2 # 3 3 # 4 4 # Name: a, dtype: int64df['b']# 0 NaN # 1 0 # 2 1 # 3 2 # 4 3 # Name: b, dtype: float64解决方法:问题是你得到NaN值是什么是浮点数,所以int转换为浮点数 – 见na type promotions. 一种可能的...

python – 在Cython与NumPy中汇总int和float时的大性能差异【代码】

我使用Cython或NumPy对一维数组中的每个元素求和.当求和整数时,Cython的速度提高了约20%.总结浮点数时,Cython慢??约2.5倍.以下是使用的两个简单函数.#cython: boundscheck=False #cython: wraparound=Falsedef sum_int(ndarray[np.int64_t] a):cdef:Py_ssize_t i, n = len(a)np.int64_t total = 0for i in range(n):total += a[i]return total def sum_float(ndarray[np.float64_t] a):cdef:Py_ssize_t i, n = len(a)np.float64_t...

python – TypeError:’float’对象不可订阅【代码】

PizzaChange=float(input("What would you like the new price for all standard pizzas to be? ")) PriceList[0][1][2][3][4][5][6]=[PizzaChange] PriceList[7][8][9][10][11]=[PizzaChange+3]基本上我有一个输入,用户将输入一个数值(浮点输入),然后它将所有这些上述列表索引设置为该值.出于某种原因,我无法在不提出问题的情况下设置它们:TypeError: 'float' object is not subscriptable错误.我做错了什么,或者我只是以...

python – TypeError:’float’对象不可调用【代码】

我试图在以下等式中使用数组中的值:for x in range(len(prof)):PB = 2.25 * (1 - math.pow(math.e, (-3.7(prof[x])/2.25))) * (math.e, (0/2.25)))当我运行时收到以下错误:Traceback (most recent call last):File "C:/Users/cwpapine/Desktop/1mPro_Chlavg", line 240, in <module>PB = float(2.25 * (1 - math.pow(math.e, (-3.7(prof[x])/2.25))) * (math.e, (0/2.25))) TypeError: 'float' object is not callable这可能很简...

python – pandas比较引发TypeError:无法将dtyped [float64]数组与[bool]类型的标量进行比较【代码】

我的dataFrame有以下结构:Index: 1008 entries, Trial1.0 to Trial3.84 Data columns (total 5 columns): CHUNK_NAME 1008 non-null values LAMBDA 1008 non-null values BETA 1008 non-null values HIT_RATE 1008 non-null values AVERAGE_RECIPROCAL_HITRATE 1008 non-null valueschunks=['300_321','322_343','344_365','36...

何时申请(pd.to_numeric)以及何时在python中使用astype(np.float64)?【代码】

我有一个名为xiv的pandas DataFrame对象,它有一列int64 Volume测量值.In[]: xiv['Volume'].head(5) Out[]: 0 252000 1 484000 2 62000 3 168000 4 232000 Name: Volume, dtype: int64我已阅读其他帖子(如this和this),提出以下解决方案.但是当我使用任何一种方法时,它似乎不会改变底层数据的dtype:In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])In[]: xiv['Volume'].dtypes Out[]: dtype('int64')要么…In[...