How can use scipy with a datetime without the right formatting? - python

I am trying to visualise a dataset and its average with scipy.interpolate and matplotlb.
But when im trying to run the code that should work perfectly fine it gives me the error:
File "mittel.py", line 19, in <module>
p1 = polyfit(x, y, 1)
File "C:\Users\simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\polynomial.py", line 589, in polyfit
x = NX.asarray(x) + 0.0
TypeError: can only concatenate str (not "float") to str
And the code is:
import time as ti
import pandas as pd
from numpy import *
from matplotlib import pyplot as plt
import csv
from sklearn import preprocessing, svm
from sklearn.model_selection import train_test_split
from scipy.interpolate import *
data = pd.read_csv("includes\\csv.csv")
x = array(data["day"])
y = array(data["balance"])
p1 = polyfit(x, y, 1)
print(p1)
plt.plot(x, y, "o")
plt.plot(x, polyval(p1, x), "-r")
plt.show()
I have already tried to convert the x array to a string with
x = str(x)
but that didnt help at all.
My csv file looks like this:
balance,day
242537,28-5
246362,29-5
246659,30-5
246844,31-5
Do you know why that error accurs?

x = NX.asarray(x) + 0.0
TypeError: can only concatenate str (not "float") to str
As you can see here, + is interpreted to concatenate two strings, whereas you need to add float. So instead of converting x to a string object, try converting x to a float object:
x = float(array(data["day"]))
y = float(array(data["balance"]))

Related

How to use math function in Python

How to execute this code:
import numpy as np
import math
x = np.arange(1,9, 0.5)
k = math.cos(x)
print(x)
I got an error like this:
TypeError: only size-1 arrays can be converted to Python scalars
Thank you in advance.
So this is happening because math.cos doesn't accept numpy arrays larger than size 1. That's why if you had a np array of size 1, your approach would still work.
A simpler way you can achieve the result is to use np.cos(x) directly:
import numpy as np
x = np.arange(1,9, 0.5)
k = np.cos(x)
print(x)
print(k)
If you have to use the math module, you can try iterating through the array and applying math.cos to each member of the array:
import numpy as np
import math
x = np.arange(1,9,0.5)
for item in x:
k = math.cos(item)
print(k) # or add to a new array/list
You're looking for something like this?
import numpy as np
import math
x = np.arange(1,9, 0.5)
for ang in x:
k = math.cos(ang)
print(k)
You are trying to pass ndarray (returned by arange) to a function, which expects just real number. Use np.cos instead.
If you want pure-Python:
You can use math.fun in map like below:
import math
x = range(1,9)
print(list(map(math.cos, x)))
Output:
[0.5403023058681398, -0.4161468365471424, -0.9899924966004454, -0.6536436208636119, 0.2836621854632263, 0.9601702866503661, 0.7539022543433046, -0.14550003380861354]

How to plot a branched sin(x) function

I'm a noob practicing how to use pylab, matplot lib etc.
Somehow I'm not able to plot this simple branched sin(x) function in pylab/matplotlib.
from math import sin
import pylab as plb
def f(x):
if sin(x) > 0:
return sin(x)
else:
return 0
x = plb.linspace(-4,4,10000)
plb.plot(x,f(x))
plb.show()
The following error outputs when I run the program:
Traceback (most recent call last):
File "C:/Users/...plot.py", line 12, in <module>
plb.plot(x,f(x))
File "C:/Users/......plot.py", line 5, in f
if sin(x) > 0:
TypeError: only size-1 arrays can be converted to Python scalars
Is there anyone who can help me out?
The inbuilt sine function inside math module accepts only a scalar value. You can use numpy sine instead to accomplish your plot as it accepts an array.
import numpy as np
import pylab as plb
def f(x):
sine_ = np.sin(x)
sine_[sine_< 0] = 0
return sine_
x = plb.linspace(-4,4,10000)
plb.plot(x,f(x))
plb.show()
The output is as shown below:
[]
However, as pointed out by Trenton McKinney, this answer states the use of pylab is no longer recommended. So, the alternate solution using matplotlib.pyplot is shown below:
import numpy as np
import matplotlib.pyplot as plt
def f(x):
sine_ = np.sin(x)
sine_[sine_< 0] = 0
return sine_
x = np.linspace(-4,4,10000)
plt.plot(x,f(x))
plt.show()
The output is the same as the image above.

