美文网首页
01 - Plotting Vectors

01 - Plotting Vectors

作者: 西瓜三茶 | 来源:发表于2017-07-05 21:04 被阅读0次

1.Plot vectors

采用如下的方法绘图

import matplotlib.pyplot as plt
plt.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=1)

举例说明:

  • x: 是所有需要绘制的向量起点的横坐标,依序排列
  • y: 是所有需要绘制的向量起点的纵坐标
  • U:每个向量横跨的长度单位
  • V:每个向量纵跨的长度单位
import numpy as np
import matplotlib.pyplot as plt

# We're going to plot three vectors
# The first will start at origin 0,0, then go over 1 and up 2
# The second will start at origin 1,2, then go over 3 and up 2
# The third will start at origin 0,0, then go over 4 and up 4

X = [0,1,0]
Y = [0,2,0]
U = [1,3,4]
V = [2,2,4]
# Create the plot
plt.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=1)
# Set the x-axis limits
plt.xlim([0,6])
# Set the y-axis limits
plt.ylim([0,6])
# Show the plot
plt.show()

2- np.dot

矩阵点乘

3-LinearRegression

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
x = cars[["weight"]].values
y = cars["mpg"].values
lr.fit(x, y)

预测

import sklearn
from sklearn.linear_model import LinearRegression
lr = LinearRegression(fit_intercept=True)
lr.fit(cars[["weight"]], cars["mpg"])
predictions = lr.predict(cars[["weight"]])
print (predictions[0:5])
print (cars["mpg"][0:5])

画图scatter

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
cars.plot("weight", "mpg", kind='scatter', ax=ax1)
cars.plot("acceleration", "mpg", kind='scatter', ax=ax2)
plt.show()

画预测点和实际点

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
se = pd.Series(predictions)
cars['predictions'] = se.values
plt.scatter(cars["weight"], cars["mpg"], c='red')
plt.scatter(cars["weight"], predictions, c='blue')

算MSE: mean_squared_errors

from sklearn.metrics import mean_squared_error
lr = LinearRegression()
lr.fit(cars[["weight"]], cars["mpg"])
predictions = lr.predict(cars[["weight"]])
mse = mean_squared_error(cars["mpg"], predictions)
print (mse)
rmse = math.sqrt(mse)
print (rmse)

清理数据,去掉?,将数据转变为float

import pandas as pd
columns = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration", "model year", "origin", "car name"]
cars = pd.read_table("auto-mpg.data", delim_whitespace=True, names=columns)
filtered_cars = cars[cars['horsepower'] != '?']
filtered_cars['horsepower'] = filtered_cars['horsepower'].astype('float')

Logistic Regression
The fit method requires that the first input be formatted with the following dimensions: num_features by num_labels. We'll need to use admissions[["gpa"]] instead of admissions["gpa"]. Compare print(admissions[["gpa"]].shape) with print(admissions["gpa"].shape) to understand the difference.

print(admissions[["gpa"]].shape)
# returns (644,1)
print(admissions[["gpa", "actual_label]].shape)
# returns (644,2)
print(admissions["gpa"].shape)
# returns (644,)
logistic_model = LogisticRegression()
logistic_model.fit(admissions[["gpa"]], admissions["admit"])
pred_probs = logistic_model.predict_proba(admissions[["gpa"]])
plt.scatter(admissions["gpa"], pred_probs[:,1])
plt.show()

另外,.predict也可以达到同样的效果

logistic_model = LogisticRegression()
logistic_model.fit(admissions[["gpa"]], admissions["admit"])
fitted_labels = logistic_model.predict(admissions[["gpa"]])
print (fitted_labels[0])

相关文章

  • 01 - Plotting Vectors

    1.Plot vectors 采用如下的方法绘图 举例说明: x: 是所有需要绘制的向量起点的横坐标,依序排列 y...

  • 01-01 Reading and plotting stock

    读取并绘制股票数据 读取AAPL csv 数据到DataFrame 计算最高收盘价 计算平均成交量 绘制股价数据 ...

  • advanced R. Data structure

    Vector Vectors come in two flavours: atomic vectors and l...

  • Plotting

    Plotting 俗称 P 盘,是 POC 共识的第一个步骤。 第一步:选择一个8字节的随机数Nonce,加上Ac...

  • Vectors

    1 Vectors Vector的结构: vector的元素可以使任意型别的T,但必须具备assignable和c...

  • 线性代数知识点整理

    本文目录 1、线性系统Linear System 2、Vectors、Matrices 2.1 向量Vectors...

  • 使用Chinese-Word-Vectors作为pytorch中

    如何在深度学习中使用开源Chinese Word Vectors 摘要:Chinese-Word-Vectors开...

  • pca 椭圆 ggplot

    from Plotting PCA (Principal Component Analysis)

  • TrendVis

    TrendVis TrendVis is a plotting package that uses matplot...

  • bokeh简介

    bokeh bokeh.charts bokeh.plotting

网友评论

      本文标题:01 - Plotting Vectors

      本文链接:https://www.haomeiwen.com/subject/yehgcxtx.html