Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Numpy provides a function numpy.loadtxt() which reads data from a file into an a

ID: 3889380 • Letter: N

Question

Numpy provides a function numpy.loadtxt() which reads data from a file into an array.

The file sample-correlation.txt has two sets of measurements that might be correlated. Make a script that loads data from the file, calculates the Pearson correlation coefficient( numpy.corrcoef()), and prints the correlation coefficient between the two variables.

#sample-correlation.txt

# Resistor 1
# V I(mA)
1.256 3.7
3.56 10.7
5.75 17.3
9.18 27.8
16.81 51.0
20.8 63.3
23.7 72.2

Thank you so much!

Explanation / Answer

>>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1 2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72 F 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '