2

I want to convert the data from the .csv file into a NumPy array and then find the mean of the data.

import csv
import numpy as np
import statistics as stat
with open('pima-indians-diabetes.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        x = np.array(row[1])
        z = stat.mean(x)
        print(z)
furas
  • 134,197
  • 12
  • 106
  • 148
B. Adams
  • 21
  • 2

1 Answers1

0

numpy arrays have a mean method built-in.

>>> import numpy
>>> data = [3, 6, 3, 5, 3, 2]
>>> numpy.array(data).mean()
3.6666666666666665

To get in the csv, see the genfromtxt function: https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html