Drawing a trendline using matplotlib

I've been trying to graph some price v time data, and I cannot figure a way of drawing a trendline here. The dates are datetime objects. The graph is fine. However, using polyfit as I do below throws up an error.
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
from datetime import datetime
import numpy as np
import matplotlib.pylab as plb
notes = pd.read_csv("tsla.csv")
notes.dropna(inplace=True)
date_list = notes['x']
price_list = notes['Close']
date_list = date_list.tolist()
price_list = price_list.tolist()
for i in range(len(date_list)):
date_list[i] = (date_list[i][:-8])
date_list[i] = date_list[i][:-5] + date_list[i][-3:-1]
##print(len(date_list[i]))
date_list[i] = datetime.strptime(date_list[i], "%m/%d/%y")
##print(date_list[i])
##print(date_list)
price_list = list(map(lambda x: int(x), price_list))
plt.plot(date_list, price_list)
plt.ylabel("Prices")
plt.xlabel("Dates")
# calc the trendline (it is simply a linear fitting)
z = np.polyfit(date_list, price_list, 1)
p = np.poly1d(z)
plb.plot(x,p(x),"r--")
##### Showing time series line graph below
plt.show()
Error below
Traceback (most recent call last):
File "/Users/ramapriyansrivatsanpd/Documents/Python for finance - fintech soc.py", line 42, in <module>
z = np.polyfit(date_list, price_list, 1)
File "<__array_function__ internals>", line 5, in polyfit
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/lib/polynomial.py", line 590, in polyfit
x = NX.asarray(x) + 0.0
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'float'
I know it's 10 months but maybe you or some other people
You could cast the Y-Axis to int() and the X-Axis to datetime-Object after that x = mdates.date2num(x).
After this it worked for me.

How to Prevent TypeError: only size-1 arrays can be converted to Python scalars from happening

I am trying to visualise a dataset with matplotlib.
The code is:
import time as ti
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import csv
from sklearn import preprocessing, svm
from sklearn.model_selection import train_test_split
from scipy.interpolate import *
data = pd.read_csv("includes\\csv.csv")
#x = array(data["day"])
#y = np.array(data["balance"])
x = float(np.array(data["day"]))
y = float(np.array(data["balance"]))
p1 = np.polyfit(x, y, 1)
print(p1)
plt.plot(x, y, "o")
plt.plot(x, polyval(p1, x), "-r")
plt.show()
The error that accurs is:
Traceback (most recent call last):
File "mittel.py", line 19, in <module>
x = float(np.array(data["day"]))
TypeError: only size-1 arrays can be converted to Python scalars
I am wondering why thats a thing because the csv file i am using is this simple:
balance,day
242537,28-5
246362,29-5
246659,30-5
246844,31-5
I have been working on this for hours.
Any answers appreciated.
Day column in your csv file is having value '28-5','29-5' ....
and np.array(data['day']) will result into a array so you cant cast array to float so getting TypeError.
change line 14-15 to this
x = [float(day_str.split('-')[0]) for day_str in np.array(data["day"])]
y = np.array(data["balance"], dtype=float)
I solved it by formatting it into a n/m/y format.

How to polyfit() an OpenCV calcHist() histogram?

I have something like this:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import numpy.polynomial.polynomial as poly
img = cv.imread('SomeImage.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv.calcHist([img],[i],None,[32],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,32])
x = np.linspace(0,histr.shape[0],1); # <== ERROR HERE
poly.polyfit(x, histr, 4)
I get the following error:
File "/Users/case/anaconda2/lib/python2.7/site-packages/numpy/polynomial/polynomial.py", line 1438, in polyfit
raise TypeError("expected x and y to have same length")
TypeError: expected x and y to have same length
I'm pretty new to this, but seems I'm missing something simple?
It looks like a minor syntax mistake when calling np.linspace. The correct syntax is
x = np.linspace(interval_start, interval_end, number_of_points)
so in your case, that would be
x = np.linspace(0, 1, histr.shape[0])

Categories