提交 2c53996a 编写于 作者: ToTensor's avatar ToTensor

extract code success

上级 4f3b0959
from keras.applications import ResNet50
from keras.applications import Inception V3
from keras.applications import Mobile Net V2
from keras.applications import Xception
from keras.applications import VGG16
from keras.applications import VGG19
df_housing = pd.read_csv("https://raw.githubusercontent.com/huangjia2019/house/
master/house.csv")
df_housing.head #显示加州房价数据
X = df_housing.drop("median_house_value", axis = 1) #构建特征集X
y = df_housing.median_house_value #构建标签集y
from sklearn.model_selection import train_test_split #导入sklearn工具库
X_train, X_test, y_train, y_test = train_test_split(X, y,
from sklearn.linear_model import Linear Regression #导入线性回归算法模型
model = Linear Regression() #确定线性回归算法
model.fit(X_train, y_train) #根据训练集数据, 训练机器, 拟合函数
y_pred = model.predict(X_test) #预测验证集的y值print ('房价的真值(测试集)', y_test)
print ('预测的房价(测试集)', y_pred)
plt.plot(X_test.median_income, y_pred, color='green', linewidth=1)
plt.xlabel('Median Income') #x轴:家庭收入中位数
plt.ylabel('Median House Value') #y轴:房价中位数
plt.show() #显示房价分布和机器学习到的函数模型
from sklearn.linear_model import Linear Regression #导入线性回归算法模型
model = Linear Regression() #使用线性回归算法
import numpy as np # 导入Num Py库
import pandas as pd # 导入Pandas库
from keras.datasets import mnist #从Keras中导入MNIST数据集
print ("数据集张量形状:", X_train_image.shape) #用shape方法显示张量的形状
print ("第一个数据样本:\n", X_train_image[0]) #注意Python的索引是从0开始的
from keras.utils import to_categorical # 导入keras.utils工具库的类别转换工具
X_train = X_train_image.reshape(60000, 28, 28, 1) # 给标签增加一个维度
X_test = X_test_image.reshape(10000, 28, 28, 1) # 给标签增加一个维度
y_train = to_categorical(y_train_lable, 10) # 特征转换为one-hot编码
y_test = to_categorical(y_test_lable, 10) # 特征转换为one-hot编码
print ("训练集张量形状:", X_train.shape) # 训练集张量的形状
print ("第一个数据标签:", y_train[0]) # 显示标签集的第一个数据
from keras import models # 导入Keras模型, 以及各种神经网络的层
from keras.layers import Dense, Dropout, Flatten, Conv2D, Max Pooling2D
model = models.Sequential() # 用序贯方式建立模型
model.add(Conv2D(32, (3, 3), activation='relu', # 添加Conv2D层
model.add(Max Pooling2D(pool_size=(2, 2))) # 添加Max Pooling2D层
model.add(Conv2D(64, (3, 3), activation='relu')) # 添加Conv2D层
model.add(Max Pooling2D(pool_size=(2, 2))) # 添加Max Pooling2D层
model.add(Dropout(0.25)) # 添加Dropout层
model.add(Flatten()) # 展平
model.add(Dense(128, activation='relu')) # 添加全连接层
model.add(Dropout(0.5)) # 添加Dropout层
model.add(Dense(10, activation='softmax')) # Softmax分类激活, 输出10维分类码
score = model.evaluate(X_test, y_test) # 在验证集上进行模型评估
print('测试集预测准确率:', score[1]) # 输出测试集上的预测准确率
pred = model.predict(X_test[0].reshape(1, 28, 28, 1)) # 预测测试集第一个数据
print(pred[0], "转换一下格式得到:", pred.argmax()) # 把one-hot编码转换为数字
import matplotlib.pyplot as plt # 导入绘图工具包
plt.imshow(X_test[0].reshape(28, 28), cmap='Greys') # 输出这个图片
import math # 导入数学工具包
y = math.log(100000000, 10)# 以10为底, 在x值等于一亿的情况下
print("以10为底, 求一亿的对数:", y)# 求出y的值为8
import numpy as np #导入Num Py库
X = np.array(5) #创建0D张量, 也就是标量
print("X的值", X)
print("X的阶", X.ndim) #ndim属性显示标量的阶
print("X的数据类型", X.dtype) #dtype属性显示标量的数据类型
print("X的形状", X.shape) #shape属性显示标量的形状
X = np.array([5, 6, 7, 8, 9]) #创建1D张量, 也就是向量
print("X的值", X)
print("X的阶", X.ndim) #ndim属性显示向量的阶
print("X的形状", X.shape) #shape属性显示向量的形状
print("X_train的形状:", X_train.shape)
print("X_train中第一个样本的形状:", X_train[0].shape)
print("y_train的形状:", y_train.shape)
weight = np.array([1, -1.8, 1, 1, 2]) #权重向量(也就是多项式的参数)
X = np.array([1, 6, 7, 8, 9]) #特征向量(也就是一个特定样本中的特征值)
y_hat = np.dot(X, weight) #通过点积运算构建预测函数
print('函数返回结果:', y_hat) #输出预测结果
import numpy as np # 导入Num Py库
list=[1, 2, 3, 4, 5] # 创建列表
array_01=np.array([1, 2, 3, 4, 5]) # 列表转换为数组
array_02=np.array((6, 7, 8, 9, 10)) # 元组转换为数组
array_03=np.array([[1, 2, 3], [4, 5, 6]]) # 列表转换为2D数组
print ('列表:', list)
print ('列表转换为数组:', array_01)
print ('元组转换为数组:', array_02)
print ('2D数组:', array_03)
print ('数组的形状:', array_01.shape)
print'列表的形状:', list.shape# 列表没有形状, 程序会报错
array_06 = np.arange(10)
print (array_06, '形状是', array_06.shape, '阶为', array_06.ndim)
array_06 = array_06.reshape(10, 1)
print (array_06, '形状是', array_06.shape, '阶为', array_06.ndim)
array_08 = np.array([[0, 0, 0], [10, 10, 10], [20, 20, 20], [30, 30, 30]])
array_09 = np.array([[0, 1, 2]])
array_10 = np.array([[0], [1], [2], [3]])
list_11 = [[0, 1, 2]]
print ('array_09的形状:', array_09.shape )
print ('array_10的形状:', array_10.shape )
array_12 = array_09.reshape(3)
print ('array_12的形状:', array_12.shape )
array_13 = np.array([1])
print ('array_13的形状:', array_13.shape )
array_14 = array_13.reshape(1, 1)
print ('array_14的形状:', array_14.shape )
print ('08 + 09结果:', array_08 + array_09)
print ('08 + 10结果:', array_08 + array_10)
print ('08 + 11结果:', array_08 + list_11)
print ('08 + 12结果:', array_08 + array_12)
print ('08 + 13结果:', array_08 + array_13)
print ('08 + 14结果:', array_08 + array_14)
else if, 上述条件都不满足, 那么两个数组当前阶不兼容, 不能够进行广播操作
vector_01 = np.array([1, 2, 3])
vector_02 = np.array([[1], [2], [3]])
vector_03 = np.array([2])
vector_04 = vector_02.reshape(1, 3)
print ('vector_01的形状:', vector_01.shape)
print ('vector_02的形状:', vector_02.shape)
print ('vector_03的形状:', vector_03.shape)
print ('vector_04的形状:', vector_04.shape)
print ('01和01的点积:', np.dot(vector_01, vector_01))
print ('01和02的点积:', np.dot(vector_01, vector_02))
print ('04和02的点积:', np.dot(vector_04, vector_02))
print ('01和数字的点积:', np.dot(vector_01, 2))
print ('02和03的点积:', np.dot(vector_02, vector_03))
print ('02和04的点积:', np.dot(vector_02, vector_04))
print ('01和03的点积:', np.dot(vector_01, vector_03))
print ('02和02的点积:', np.dot(vector_02, vector_02))
matrix_01 = np.arange(0, 6).reshape(2, 3)
matrix_02 = np.arange(0, 6).reshape(3, 2)
print(matrix_01)
print(matrix_02)
print ('01和02的点积:', np.dot(matrix_01, matrix_02))
print ('02和01的点积:', np.dot(matrix_02, matrix_01))
print ('01和01的点积:', np.dot(matrix_01, matrix_01))
array_04=np.arange(1, 5, 1) # 通过arange函数生成数组
array_05=np.linspace(1, 5, 5) # 通过linspace函数生成数组
print (array_04)
print (array_05)
array_06 = np.arange(10)
print (array_06)
index_01 = array_06[3] # 索引—第4个元素
print ('第4个元素', index_01)
index_02 = array_06[-1] # 索引—最后一个元素
print ('第-1个元素', index_02)
slice_01 = array_06[:4] # 从0到4切片
print ('从0到4切片', slice_01)
slice_02 = array_06[0:12:4] # 从0到12切片, 步长为4
print ('从0到12切片, 步长为4', slice_02)
array_07 = np.array([[1, 2, 3], [4, 5, 6]])
print (array_07[1:2], '它的形状是', array_07[1:2].shape)
print (array_07[1:2][0], '它的形状又不同了', array_07[1:2][0].shape)
print (array_07, '形状是', array_07.shape)
print (array_07.reshape(3, 2), '形状是', array_07.reshape(3, 2).shape)
X_train, X_test = scaler(X_train, X_test) #对特征归一化
y_train, y_test = scaler(y_train, y_test) #对标签也归一化
plt.plot(X_train, y_train, 'r.', label='Training data') # 显示训练数据
plt.xlabel('wechat') # x轴标签
plt.ylabel('sales') # y轴标签
plt.legend() # 显示图例
plt.show() # 显示绘图结果
df_ads = pd.read_csv('../input/advertising-simple-dataset/advertising.csv')
df_ads.head()
import matplotlib.pyplot as plt #Matplotlib为Python画图工具库
import seaborn as sns #Seaborn为统计学数据可视化工具库
sns.heatmap(df_ads.corr(), cmap="Yl Gn Bu", annot = True)
plt.show() #plt代表英文plot, 就是画图的意思
X = np.array(df_ads.wechat) #构建特征集, 只含有微信公众号广告投放金额一个特征
y = np.array(df_ads.sales) #构建标签集, 销售额
print ("张量X的阶:", X.ndim)
print ("张量X的形状:", X.shape)
print ("张量X的内容:", X)
X = X.reshape((len(X), 1)) #通过reshape方法把向量转换为矩阵, len函数返回样本个数
y = y.reshape((len(y), 1)) #通过reshape方法把向量转换为矩阵, len函数返回样本个数
print ("张量X的阶:", X.ndim)
print ("张量X的形状:", X.shape)
print ("张量X的内容:", X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
print ("当权重为5, 偏置为3时, 损失为:",
loss_function(X_train, y_train, weight=5, bias=3))
print ("当权重为100, 偏置为1时, 损失为:",
loss_function(X_train, y_train, weight=100, bias=1))
y_hat = weight*X + bias # 这是向量化运算实现的假设函数
loss = y_hat-y # 这是中间过程, 求得的是假设函数预测的y'和真正的y值之间的差值
derivative_wight = X.T.dot(loss)/len(X) # 对权重求导, len(X)就是样本总数
derivative_bias = sum(loss)*1/len(X)  # 对偏置求导, len(X)就是样本总数
weight = weight - alpha*derivative_wight # 结合学习速率alpha更新权重
bias = bias - alpha*derivative_bias # 结合学习速率alpha更新偏置
iterations = 100# 迭代100次
alpha = 1# 初始学习速率设为1
weight = -5 # 权重
bias = 3 # 偏置
plt.plot(X_train, y_train, 'r.', label='Training data') # 显示训练数据
line_X = np.linspace(X_train.min(), X_train.max(), 500) # X值域
line_y = [weight*xx + bias for xx in line_X] # 假设函数y_hat
plt.plot(line_X, line_y, 'b--', label='Current hypothesis' ) #显示当前假设函数
plt.xlabel('wechat') # x轴标签
plt.ylabel('sales') # y轴标签
plt.legend() # 显示图例
plt.show() # 显示函数图像
plt.plot(loss_history, 'g--', label='Loss Curve') # 显示损失曲线
plt.xlabel('Iterations') # x轴标签
plt.ylabel('Loss') # y轴标签
plt.legend() # 显示图例
plt.show() # 显示损失曲线
plt.plot(X_train, y_train, 'r.', label='Training data') # 显示训练数据
line_X = np.linspace(X_train.min(), X_train.max(), 500) # X值域
line_y = [weight_history[-1]*xx + bias_history[-1] for xx in line_X] # 假设函数
plt.plot(line_X, line_y, 'b--', label='Current hypothesis' ) # 显示当前假设函数
plt.xlabel('wechat') # x轴标签
plt.ylabel('sales') # y轴标签
plt.legend() # 显示图例
plt.show() # 显示函数图像
X = np.array(df_ads) # 构建特征集, 包含全部特征
X = np.delete(X, [3], axis = 1) # 删除标签
y = np.array(df_ads.sales) #构建标签集, 销售额
print ("张量X的阶:", X.ndim)
print ("张量X的维度:", X.shape)
print (X)
print("权重历史记录:", weight_history)
print("损失历史记录:", loss_history)
X_plan = [250,50,50] # 要预测的X特征数据
X_train,X_plan = scaler(X_train_original,X_plan) # 对预测数据也要归一化缩放
X_plan = np.append([1], X_plan ) # 加一个哑特征X0 = 1
y_plan = np.dot(weight_history[-1],X_plan) # [-1] 即模型收敛时的权重
y_value = y_plan*23.8 + 3.2 #23.8是当前y_train中最大值和最小值的差,3.2是最小值
print ("预计商品销售额:",y_value, "千元")
x0_train = np.ones((len(X_train), 1)) # 构造X长度的全1数组配合对偏置的点积
X_train = np.append(x0_train, X_train, axis=1) #把X增加一系列的1
print ("张量X的形状:", X_train.shape)
print (X_train)
def loss_function(X, y, W): # 手工定义一个均方误差函数, W此时是一个向量
iterations = 300# 迭代300次
alpha = 0.15#学习速率设为0.15
weight = np.array([0.5, 1, 1, 1]) # 权重向量, w[0] = bias
import numpy as np # 导入Num Py库
import pandas as pd # 导入Pandas库
df_heart = pd.read_csv("../input/heart-dataset/heart.csv") # 读取文件
df_heart.head() # 显示前5行数据
dimension = X.shape[1] # 这里的维度len(X)是矩阵的行的数目, 维度是列的数目
weight = np.full((dimension, 1), 0.1) # 权重向量, 向量一般是1D, 但这里实际上创建了2D张量
bias = 0 # 偏置值
y_pred = predict(X_test, weight_history[-1], bias_history[-1]) # 预测测试集
testing_acc = 100 - np.mean(np.abs(y_pred - y_test))*100 # 计算准确率
print("逻辑回归测试准确率: {:.2f}%".format(testing_acc))
loss_history_test = np.zeros(iterations) # 初始化历史损失
for i in range(iterations): #求训练过程中不同参数带来的测试集损失
index = np.arange(0, iterations, 1)
plt.plot(index, loss_history, c='blue', linestyle='solid')
plt.plot(index, loss_history_test, c='red', linestyle='dashed')
plt.legend(["Training Loss", "Test Loss"])
plt.xlabel("Number of Iteration")
plt.ylabel("Cost")
plt.show() # 同时显示训练集和测试集损失曲线
from sklearn.linear_model import Logistic Regression #导入逻辑回归模型
lr = Logistic Regression() # lr, 就代表是逻辑回归模型
lr.fit(X_train, y_train) # fit, 就相当于是梯度下降
print("SK learn逻辑回归测试准确率{:.2f}%".format(lr.score(X_test, y_test)*100))
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册