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)
2
  • numpy should have own function to read .csv or .tab data. Commented Apr 22, 2019 at 0:06
  • use numpy.loadtxt with delimiter=',' Commented Apr 22, 2019 at 0:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I attempted to find the mean using the NumPy method, but I got the error "cannot perform reduce with flexible type".
Thanks Matt VE, that solved my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.