Recently I have been working on a code and got stuck for days on this error. Basically the program plots a 3D colormap from csv file. I am using Python 3 with anaconda3.
https://drive.google.com/drive/folders/1hfL_TbfWwD6uZCgxiOa-xjWT1ChL2PUs?usp=sharing
This is the code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
DataX_Y_1D = np.loadtxt("datacsv_1d_xy.csv", delimiter=",")
X, Y = np.meshgrid(DataX_Y_1D[:,0], DataX_Y_1D[:,1])
Z = np.loadtxt("datacsv_2d_Z.csv", delimiter=",")
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
The problem like this:
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 1, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
NameError: name 'np' is not defined
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", dtype= "float", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff10'
you need to define np
import numpy as np
I was able to import your file with:
x,y = np.genfromtxt('test.csv', delimiter=',', unpack=True, skip_header=0)
Loading z data:
z_all = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)
A quick plot:
plt.imshow(z_all)
Gives:
3D colormap graph:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize=(10, 10) )
X, Y = np.genfromtxt('datacsv_1d_xy.csv', delimiter=',', unpack=True, skip_header=0)
X, Y = np.meshgrid(X, Y)
Z = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
Related
import numpy as np
#read in data and extract columns
cols = np.loadtxt('CST210.txt')
Day1Attendence = cols[:,0]
Day2Attendence = cols[:,1]
#calculate the total attendence over the two days
TotalAttendence = Day1Attendence + Day2Attendence
#create second array from Day1Attendence and Day2Attendence
outArray = np.column_stack((Day1Attendence,Day2Attendence))
#save to test file
np.savetxt('Totalattandence.txt',outArray, delimiter = 'A')
This is the error code I'm getting
runfile('/home/kasey/Documents/module1code.py',
wdir='/home/kasey/Documents') Traceback (most recent call last):
File "", line 1, in
runfile('/home/kasey/Documents/module1code.py', wdir='/home/kasey/Documents')
File
"/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py",
line 668, in runfile
execfile(filename, namespace)
File
"/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py",
line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/kasey/Documents/module1code.py", line 13, in
cols = np.loadtxt('CST210.txt')
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1101, in loadtxt
for x in read_data(_loadtxt_chunksize):
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1028, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1028, in
items = [conv(val) for (conv, val) in zip(converters, vals)]
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
746, in floatconv
return float(x)
ValueError: could not convert string to float: '"""'
I am trying to extract data from a txt file with numpy and I get this big error:
Traceback (most recent call last):
File "C:\Python36\machine learning\bayes_classifier.py", line 14, in
<module>
data = np.loadtxt(input_file, delimiter=",")
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1092, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1019, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1019, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 738, in floatconv
return float(x)
ValueError: could not convert string to float: '2.18'
notice the ValueError, what is the '2.18'?
Here is the full code:
### Naïve bayes classifier ###
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn import cross_validation
from utilities import visualize_classifier
# Input file containing data
input_file = "data_multivar_nb.txt"
# Load data from input file
data = np.loadtxt(input_file, delimiter=",")
x, y = data[:, :-1], data[:, -1]
and here is the file:
file link here
I'm trying to plot time series data using matplotlib using the following code
import matplotlib.pyplot as plt
x, y = data.shape
y_metrics = alarms_metrics + udp_alarms_metrics_names
print x, y
for j in range(1, y):
time_stamp = []
metric = []
plt.figure()
for i in range(x):
time_stamp.append(data_time[i][0])
metric.append(data[i][j])
plt.xlabel("Time")
plt.ylabel(str(y_metrics[j-1]))
plt.plot(time_stamp, metric)
plt.savefig(str(j))
The format of time_stamp is
['2017:02:01:14:44:00', '2017:02:01:14:44:01',...etc]
time_stamp is a list of type 'numpy.string_'
I'm getting the following error
Traceback (most recent call last):
File "/home/joseph/PycharmProjects/demo/src/alarm_log/alarm_db_plot.py", line 23, in <module>
plt.plot(time_stamp, metric)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3154, in plot
ret = ax.plot(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
return func(ax, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
self.add_line(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
self._update_line_limits(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
path = line.get_path()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
self.recache()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
x = np.asarray(xconv, np.float_)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2017:02:01:14:44:00
I think I might need to used matplotlib dateformatter to parse the time_stamp. I tried so but I couldn't figure out how. Any help please?
Not exactly sure what you're trying to plot without seeing the data, but you could try to convert the string to a datetime object. See here for a list with the format codes.
from datetime import datetime
time_stamp = ['2017:02:01:14:44:00', '2017:02:01:14:44:01']
time_stamp = [datetime.strptime(i, '%Y:%m:%d:%H:%M:%S') for i in time_stamp]
print(time_stamp)
I keep getting an error using the numpy loadtxt converter.
Your help is greatly appreciated
import numpy as np
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import strpdate2num
import urllib2
## global variables
eachStock = 'AAPL','GOOG','MSFT','AMZN','CMG'
for stock in eachStock:
stockFile = stock+'.txt'
date, closep, highp, lowp, openp, volume = np.loadtxt(eachStock, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
dFrame = Series(closep)
here is the first line in my text file
20040322,13.5200,13.6800,12.6100,12.6850,15850720
here is the error I keep getting
Traceback (most recent call last):
File "C:\Users\antoniozeus\Desktop\BuyAndHold.py", line 27, in <module>
converters={ 0: mdates.strpdate2num('%Y%m%d')})
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 233, in __call__
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
File "C:\Python27\lib\_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'AAPL' does not match format '%Y%m%d'
It seems like you mistyped stockFile (filename) as eachStock.
date, closep, highp, lowp, openp, volume = np.loadtxt(
stockFile, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
I wrote a Python script (below) which load data from a text file (using pandas) and checks the values in the columns.
import sys
import pandas as pd
import numpy as np
from numpy import ndarray
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from skimage import data
from skimage.feature import match_template
if __name__ == '__main__':
data = pd.read_csv('Fe_PSI_spt_refined.txt', sep=" ", header = None)
data.columns = ["Angle_number", "Omega", "Intensity", "X", "Y", "Address", "ID"]#, "flag"]
Number_of_projections = 181
Number_of_lines_in_txt = 3493
numrows = len(data)
counter_array = []
correlation_threshold_value = 0.7
a = np.zeros(Number_of_lines_in_txt)
output_file = ("output.txt")
for i in range(2, (Number_of_projections + 1)):
filename_cutouts_combined = ("cutouts_combined_%03i.txt" % (i))
filename_cutouts_combined_tag = ("cutouts_combined_tag_%03i.txt" % (i))
image = np.loadtxt(filename_cutouts_combined)
image_tagged = np.loadtxt(filename_cutouts_combined_tag)
for j in range(0, Number_of_lines_in_txt - 1):
print data.Angle_number[j], i
After one iteration of j I get the error below. Do you spot any error I should fix? Thanks
`Traceback (most recent call last):
File "Hyperbola_search.py", line 46, in <module>
print data.Angle_number[j], i
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 491, in __getitem__
result = self.index.get_value(self, key)
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1032, in get_value
return self._engine.get_value(s, k)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2661)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2476)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3215)
File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6450)
File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6394)
KeyError: 3491`
You load files into image and image_tagged, while a remains unused.
I don't know what data.Angle_number and numrows are, but they appear to be from libraries, not related to your files.