我试图用高斯朴素贝叶斯"分类器"来预测经济周期.
数据(输入X):
SPY Interest Rate Unemployment Employment CPI
Date
1997-01-02 56.05 7.82 9.7 3399.9 159.100
1997-02-03 56.58 7.65 9.8 3402.8 159.600
1997-03-03 54.09 7.90 9.9 3414.7 160.000
Run Code Online (Sandbox Code Playgroud)
目标(输出Y):
Economy
0 Expansion
1 Expansion
2 Expansion
3 Expansion
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.cross_validation import train_test_split
X = data
Y = target
model = GaussianNB
X_train, X_test, Y_train, Y_test = train_test_split(X,Y)
model.fit(X_train, Y_train)
Run Code Online (Sandbox Code Playgroud)
以下是错误:
TypeError Traceback (most recent call last)
<ipython-input-132-b0975752a19f> in <module>()
6 model = …Run Code Online (Sandbox Code Playgroud) 我能够将csv文件转换为pandas DataFormat并能够打印出表格,如下所示.但是,当我尝试打印出高度列时,我收到一个错误.我怎样才能解决这个问题?
import pandas as pd
df = pd.read_csv('/path../NavieBayes.csv')
print df #this prints out as seen below
print df.Height #this gives me the "AttributeError: 'DataFrame' object has no attribute 'Height'
Height Weight Classifer
0 70.0 180 Adult
1 58.0 109 Adult
2 59.0 111 Adult
3 60.0 113 Adult
4 61.0 115 Adult
Run Code Online (Sandbox Code Playgroud) 我有一个 C# 变量“值”,我想将它传递给 JavaScript Chartjs 数据对象。它呈现图表但不包括两个@p 值。请参阅下面的代码源:
.cshtml 文件:
@{
int p1 = 43;
int p2 = 45;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div style="width: 400px;">
<canvas id="lineChart" width="400" height="400"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<script src="main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
javascript文件:
var chart = document.getElementById("lineChart");
var data = {
labels: [2012, 2013, 2014, 2015, 2016, 2017],
datasets: [
{
label: "My Chart Label",
fill: false,
lineTension: 0.1,
data: ['(@p1)', '(@p2)', 50, 48, 47, 52]
}
] …Run Code Online (Sandbox Code Playgroud) 我有Anaconda Python 3.4,但每当我运行旧代码时,我会通过输入"source activate python2"切换到Anaconda Python 2.7.我的问题是我为Anaconda Python 3.4安装了psycopg2,但是没有为Anaconda Python 2.7安装.当我运行pip install psycopg2(在Python 2.7上)时,我收到以下消息:
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Run Code Online (Sandbox Code Playgroud)
我对编程很新,需要帮助:
1. Obtaining directory containing pg_config
2. Finding the path to Anaconda Python 2.7
3. Adding pg_config to the PATH.
Run Code Online (Sandbox Code Playgroud)
完成这些步骤后,我应该能够安装安装psycopg2
我正在尝试创建一个没有标题的重复CSV.当我尝试这个时,我收到以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 1895: invalid start byte.
Run Code Online (Sandbox Code Playgroud)
我读过蟒蛇CSV 文档上Unicode和UTF-8编码,并已付诸实施.但是,我的输出文件生成时没有数据.不知道我在这里做错了什么.
import csv
path = '/Users/johndoe/file.csv'
with open(path, 'r') as infile, open(path + 'final.csv', 'w') as outfile:
def unicode_csv(infile, outfile):
inputs = csv.reader(utf_8_encoder(infile))
output = csv.writer(outfile)
for index, row in enumerate(inputs):
yield [unicode(cell, 'utf-8') for cell in row]
if index == 0:
continue
output.writerow(row)
def utf_8_encoder(infile):
for line in infile:
yield line.encode('utf-8')
unicode_csv(infile, outfile)
Run Code Online (Sandbox Code Playgroud) 我的数据帧:
HLLM HXBX JHWO RPNZ ZHNL
2008-08-31 0 0 0 0 0
2008-09-30 0 0 0 0 0
2008-10-31 3 1 0 0 5
2008-11-30 0 -1 0 0 0
Run Code Online (Sandbox Code Playgroud)
我正在尝试将所有不等于 0 的值替换为值 1
df = df.replace(df != 0, 1)
Run Code Online (Sandbox Code Playgroud)
我怎样才能重写它以使其有效?
我使用pip安装了最新版本的Pymongo 3.2.2。另外,我使用Python 3.4 Anaconda。我正在尝试做一个简单的插入。这是我的代码:
>>> from pymongo import MongoClient
>>> client = MongoClient(connect=False)
>>> client["mydb"]
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mydb')
>>> db = client["mydb"]
>>> collection = db["mycollection"]
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mydb'), 'mycollection')
>>> book = {}
>>> book["title"] = "AnyBook"
>>> book["Author"] = "AnyAuthor"
>>> collection.insert(book)
Run Code Online (Sandbox Code Playgroud)
这是我在“回溯”底部得到的消息:
Traceback (most recent call last):
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 8] nodename nor servname provided, or not known
Run Code Online (Sandbox Code Playgroud)
如何解决此问题,以便可以将记录插入MongoDB?
python ×4
pandas ×3
python-2.7 ×2
anaconda ×1
c# ×1
charts ×1
csv ×1
dataframe ×1
javascript ×1
mongodb ×1
psql ×1
pymongo-3.x ×1
python-3.x ×1
scikit-learn ×1
utf-8 ×1