1

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
Thomas K
  • 39,200
  • 7
  • 84
  • 86
Sampath Rajapaksha
  • 111
  • 1
  • 1
  • 11
  • Please don't post code or output as images. In a few months time that image may disappear and then your question will be no help to anyone else in the same situation. Please edit your question to include the text of your IPython session. – Luke Woodward Jul 03 '16 at 07:28
  • `a` is a list. Not a float. And please don't include images. Paste all code here. – Rahul Jul 03 '16 at 07:29
  • thanks very much for the reply. i have edited the post. any help would be really appreciate – Sampath Rajapaksha Jul 03 '16 at 16:04

4 Answers4

2

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])
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
1

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)
Evan Samanas
  • 516
  • 3
  • 6
0

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
>>> 
tale852150
  • 1,618
  • 3
  • 17
  • 23
0

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)

Medhat
  • 1,622
  • 16
  • 31