我正在尝试使用来自sklearn的LinearRegression,并且收到“无法将字符串转换为浮点型”的信息。数据框的所有列都是浮点型的,输出y也是浮点型的。我看过其他帖子,建议将其转换为浮动内容。
<class 'pandas.core.frame.DataFrame'>
Int64Index: 789 entries, 158 to 684
Data columns (total 8 columns):
f1 789 non-null float64
f2 789 non-null float64
f3 789 non-null float64
f4 789 non-null float64
f5 789 non-null float64
f6 789 non-null float64
OFF 789 non-null uint8
ON 789 non-null uint8
dtypes: float64(6), uint8(2)
memory usage: 44.7 KB
type(y_train)
pandas.core.series.Series
type(y_train[0])
float
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,Y,random_state=0)
X_train.head()
from sklearn.linear_model import LinearRegression
linreg = LinearRegression().fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
ValueError Traceback (most recent call last)
<ipython-input-282-c019320f8214> in …Run Code Online (Sandbox Code Playgroud) 我的数据框有子类别,每个类别下(cat,dog,bird),统计信息呈现.我需要删除行,如果它们包含count和中的信息freq,并且只保留行sd和mean值.有些值是 NaN.
ValueError 发生在我的代码中.
DF:
var stats A B C
cat mean 2 3 4
NaN sd 2 1 3
NaN count 5 2 6
NaN freq 3 1 19
dog mean 8 1 2
NaN sd 2 1 3
NaN count 4 6 1
NaN freq 3 1 19
bird mean 2 3 4
NaN sd 2 1 3
NaN count 5 2 6 …Run Code Online (Sandbox Code Playgroud) 我的操作系统是 Ubuntu。
当我运行我的 keras 模型时,我收到了这个错误消息。
我在谷歌上搜索了一些解决方案,但它们不起作用。例如,我已经尝试了这篇文章中提到的解决方案: How to fix Python ValueError:bad marshal data?
Using TensorFlow backend.
[INFO] loading network...
Traceback (most recent call last):
File "classify.py", line 43, in <module>
color_model = load_model(color_model_path, custom_objects={"tf": tf})
File "/home/ubuntufashion/.local/lib/python2.7/site-
packages/keras/engine/saving.py", line 260, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/ubuntufashion/.local/lib/python2.7/site-
packages/keras/engine/saving.py", line 334, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/ubuntufashion/.local/lib/python2.7/site-
packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntufashion/.local/lib/python2.7/site-
packages/keras/utils/generic_utils.py", line 145, in
deserialize_keras_object
list(custom_objects.items())))
File "/home/ubuntufashion/.local/lib/python2.7/site-
packages/keras/engine/network.py", line 1017, in from_config
process_layer(layer_data) …Run Code Online (Sandbox Code Playgroud) #conftest.py
include pytest
def pytest_addoption(parser):
parcer.addoption("--add", action="append")
@pytest.fixture(scope='session')
def adding(request):
name_value = request.config.option.add
if name_value == "plus":
arg1 = 1
arg2 = 2
return arg1, arg2
#addition.py
@mark.first
def test_Valid_US_Phone_Number_1(adding):
val1, val2 = adding
assert val1 + val2 == 3
Run Code Online (Sandbox Code Playgroud)
运行命令
$pytest -m first plusplus.py --add plus
Run Code Online (Sandbox Code Playgroud)
我收到以下错误,有人可以帮忙吗?
File "c:\users\g702823\appdata\local\continuum1\anaconda3\lib\site-packages\_pytest\config\argparsing.py", line 72, in addoption
self._anonymous.addoption(*opts, **attrs)
File "c:\users\g702823\appdata\local\continuum1\anaconda3\lib\site-packages\_pytest\config\argparsing.py", line 303, in addoption
raise ValueError("option names %s already added" % conflict)
ValueError: option names {'--add'} already added
Run Code Online (Sandbox Code Playgroud) Q = [np.array([0, 1]), np.array([1, 2]), np.array([2, 3]), np.array([3, 4])]
for q in Q:
print(q in Q)
Run Code Online (Sandbox Code Playgroud)
运行上面的代码,它在第一次迭代时给出结果“True”,而随后出现 ValueError。
真的
ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
我不知道为什么它在第二次迭代时开始出错。任何人都可以帮助我plz..
运行以下Python Panda代码时:
xl = pd.ExcelFile(dataFileUrl)
sheets = xl.sheet_names
data = xl.parse(sheets[0])
colheaders = list(data)
Run Code Online (Sandbox Code Playgroud)
我收到ValueError:
Must explicitly set engine if not passing in buffer or path for io
Run Code Online (Sandbox Code Playgroud)
毫无疑问,该文件肯定是excel文件。
怎么了?
以下是我的代码(仅模拟数字):
import pandas as pd
d = {'x' : [1,4,6,9],
'y' : [1,4,6,8]}
df = pd.DataFrame(d)
ct = pd.concat([df.x,
pd.cut(df.y, bins=2)], axis=1)
gp = ct.groupby('x').y.value_counts().unstack().fillna(0)
print(gp)
print(gp[gp.columns[0]])
gp[gp.columns[0]] = gp[gp.columns[0]]/10
Run Code Online (Sandbox Code Playgroud)
print(gp) 得到:
y (0.993, 4.5] (4.5, 8.0]
x
1 1.0 0.0
4 1.0 0.0
6 0.0 1.0
9 0.0 1.0
Run Code Online (Sandbox Code Playgroud)
print(gp[gp.columns[0]]) 给出这个:
x
1 1.0
4 1.0
6 0.0
9 0.0
Name: (0.993, 4.5], dtype: float64
Run Code Online (Sandbox Code Playgroud)
但是以下行:
gp[gp.columns[0]] = gp[gp.columns[0]]/10
Run Code Online (Sandbox Code Playgroud)
抛出此错误:
ValueError: Buffer has wrong number of dimensions (expected 1, …Run Code Online (Sandbox Code Playgroud) 我是Python的新手,我在Lynda上使用视频教程来帮助我构建Social WebApp的框架.我正在尝试使用cmd python manage.py runserver从cmd 运行服务器,但是,我一直遇到此错误消息.
CMD PROMPT ERROR
Traceback (most recent call last):
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\functional.py", line 36, in __get__ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码行将“数据”(大小为112943)截断为形状(1,15000):
data = np.reshape(data, (1, 15000))
Run Code Online (Sandbox Code Playgroud)
但是,这给了我以下错误:
ValueError: cannot reshape array of size 112943 into shape (1,15000)
Run Code Online (Sandbox Code Playgroud)
关于如何解决此错误的任何建议?
任务1
编写一个脚本,从 STDIN 读取字符串,如果字符串超过 10 个字符,则引发 ValueError 异常,否则打印读取的字符串。
我这样写代码
a = input("Enter a string")
if(len(a) > 10):
raise ValueError
else:
print(a)
Run Code Online (Sandbox Code Playgroud)
任务2
使用
try ... except子句。打印 except 块内的错误消息。
我现在对如何使用try-except这里感到困惑,因为要打印except块中的任何消息,程序必须在try块中失败。
我的输入将是PythonIsAmazing
python ×10
valueerror ×10
pandas ×4
numpy ×2
dataframe ×1
django ×1
django-2.0 ×1
marshalling ×1
pytest ×1
python-3.x ×1
reshape ×1
scikit-learn ×1
string ×1
try-except ×1
ubuntu ×1