473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

statistical analysis with decimal data retrieved from MySQL DB

2 New Member
I'm a complete Python/MySQL newbie - trying to retrieve and manipulate data from a MySQL DB over a VPN/LAN from an Ubuntu/Python shell.

I've imported all the modules I think I need - MySQLdb, decimal, scipy, scipy.stats, numpy. I have been able to open the connector to the MySQL DB and retrieve the data of interest. My problem is as follows

1. I open a connection with

mydb = MySQLdb.connect(host = "xxx", port = yyy, user = "zzz", passwd = "nnnn", db = "whatineed")
cursor = mydb.cursor()

2. For some reason, each entry is a tuple though I am retrieving a single column from the MySQL table with a command

cursor.execute("SELECT difference FROM growth_record")

3. Data from the DB comes back in the format

((Decimal('-3.0600'),), (Decimal('-0.8500'),), (Decimal('-1.1900'),), (Decimal('-1.7000'),), (Decimal('0.4800'),),...

4. I am able to pull out the individual values by doing

result = cursor.fetchall()
for i in range(len(result)):
a = result[i][0]

OR to get a floating point result
for i in range(len(result)):
a = float(str(result[i][0]))

5. My problems

a) I am not able to create an array with the assignment
a[i] = result[i][0] - keep getting a type mismatch. I'd like to create an array with the direct results without the Tuple returned by the DB.

b) I am not able to perform statistical analyses from scipy.stats (e.g. mean) with the results. The functions keep expecting floating point arguments, the numbers returned from the DB are decimal numbers.

Is there an obviously simple way to do either?

Thank you
Bapi
Apr 29 '10 #1

✓ answered by Glenton

Hi

Good work. Here's an interactive session, which should help. I've defined result as the tuple in your point 3, and I've imported decimal and scipy.stats.

Then
Expand|Select|Wrap|Line Numbers
  1. In [16]: a=[]
  2.  
  3. In [17]: for r in result:
  4.    ....:     a.append(float(r[0]))
  5.    ....:     
  6.    ....:     
  7.  
  8. In [18]: a
  9. Out[18]: 
  10. [-3.0600000000000001,
  11.  -0.84999999999999998,
  12.  -1.1899999999999999,
  13.  -1.7,
  14.  0.47999999999999998]
  15.  
  16. In [19]: mean(a)
  17. Out[19]: -1.264
  18.  
For numpy arrays, instead of lists, the append function generally doesn't work/exist in my experience. So it's better to define it up front at the right size, and then update the elements:

Expand|Select|Wrap|Line Numbers
  1. In [21]: from numpy import *
  2.  
  3. In [22]: a=zeros((5))
  4.  
  5. In [23]: a
  6. Out[23]: array([ 0.,  0.,  0.,  0.,  0.])
  7.  
  8. In [26]: for i,r in enumerate(result):
  9.    ....:     a[i]=r[0]
  10.    ....:     
  11.    ....:     
  12.  
  13. In [27]: a
  14. Out[27]: array([-3.06, -0.85, -1.19, -1.7 ,  0.48])
  15.  
  16. In [28]: mean(a)
  17. Out[28]: -1.264
  18.  
In general the assignment (a[i]=foo) only works if a[i] already exists, which may be the source of problem a.

Also, things like mean only work on lists or arrays or similar iterables.

Good luck, and let us know how it goes!

2 2639
Glenton
391 Recognized Expert Contributor
Hi

Good work. Here's an interactive session, which should help. I've defined result as the tuple in your point 3, and I've imported decimal and scipy.stats.

Then
Expand|Select|Wrap|Line Numbers
  1. In [16]: a=[]
  2.  
  3. In [17]: for r in result:
  4.    ....:     a.append(float(r[0]))
  5.    ....:     
  6.    ....:     
  7.  
  8. In [18]: a
  9. Out[18]: 
  10. [-3.0600000000000001,
  11.  -0.84999999999999998,
  12.  -1.1899999999999999,
  13.  -1.7,
  14.  0.47999999999999998]
  15.  
  16. In [19]: mean(a)
  17. Out[19]: -1.264
  18.  
For numpy arrays, instead of lists, the append function generally doesn't work/exist in my experience. So it's better to define it up front at the right size, and then update the elements:

Expand|Select|Wrap|Line Numbers
  1. In [21]: from numpy import *
  2.  
  3. In [22]: a=zeros((5))
  4.  
  5. In [23]: a
  6. Out[23]: array([ 0.,  0.,  0.,  0.,  0.])
  7.  
  8. In [26]: for i,r in enumerate(result):
  9.    ....:     a[i]=r[0]
  10.    ....:     
  11.    ....:     
  12.  
  13. In [27]: a
  14. Out[27]: array([-3.06, -0.85, -1.19, -1.7 ,  0.48])
  15.  
  16. In [28]: mean(a)
  17. Out[28]: -1.264
  18.  
In general the assignment (a[i]=foo) only works if a[i] already exists, which may be the source of problem a.

Also, things like mean only work on lists or arrays or similar iterables.

Good luck, and let us know how it goes!
May 4 '10 #2
ipab
2 New Member
Glenton,

Thanks. Both methods worked. I appreciate your response.

Regards
Bapi
p.s. do you happen to know of any math modules that work with the Decimal data type?
May 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: chris | last post by:
I open MySQL Manager and connect to my database. I then insert one record in to an InnoDB table in MySQL (4.0 and 4.1 alpha hosted on XP running mysqld --console). I then retrieve the record...
0
by: Stormblade | last post by:
Hey all, I have an existing JSP web application which retrieved data from a SQLServer database and displayed it. The data contained Unicode chars. This worked fine. I changed databases/JDBC...
4
by: John Bowman | last post by:
Hi, A couple Q's , so I've come to the experts<g>. 1) I need a definitive answer to the following debate. I've got a couple developers who disagree on the following question. We've got an XML...
1
by: invinfo | last post by:
keywords: mysql accounting currency decimal fixed "floating point" Quoting the manual: DECIMAL)] If D is omitted, the default is 0. If M is omitted, the default is 10. All basic calculations...
3
by: Thomas Nelson | last post by:
Sorry if this is a FAQ, but I couldn't find a good summary through google. What kinds of statistical analysis tools exist in python? I really just need t-tests, chi-squared test, and other such...
25
by: Wim Cossement | last post by:
Hello, I was wondering if there are a few good pages and/or examples on how to process form data correctly for putting it in a MySQL DB. Since I'm not used to using PHP a lot, I already found...
0
by: John Henry | last post by:
I am looking for a simple Python function for handling a set of time series data. For instance, I might have the following raw data (year's worth): 1/1/2005 12:00 AM 11.24 1/1/2005 12:10...
4
by: Talbot Katz | last post by:
Greetings Pythoners! I hope you'll indulge an ignorant outsider. I work at a financial software firm, and the tool I currently use for my research is R, a software environment for statistical...
1
by: yucefrizk | last post by:
hello all, I'm trying to write a code to retreive data from a database to an excel file, I achieved my code and everything is going good by running it through unix, but when I create a webpage to...
1
by: Laphan | last post by:
Hi All My ASP driven site has always used MySQL as the DB backbone, mainly as the hosting costs of MySQL are far cheaper than SQL Server at the mo, and it has worked fine until my ISP thought...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.