簡體   English   中英

如何在 Python 中導入這個數據文件?

[英]How do I import this file of data in Python?

所以我剛開始使用 Python,我不知道如何以我想要的方式導入文本文件。

到目前為止,這是我的代碼:

f = open("Data.txt", "r")
attributes = f.readlines()

w1 = 2 * np.random.random((1,19)) -1

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

outputs = sigmoid(np.dot(attributes, w1))

所以這里的問題是我收到錯誤消息:

Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'.

我知道問題在於代碼沒有將文本文件作為數字數組讀取,這就是我收到錯誤的原因。

這是文本文件中的一行:

1,1,22,22,22,19,18,14,49.895756,17.775994,5.27092,0.771761,0.018632,0.006864,0.003923,0.003923,0.486903,0.100025,1,0

您快到了 :

import numpy as np
with open("Data.txt", "r") as f:
    attributes = f.readlines()
attributes = [row.strip().split(",") for row in attributes]
attributes = [[float(x) for x in row] for row in attributes]

w1 = 2 * np.random.random((1,20)) -1

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

outputs = sigmoid(np.dot(np.array(attributes), w1.T))

您需要諸如“列表理解”之類的東西來分割每一行的值(如果有多個)。 此外,您在 w1 中缺少一個元素來匹配屬性的維度。 最后,您將不得不使用 w1 的轉置來使用 np.dot(np.array(attributes) 的形狀是 (1,20) 以及 w1 的形狀。

另外:永遠記住使用這個“with”語句來打開文件(它會在語句結束后自動關閉文件;否則,你可能會遇到一些麻煩......)


編輯

如果您的真實數據集包含更多數據,您應該考慮使用 Pandas,這樣效率會更高:

import pandas as pd
df = pd.read_csv("Data.txt"), sep=",", header=None)
numpy_array = df.values

我認為你想要做的是如下:

import numpy as np


f = open("Data.txt", "r")

# Read the file, replace newline by "", split the string on commas
attributes = f.read().replace("\n", "").split(",")

# convert every element from string to float
attributes = np.array([float(element) for element in attributes])

# 20 random values in [-1,1)
w1 = 2 * np.random.random(20) - 1

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

outputs = sigmoid(np.dot(attributes, w1))

# Printing results
print(attributes)
print(w1)
print(outputs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM