Python Forum
Error in find pearson correlation function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in find pearson correlation function
#1
Output:
functional group A_OS A'_OS A_HOMO- ... μĀ μ𝐵 ̅ t Perovskite 0 RbTaO3 1 1 0.000 ... 0.0000 0.0000 1.0815 1 1 Ba2PrIrO6 2 2 0.000 ... 0.0000 0.0804 0.9957 1 2 Ba2PrOsO6 2 2 0.000 ... 0.0000 0.0786 0.9946 1 3 Ba2SmIrO6 2 2 0.000 ... 0.0000 0.0196 1.0370 1 4 Ba2TbIrO6 2 2 0.000 ... 0.0000 0.0482 1.0172 1 ... ... ... ... ... ... ... ... ... ... 1500 Yb2NiMnO6 3 3 0.000 ... 0.0000 0.0571 0.9203 1 1501 Yb2NiRuO6 3 3 0.000 ... 0.0000 0.0250 0.9001 0 1502 GdBaCo2O6 3 2 1.067 ... 0.1193 0.0286 1.0205 1 1503 KLaMnWO6, 1 3 0.555 ... 0.0768 0.0089 1.0202 1 1504 NaTbMnWO6, 1 3 -0.264 ... 0.0439 0.0089 0.9492 1 [1505 rows x 36 columns]
The data set image is also attached.
This is the form of data set I have and I want to obtained Pearson's correlation of the file. I am running this in Jupyter Notebook.
The commands are as:

import numpy as np
import matplotlib.pyplot as plt     #.pyplot is a module
import pandas as pd
import seaborn as sns


x = pd.read_csv("formability_database.csv")

R1 = np.corrcoef(x, rowvar=False)
The error message is given below:

Error:
TypeError Traceback (most recent call last) <ipython-input-31-3c1804e8149c> in <module>() ----> 1 R1 = np.corrcoef(x, rowvar=False) 2 #print(x.corr()) <__array_function__ internals> in corrcoef(*args, **kwargs) 3 frames /usr/local/lib/python3.7/dist-packages/numpy/lib/function_base.py in corrcoef(x, y, rowvar, bias, ddof, dtype) 2681 warnings.warn('bias and ddof have no effect and are deprecated', 2682 DeprecationWarning, stacklevel=3) -> 2683 c = cov(x, y, rowvar, dtype=dtype) 2684 try: 2685 d = diag(c) <__array_function__ internals> in cov(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights, dtype) 2516 w *= aweights 2517 -> 2518 avg, w_sum = average(X, axis=1, weights=w, returned=True) 2519 w_sum = w_sum[0] 2520 <__array_function__ internals> in average(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned) 378 379 if weights is None: --> 380 avg = a.mean(axis) 381 scl = avg.dtype.type(a.size/avg.size) 382 else: /usr/local/lib/python3.7/dist-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims, where) 180 if isinstance(ret, mu.ndarray): 181 ret = um.true_divide( --> 182 ret, rcount, out=ret, casting='unsafe', subok=False) 183 if is_float16_result and out is None: 184 ret = arr.dtype.type(ret) TypeError: unsupported operand type(s) for /: 'str' and 'int'
Larz60+ write Mar-01-2022, 04:27 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode on future posts.

Attached Files

Thumbnail(s)
   
Reply
#2
This is a good example of where the very last line of the error is sufficient to show the cause of the problem (although not necessarily to fix it!).

TypeError: unsupported operand type(s) for /: 'str' and 'int'

You are trying to work out the correlation coefficient of a column that includes strings.

numpy is trying to calculate the correlation coefficient of each pair of columns, but column 1 (functional groups) is all strings, so when numpy tries to compute the average, it adds up all those strings (concatenation) to get one large string, then tries to divide by the number of rows.

I'm not sure how to fix that, there doesn't seem to be any facility in np.corrcoef to select which columns (or rows) are used.

By the way, the code you have copied and pasted here seems to be different from the code in your screen shot.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting a valueError. And not sure why? My goal is to visualize the correlation ReadytoCode 0 472 Dec-11-2023, 05:33 AM
Last Post: ReadytoCode
  Error when using FIND WJSwan 2 898 Jan-27-2023, 05:49 AM
Last Post: deanhystad
  how can a function find the name by which it is called? Skaperen 18 3,470 Aug-24-2022, 04:52 PM
Last Post: Skaperen
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,509 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  How to remove a column or two columns in a correlation heatmap? lulu43366 3 5,219 Sep-30-2021, 03:47 PM
Last Post: lulu43366
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,269 Aug-17-2021, 05:24 PM
Last Post: Franky77
  pdfminer package: can't find exgtract_text function Pavel_47 7 5,267 Jan-25-2021, 03:31 PM
Last Post: Pavel_47
  List of error codes to find (and count) in all files in a directory tester_V 8 3,686 Dec-11-2020, 07:07 PM
Last Post: tester_V
  How do I find if a function has been defined? AndyHolyer 3 2,271 Jul-24-2020, 01:39 PM
Last Post: Gribouillis
  Cant find root cause of thread.lock error burlyboys 0 1,552 May-18-2020, 12:51 PM
Last Post: burlyboys

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020