Sampath Rajapaksha
Sampath Rajapaksha

Reputation: 101

python log() function TypeError

i have a data set called "sales" which is a SFrame. there's a column called "sqft_living" and i want to convert it to log value. data type of column is float. but when i try to convert it with log() function it asks a float although its already a float. here are the screenshot of codes and error. could you please help me to find the issue and convert the column to log

a=train_data['sqft_living']
a

result of a
dtype: float
Rows: 17384
[1180.0, 2570.0, 770.0, 1960.0,...]

this shows that "a" is float

then i used below code to transform to log value in to new column called 'log_sqft_living'

train_data['log_sqft_living']= log(a)

but it gives be below error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-6de995ee575f> in <module>()
----> 1 train_data['log_sqft_living']= log(a)

TypeError: a float is required

Upvotes: 1

Views: 480

Answers (4)

Medhat
Medhat

Reputation: 1652

If you need to get the log of SFrame you need to do it like:

from math import log train_data['sqft_living'].apply(log)

Upvotes: 0

Evan Samanas
Evan Samanas

Reputation: 516

In your example, a is an SArray of type float. It cannot be passed to any function that takes a float. You can however apply any function that takes a float to the SArray like this:

import math
log_a = a.apply(math.log)

Upvotes: 1

J.J. Hakala
J.J. Hakala

Reputation: 6214

numpy.log can be used with an array

>>> import numpy
>>> a = numpy.array([180.0, 2570.0, 770.0, 1960.0])
>>> b = numpy.log(a)
>>> b
array([ 5.19295685,  7.85166118,  6.64639051,  7.58069975])

Upvotes: 2

tale852150
tale852150

Reputation: 1638

Using Python 3.5 on Windows 10 using Python shell

As pointed out by Rahul, a is a list.
You can take an element of the list as shown below and it would work:

>>> import math
>>> train_data=[12.1, 14.5, 56.5, 43.2]
>>> a=train_data
>>> a
[12.1, 14.5, 56.5, 43.2]
>>> another_train_data=math.log(a[1])
>>> another_train_data
2.6741486494265287
>>> 

Upvotes: 0

Related Questions