I am trying to build an app by using python and rpy2. I read a .csv file (see below, table_mean_plain) and I would like to plot an histogram of its percentage of explained variances. When I use the code in Jupyter notebook it is working fine. But when I try through streamlit the code is loading forever and I receive the message:
2022-10-25 17:08:05.231 Uncaught app exception
Traceback (most recent call last):
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 562, in _run_script
exec(code, module.__dict__)
File "/Users/XX/Documents/XX/pages/4_Visualisations.py", line 69, in <module>
res_pca = FactoMineR.PCA(my_data)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/robjects/functions.py", line 201, in __call__
return (super(SignatureTranslatedFunction, self)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/robjects/functions.py", line 124, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/rinterface_lib/conversion.py", line 45, in _
cdata = function(*args, **kwargs)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/rinterface.py", line 810, in __call__
raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Erreur dans (function (title, width, height, pointsize, family, antialias, :
impossible de créer le périphérique cible quartz(), le type fourni n'est peut-être pas supporté
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
[9] 59974 illegal hardware instruction streamlit run 1_Workbench.py
Here is my code
from rpy2.robjects.packages import importr, data
from rpy2.robjects.vectors import DataFrame
from rpy2.ipython.ggplot import image_png
import streamlit as st
import matplotlib.pyplot as plt
from rpy2 import robjects
import matplotlib.image as mpimg
utils = importr('utils')
corrplot = importr('corrplot')
FactoMineR = importr('FactoMineR')
factoextra = importr('factoextra')
my_datafile = 'table/plain/table_mean_plain.csv'
#my_data = utils.read_csv(my_datafile)
my_data = DataFrame.from_csvfile(my_datafile, row_names="X")
#rownames(my_data = my_data$X
#my_dataX = NULL
# Do PCA
# --------
res_pca = FactoMineR.PCA(my_data)
# Eigen values / Variance
# --------
eig_val = factoextra.get_eigenvalue(res_pca)
st.pyplot(display(image_png(factoextra.fviz_eig(res_pca, addlabels = True))))
The corresponding .csv file is
,logistic-regression,svm-linear,svm-rbf,Gnb,decision-tree,random-forest,XGBoost,MLP,Ensemble,GEV,iForest,DevNet
news,0.827,0.566,0.724,0.681,0.687,0.852,0.865,0.859,0.866,0.87,0.497,0.666
telE,0.761,0.552,0.623,0.756,0.86,0.944,0.957,0.915,0.95,0.941,0.399,0.717
bank,0.832,0.62,0.772,0.817,0.687,0.848,0.841,0.848,0.855,0.873,0.625,0.793
member,0.648,0.488,0.514,0.625,0.574,0.696,0.695,0.669,0.707,0.667,0.545,0.598
dsn,0.735,0.708,0.785,0.707,0.761,0.884,0.897,0.754,0.884,0.766,0.568,0.661
mobile,0.889,0.432,0.393,0.842,0.776,0.9,0.906,0.9,0.908,0.907,0.7,0.852
campaign,0.907,0.543,0.668,0.809,0.709,0.928,0.933,0.923,0.935,0.924,0.65,0.832
HR,0.85,0.766,0.841,0.762,0.615,0.809,0.797,0.825,0.838,0.827,0.589,0.72
sato,0.794,0.747,0.8,0.729,0.651,0.808,0.809,0.799,0.825,0.82,0.5,0.739
uci,0.854,0.586,0.897,0.859,0.818,0.901,0.905,0.847,0.913,0.91,0.663,0.793
TelC,0.844,0.632,0.797,0.814,0.658,0.819,0.825,0.842,0.841,0.85,0.295,0.784
median_AUC,0.832,0.586,0.772,0.762,0.687,0.852,0.866,0.847,0.866,0.87,0.568,0.739
Rank,5.67,10.67,7.5,8.08,9.0,4.0,3.25,5.0,1.83,2.67,11.42,8.92
I am trying to get into writing code to scrape stock web pages, I came across this youtube video https://www.youtube.com/watch?v=2BrpKpWwT2A. When I copy and paste the following code (from the video)
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use("ggplot")
start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
df = web.DataReader("TSLA", "yahoo", start, end)
print(df.head())
I still get the same error (Full traceback is too long to add here) but the last few lines say:
File "/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 363, in getpeercert
'subjectAltName': get_subj_alt_name(x509)
File "/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 213, in get_subj_alt_name
ext = cert.extensions.get_extension_for_class(
File "/anaconda3/lib/python3.6/site-packages/cryptography/utils.py", line 170, in inner
result = func(instance)
File "/anaconda3/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/x509.py", line 127, in extensions
self._backend, self._x509
File "/anaconda3/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/decode_asn1.py", line 252, in parse
value = handler(backend, ext_data)
File "/anaconda3/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/decode_asn1.py", line 438, in _decode_subject_alt_name
_decode_general_names_extension(backend, ext)
File "/anaconda3/lib/python3.6/site-packages/cryptography/x509/extensions.py", line 1262, in __init__
self._general_names = GeneralNames(general_names)
File "/anaconda3/lib/python3.6/site-packages/cryptography/x509/extensions.py", line 1217, in __init__
"Every item in the general_names list must be an "
TypeError: Every item in the general_names list must be an object conforming to the GeneralName interface
Any ideas as to what I could be doing wrong?
I tried the above code and got the following output:
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import time
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
import numpy as np
df=pd.read_excel('currencynames.xlsx', sheet_name='Sheet1')
writer = pd.ExcelWriter('cryptodata.xlsx', engine='xlsxwriter')
currencies = df['Currency Name']
print(currencies[0])
for i in range(0,1465,1):
print("https://coinmarketcap.com/currencies/"+currencies[i]+"/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))
currency_market_info = pd.read_html("https://coinmarketcap.com/currencies/"+currencies[i].lower()+"/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))[0]
currency_market_info = currency_market_info.assign(Date=pd.to_datetime(currency_market_info['Date']))
currency_market_info.loc[currency_market_info['Volume']=="-",'Volume']=0
currency_market_info['Volume'] = currency_market_info['Volume'].astype('int64')
currency_market_info.head()
currency_market_info.to_excel(writer, sheet_name=currencies[i])
currency_market_info = 0
print("Done with "+currencies[i])
C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py:816:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y) Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/SAU/Desktop/deep learning/cryptodata/data.py', wdir='C:/Users/SAU/Desktop/deep learning/cryptodata')
File
"C:\Users\SAU\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 880, in runfile
execfile(filename, namespace)
File
"C:\Users\SAU\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/SAU/Desktop/deep learning/cryptodata/data.py", line
19, in
currency_market_info.loc[currency_market_info['Volume']=="-",'Volume']=0
File "C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py",
line 879, in wrapper
res = na_op(values, other)
File "C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py",
line 818, in na_op
raise TypeError("invalid type comparison")
TypeError: invalid type comparison
I'm using pyalgotrade to write a trading algo. I got the error above and can't seem to fix it. I'm trying to use the "SLOW STOCHASTIC", any help on solving this error and getting the slow stochastic to work is greatly appreciated:
Error:
C:\Users\...\Desktop>python bobo.py
Traceback (most recent call last):
File "bobo.py", line 114, in <module>
main()
File "bobo.py", line 110, in main
run_strategy(10,inst,2,14,5,2,3)
File "bobo.py", line 102, in run_strategy
myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
File "bobo.py", line 26, in __init__
self.__stoch = indicator.STOCH(self.__prices,fastk_period,slowk_period,slowd_period)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 803, in STOCH
ret = call_talib_with_hlc(barDs, count, talib.STOCH, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 93, in call_talib_with_hlc
high = bar_ds_high_to_numpy(barDs, count)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 45, in bar_ds_high_to_numpy
return value_ds_to_numpy(barDs.getHighDataSeries(), count)
AttributeError: 'SequenceDataSeries' object has no attribute 'getHighDataSeries'
Code:
from pyalgotrade.tools import yahoofinance
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import stoch
from pyalgotrade import dataseries
from pyalgotrade.technical import ma
from pyalgotrade import technical
from pyalgotrade.technical import highlow
from pyalgotrade import bar
from pyalgotrade.talibext import indicator
from pyalgotrade.technical import rsi
import numpy
import talib
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed,instrument,smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period):
strategy.BacktestingStrategy.__init__(self, feed, 1000) #change portfolio amount
self.__position = None
self.__instrument = instrument
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__sma = ma.SMA(self.__prices, smaPeriod)
self.__ema = ma.EMA(self.__prices, emaPeriod)
self.__rsi = rsi.RSI(self.__prices, rsiPeriod)
self.__stoch = indicator.STOCH(self.__prices,fastk_period,slowk_period,slowd_period)
Now I'm getting the error:
Traceback (most recent call last):
File "bobo.py", line 103, in <module>
main()
File "bobo.py", line 99, in main
run_strategy(inst,10,250,14,5,5,5)
File "bobo.py", line 90, in run_strategy
myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
File "bobo.py", line 28, in __init__
self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 803, in STOCH
ret = call_talib_with_hlc(barDs, count, talib.STOCH, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 105, in call_talib_with_hlc
return talibFunc(high, low, close, *args, **kwargs)
File "talib/func.pyx", line 9388, in talib.func.STOCH (talib\func.c:87125)
Exception: inputs are all NaN
Try this:
self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)
The stochastic oscillator is expecting a bar dataseries, not a regular one.
Pandas' corrwith work fine with SPY but not OK with ^GSPC.
Can anybody help me ? Thanks in advance.
import pandas.io.data as web
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import csv
Symbol = ['^GSPC','SPY','XLK','XLV','XLY','XLP','XLE','XLF','XLI','XLB','XLU']
STOCK = web.DataReader(name=Symbol,data_source='yahoo',start='2000-1-1',end='2009-3-1')
STOCK['PercentA'] = np.round(np.log(STOCK['Adj Close']/STOCK['Adj Close'].shift(1)),3)
### OK wiht SPY, but not OK wiht ^GSPC
STOCKCORR = np.round(STOCK['PercentA'].corrwith(STOCK['PercentA'].^GSPC),3)
StockNan = STOCKCORR[~STOCKCORR.isnull()]
StockOrder = StockNan.order().iloc[::-1]
StockRows = StockOrder.iloc[:20]
print StockRows
the error messages:
runfile('D:/###pg/yahoo_final_Chang/150830__Revised/test.py', wdir='D:/###pg/yahoo_final_Chang/150830__Revised')
Traceback (most recent call last):
File "", line 1, in
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/###pg/yahoo_final_Chang/150830__Revised/test.py", line 16
STOCKCORR = np.round(STOCK['PercentA'].corrwith(STOCK['PercentA'].^GSPC),3)
^
SyntaxError: invalid syntax